d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java.lang.nullpointerexception - Unable To Resolve > Java, Null, Nullpointer, Debug, Eclipse
Add Reply New Topic New Poll
Member
Posts: 111
Joined: Jun 24 2012
Gold: 0.00
Mar 13 2014 05:24pm
I'm experiencing
Code
Exception in thread "main" java.lang.NullPointerException
at Game.<init>(Game.java:98)
at Main_.main(Main_.java:19)erDie;

where line 98 is myDie.setup(xCoord, yCoord, dieNumb);



In my 'Game' class:
Code
public class Game extends Frame
{

private Die myDie, theComputerDie;

public Game()
{

lots of other stuff happening...

myDie.setup(xCoord, yCoord, dieNumb);
theComputerDie.setup(xComp, yComp, dieComp);
}
}

In my 'Die' class:
Code
public class Die
{
private int xCoord, yCoord, dieNumb;

public void setup(int someX, int someY, int someValue)
{
xCoord = someX;
yCoord = someY;
dieNumb = someValue;
}
}


Even if I throw in some integer literals as arguments, it still gives me this error. I Google'd the issue and was unable to make use of any of the information. Any help with this? I think I provided enough information here. If you want more code I can throw it into a pastebin.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 13 2014 05:27pm
in Game's constructor, add this line before you use myDie (line 97):

myDie = new Die();

This post was edited by carteblanche on Mar 13 2014 05:27pm
Member
Posts: 111
Joined: Jun 24 2012
Gold: 0.00
Mar 13 2014 05:49pm
Quote (carteblanche @ Mar 13 2014 07:27pm)
in Game's constructor, add this line before you use myDie (line 97):

myDie = new Die();


Thank you! This worked. One other issue though, it doesn't seem to actually be painting in the Die class. I wrote the Die class for a past program so I know the code is good. I wanted to see if it was passing the information and it is: I put a print statement in the Die class after the data is passed through and it's going in just fine. But it's not painting it. Since I am defining the awt box in the Game class and not the Die class, do I need to repaint() ?

edit/ The next public method in the Die class isn't executing so I added myDie.drawDie(getGraphics()); but it's still not physically drawing my die. That second added print statement in the drawDie method within the Die class is executing with the correct values, but again... it's not drawing. For no reason. Is getGraphics the correct argument? I tried pane, but it wouldn't even compile.

This post was edited by haeload on Mar 13 2014 06:11pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 13 2014 06:33pm
Quote (haeload @ Mar 13 2014 06:49pm)
Thank you! This worked. One other issue though, it doesn't seem to actually be painting in the Die class. I wrote the Die class for a past program so I know the code is good. I wanted to see if it was passing the information and it is: I put a print statement in the Die class after the data is passed through and it's going in just fine. But it's not painting it. Since I am defining the awt box in the Game class and not the Die class, do I need to repaint() ?

edit/ The next public method in the Die class isn't executing so I added myDie.drawDie(getGraphics()); but it's still not physically drawing my die. That second added print statement in the drawDie method within the Die class is executing with the correct values, but again... it's not drawing. For no reason. Is getGraphics the correct argument? I tried pane, but it wouldn't even compile.


Please highlight the relevant code. I can't seem to find it anywhere in your post.

I assume you have a JPanel somewhere, you're adding the panel to your frame, and you're drawing on your panel. Please post that code.
Member
Posts: 111
Joined: Jun 24 2012
Gold: 0.00
Mar 14 2014 12:53pm
Quote (carteblanche @ Mar 13 2014 08:33pm)
Please highlight the relevant code. I can't seem to find it anywhere in your post.

I assume you have a JPanel somewhere, you're adding the panel to your frame, and you're drawing on your panel. Please post that code.


I PM'd you my pastebin of my Die class, were it's being drawn. I'm not using JPanel.

edit/ I created a helper method within my Game class and passed a graphics pane to it. I called my myDie.drawDie method (and it's computer counterpart) in that new paint method in my Game class. Thank you for all your help!

This post was edited by haeload on Mar 14 2014 01:19pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll