-
Quick help needed
I am trying to create a game of 'Pig' using a precoded die and gui class (school project), and am getting the following exception:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1045)
at java.awt.Container.add(Container.java:365)
at GUI.<init>(GUI.java:53)
at GUI.main(GUI.java:37)
It is flagging these lines of code (denoted with '<<<----------'):
Code:
public static void main(String args[]){
// the traditional five lines of code from the book are
// provided in the GUI constructor
new GUI();
}
public GUI(){ <<<---------
// create the game object as well as the GUI Frame
ButtonListener buttonListener= new ButtonListener();
game = new Pig();
myGUI = new JFrame();
myGUI.setSize(400,400);
myGUI.setTitle("Taylor's Game of Pig");
// place the die in the middle of the screen
JPanel panel = new JPanel();
d1 = game.getDie(1);
d2 = game.getDie(2);
panel.add(d1); <<<-----------
panel.add(d2);
myGUI.add(panel);
I am kinda sick of screaming questionable things at my computer, so any help is appreciated.
ps. ready to commence facedesking when I find out it's something really stupid.
-
Re: Quick help needed
d1 is null? So it looks like that getDie(..) returns null. Why? We don`t know, take a look at your Pig class!
-
Re: Quick help needed
Well, thanks for the help, but I think I found the problem:
I misread what was going to what, and it turns out my Pig class needs a complete rewrite.
In other words, PICNIC error: Problem In Chair, Not In Computer.
Cheers!
AlternateLives
-
Re: Quick help needed
Please go through the Forum Rules -- particularly the third paragraph.
db