0

I got a null pointer exception when referencing position. In the debug view i found 2 different variables with the same name. One seems to be null and has a green circle, one is the correct variable and has a blue triangle next to it.

The View of my Debugger

Why is my code referencing the null variable and why would there be 2 copies of that variable in memory?

The position gets set in the constructor here

public Obstacle(int x, int y) {
  position = new PVector(x,y);

}

The constructor gets called from a level generator class here

obstacle1 = new Obstacle(levelWidth/4, 375);
obstacle2 = new Obstacle(levelWidth/2, 375);
obstacle3 = new Obstacle(levelWidth*3/4, 375);

Not sure what other code to show.

Stephan
  • 16,509
  • 7
  • 35
  • 61
  • 2
    local/global scope perhaps? – Floris Dec 12 '13 at 20:45
  • 3
    We can't answer this without seeing more code. – arshajii Dec 12 '13 at 20:47
  • argument `c` in method `collidesWith` is `null` that's why it's throwing `npe`, post `CodeManager::detectCollisions` – Eugen Halca Dec 12 '13 at 20:47
  • 1
    "Not sure what other code to show": in general, a [SSCCE](http://sscce.org/) is your best bet. – Floris Dec 12 '13 at 20:50
  • Post your entire `Obstacle` class. I agree that it's probably a scope issue. Your blue triangles indicate default fields, the green circle public fields. – Andrew Dec 12 '13 at 20:50
  • Looks to me like you're shadowing the variable you want with another variable of the same name. – Boris the Spider Dec 12 '13 at 20:56
  • just a guess: Obstacle is a subclass of Collidable and both have the fields with the same name (e.g., height) or another superclass of Obstacle has these fields. Moreover, I guess you are accessing the wrong position field (the one o them is null) and thus getting the NPE. – s106mo Dec 12 '13 at 21:29
  • @Floris i think you're on the right track with the scope, looking into superclass variable override conflicts. I think I adhered to SSCCEE as well Thanks. – Stephan Dec 12 '13 at 21:35
  • @s106mo I think you got it, testing now – Stephan Dec 12 '13 at 21:36
  • addition: the field you want to access has the visibility package private, so you do not access it, but instead the public field of the superclass (which is null). To prevent such errors in the future, do not use duplicated fields in subclasses and use getter methods to access the field values. – s106mo Dec 12 '13 at 21:39
  • @s106mo thanks man, my problem was that I declared it in the subclass and the superclass, i deleted the subclass declaration and it works. If you post a fairly detailed answer with some example code i'll accept/vote you – Stephan Dec 12 '13 at 21:44
  • you are welcome, I posted a comment that combines my both comments :) – s106mo Dec 12 '13 at 22:04

2 Answers2

1

Green circle indicates a public method

Red square indicates a private method

Yellow diamond indicates a protected method

Blue triangle indicates default (package visible) method

You can see the difference between these two icons in What do the icons for methods in Eclipse mean?

Community
  • 1
  • 1
Thiago Falcao
  • 4,463
  • 39
  • 34
1

The problem is that you have the field point both in the superclass and the subclass. Most likely you are setting the field of the superclass correctly but "forget" to set the field of the subclass. Consider following example:

class Super {
    Boolean exist;
}

class Sub extends Super {
    Boolean exist;
    Sub() {
        super.exist = true;
    }
}

when you execute following code::

Sub sub = new Sub();
System.out.println(sub.exist);

null will be printed because its exist field of Sub has not been initiated.

To prevent such errors in the future, do not use duplicated fields in subclasses and use getter methods to access the field values.

s106mo
  • 1,243
  • 2
  • 14
  • 20