Question

Okay, here's this problem I have in my java class. Upon searching, I didn't find any results, so. I have this string value for x, y and z positions in one of my classes named 'statpedia'...

public class Statpedia   
{
    private game conto;

    String car_x = Integer.toString(conto.x / 100     ); String display_car_x  = "X: "+ car_x; 
    String car_y = Integer.toString(conto.y + 27 / 100); String display_car_y  = "Y: "+ car_y;
    String car_z = Integer.toString(conto.z / 100     ); String display_car_z  = "Z: "+ car_z; 

}

And in another class graphimage a void...

public void stateInGame(Statpedia stat)
{
    rd.drawString(stat.display_car_x, 20, 360);
...
}

Now, rd is just graphics2D and drawing strings work perfectly fine, but when the void is called and it tries to draw display_car_x, I get this:

Exception in thread "Thread-2" java.lang.NullPointerException
at applet.xtGraphics.stateInGame(GraphImage.java:716)
at applet.GameSparker.run(MainGame.java:1092)
at java.lang.Thread.run(Thread.java:744)

What is happening? And how can I transfer the strings? Thanks in advance.

Was it helpful?

Solution 2

Default values of class variables.:

enter image description here

So, you need to initialize conto. So that it is not null.

If you have not written constructor in game class. Do it by calling default constructor like :

private game conto = new game();

OTHER TIPS

Initialise the object as below :

private game conto = new game(); // Please use the class name starting with uppercase

You only create the reference and trying to get the instance variable due to which it throws NPE

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top