• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading database data onto a GUI textfield  RSS feed

 
stan zhu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Having a bit of trouble doing the task in my topic title. I have a database that essentially stores a Player with data like the player id, first name, last name, gamertag, etc. I have to make a gui with a menu bar that has a player menu item, once clicked it will load a random player onto the textfields accordingly.
So i would have a player id label, first name label, last name label, and it would load the data onto those textfields. My database has a Data Access Object, a DAO for shorthand that handles the creation of the table, the update, delete, and add methods. I'm having trouble trying to get it onto the textfields.
I've made a method that would go through the table and then setting the values but im not sure if thats the right way to go at it. Here's my code.

This is my DAO class.


This is the PlayerDao class that would exten my DAO and has a method at the bottom of the code that would select all the rows in my table.


Part of my mainframe code. The values im getting are null because well im not getting them from my database and no values were set. I need some help on getting this to work.


Any help would be greatly appreciated it! Thanks!
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing to jump out as an error is the following line of code:


Seeing that you are operating in a GUI, an exception is being thrown which you might be unaware of...
 
stan zhu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm yeah I believe it's giving me a null pointer exception when I execute the method. I'm pretty new to Gui and databases what would I need to do to fix this? Thank you
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May I ask what were you trying to achieve in that line of code?
 
stan zhu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I wanted to select all the rows in my table and then set the fields but I honestly don't know how to achieve that correctly cause I need to pass the values onto my Gui text fields. It's supposed to pass a random Player data onto the text fields like player id, player name, and etc.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One simple approach you can take is to query the database and upon retrieving a result set, populate an in-memory data structure like a list of players (ArrayList<Player>)...
Then randomly pick one from the index within that structure by using the get(int index) method which you can achieve with the java.util.Random class...
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's have a look at the Java™ Tutorials about databases. I think you will find all your questions answered there.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!