-1

I am working on a Java project that deals with projecting a grid to the user using the paint() method along with a JFrame and Canvas.

I have a class called Runner, which includes my main method. Within Runner, I have the java paint() method. I also have a public variable of type Player within the Runner class. The problem arises when I try to call a public function of the Player instance variable. I get the following run-time error:

Run-time error

Can you not call other functions from inside the paint() method? The Java documentation didn't mention that that action would be bad, as far as I can tell with my limited Java understanding.

I will include some of my code below. I will do my best to only include pieces of the code that are relevant to the problem to make your job easier.

This statement is within my class Runner:

//Player
public Player player;

Here is the code from the paint method:

public void paint(Graphics g)
{
    //(Code that draws grid from 2D array to screen)
    // . . .

    //Draw player
    int playerX = 1 + (int)numRows/2;
    int playerY = 1 + (int)numCols/2;

    //CAUSE OF ERROR*******************
    //Sets playerX and playerY
    player.setPlayerX(playerX);
    player.setPlayerY(playerY);
    //*********************************
    array[playerX][playerY] = 1;
}

Here is the code from the class Player:

public class Player {

private int playerX;
private int playerY;
private int playerHealth;

//Constructor
Player()
{
    playerHealth = 100;
}

//Getters
// . . .

//Setters
public void setPlayerX(int x)
{
    playerX = x;
}

public void setPlayerY(int y)
{
    playerY = y;
}
}

If more code is needed to diagnose the issue please let me know and I will happily provide it. Thank you so much for your time.

Suricata
  • 11
  • 5
  • Also, read the tutorial on custom painting, **"By now you know that the paintComponent method is where all of your painting code should be placed. "** : https://docs.oracle.com/javase/tutorial/uiswing/painting/closer.html – markspace Mar 20 '18 at 16:12
  • So do you have some code somewhere which has ``player = new Player();`` in it? If so, that code is not called for whatever reason. If not, well you need to create a Player object. – Gimby Mar 20 '18 at 16:18
  • @Gimby That's it. The code wasn't reaching that statement. Thank you so much! – Suricata Mar 20 '18 at 16:22

2 Answers2

0

As @Gimby pointed out, the problem was likely resulting due to the statement player = new Player() either not being included or not being reached. This was the issue. I moved the line player = new Player() into the Runner constructor, since for some reason I forgot to put it there. Thanks to everyone and especially @Gimby for the assistance.

Suricata
  • 11
  • 5
-1

show me

Runner class

You have object with value=null, in Runner class, line 145.

And you cath NullPointerException, becouse you call something on object, that = null.

May be it help your situation:

Object o = null;
o.toString(); //throw NullPointerException

And fix it:

Object o = new Object();
o.toString();
Julia
  • 81
  • 1
  • 6
  • Hi Julia, thank you for your response. The issue was actually due to player = new Player() not being reached before the call to player.setPlayerX(). Simple fix that I somehow overlooked. – Suricata Mar 20 '18 at 16:27