• Post Reply Bookmark Topic Watch Topic
  • New Topic

I think I have gone loopy!

 
Roonie Collins
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am having problems with a dice-rolling GUI. I can't get the value of the dice to appear in the JLabel. Can someone please tell what I am doing wrong here? I would really appreciate it! : )

 
Les Morgan
Rancher
Posts: 717
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, without seeing GameDie class and not having specifically stated what problem you are experiencing in your post, I'm going to go out on a limb and say my crystal ball says you are not initializing your myDie object correctly in your constructor or the class it is instantiated from is not giving you an initial value or you have some other problem in your GameDie class.

Now specifically: do you know how to use a debugger? If not I suggest you stop and learn right now. Don't cut another line of code until you can use your debugger to follow the execution of your code so you can tell exactly what is happening. From your post and description, it is not probable you'll get better advice than that.
 
Paul Clapham
Sheriff
Posts: 21876
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at lines 79 and 119 in your posted code. I predict that trying to set the values of those JLabels will be throwing a NullPointerException, but usually when you run a Swing application you can't see the console so you can't tell. Although perhaps Netbeans allows you to run your application while showing the console?
 
Roonie Collins
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Have a look at lines 79 and 119 in your posted code. I predict that trying to set the values of those JLabels will be throwing a NullPointerException, but usually when you run a Swing application you can't see the console so you can't tell. Although perhaps Netbeans allows you to run your application while showing the console?

Thanks Paul, I didn't declare the JLabel array properly there. I changed the code so that there is only one results JLabel for all of the instances of the GameDie. Thanks again for your friendly and helpful response. I am having a difficult time grasping the syntax; coding can be extremely frustrating for me sometimes. Here is the new code...



Here are the only changes....


JButton dieButton1 = new JButton("roll die");
dieButton1.addActionListener(new DieRollAction());
this.add(dieButton1);

JButton dieButton2 = new JButton("roll die");
dieButton2.addActionListener(new DieRollAction());
this.add(dieButton2);

JButton dieButton3 = new JButton("roll die");
dieButton3.addActionListener(new DieRollAction());
this.add(dieButton3);

JButton dieButton4 = new JButton("roll die");
dieButton4.addActionListener(new DieRollAction());
this.add(dieButton4);
// create a label to display the die's current value


// create a label to display the die's current value

currDieValueLabel = new JLabel("latest roll: " + myDie.getTop());
this.add(currDieValueLabel);

I really wanted to use an array for the die and the JLabels.... Hopefully I can get the other to work somehow because I think it could be slicker... How would I go about constructing an individual actionListener for each button in the array but only have one actionevent?
 
Paul Clapham
Sheriff
Posts: 21876
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roonie Collins wrote:How would I go about constructing an individual actionListener for each button in the array but only have one actionevent?







 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!