NullPointerException, Use variable from another class.

edited June 2016 in Questions about Code

Hello, I have a question. This time I'm making a zombie generator or zombie spawn ( I do not know what to call it ) , so I made a new class "Zombies" in this class I have a constructor and some methods. Generator working now but I have to use PVector "location" from class Player and here is the problem => I have a NullPointerException because I want to use varieable from another class. // Problem is in class Zombies - method "faceToPlayer" ( line 364 ) and i wrote some comments on methods "zombieSpawn()" ( line 163 ) and "ZGenerator()" ( line 173 ) ( in main class ).

If you don't understand something, ask me. Thanks a lot. processingtogether.com/sp/pad/export/ro.K3Qtjdl0csA/rev.36

Answers

  • Is the NullPointerException in line 364?

    because this line looks ok with p.

    Just make sure that the player is defined with new before you come to line 364

  • edited June 2016

    I tried to put p = new Player(width/2-28, height/2-20); after set size ( between lines 42 and 43 ) but still NullPointerException :/ . Player should be created earlier than I call ZGenerate() and zombieSpawn() (line 63 and 64 ) right?

  • When you use it , yes...

    But also the data inside it, the player Position

  • Use println to Check which var is causing the NullPointerException

  • edited June 2016

    I tried every single var but still NullPointerException. Even if I tried var which is defined like int heal = 100; //In Class Player I tried println just 'p' => println(p); and is it null..

  • Is location from class player defined???

  • edited June 2016

    It should by. I have var location and when I create new object Player I give to the var locatoin x and y.

    PVector location; Player(float x, float y) { location = new PVector(x, y); }

    in main class:

    void setup() { // size(1000, 700); fullScreen(1); p = new Player(width/2-28, height/2-20); } And down in draw I call method ZGenerate() where I create new zombie object

  • Answer ✓

    println(p); and is it null..

    that is causing the error!!!!!!!!

    It must not be null!!!!!!!!!!

    Make sure you define p!!!!!!

    There must be only one p in your sketch!!!!!

  • OH MY GOD... Yeah you have right.. I had in main class Player p; and also in class Zombies.. X_X ... Thank you!

Sign In or Register to comment.