This site requires JavaScript, please enable it in your browser!
Greenfoot back
erdelf
erdelf wrote ...

2013/1/5

NullPointerException

1
2
3
danpost danpost

2013/1/5

#
Change line 57 to the following and report back as to what happens:
public void damage(int value)
{
    health -= value;
    System.out.println("bar exists: " + (bar instanceof Bar.class));
    System.out.println("bar in world: " + (bar.getWorld() != null));
    System.out.println("bar value: " + bar.getValue());
    bar.setValue(health);
}
erdelf erdelf

2013/1/5

#
this line gives me an error during compiling:
        System.out.println("bar exists: " + (bar instanceof Bar.class));  
identifier expected, pointed at the bar.class
danpost danpost

2013/1/5

#
try 'bar instanceof Bar' without the 'dot-class'.
erdelf erdelf

2013/1/5

#
bar exists: false after that, nullpointer exception at the second system.out line. does that mean that the bar is not even existent?
danpost danpost

2013/1/5

#
OK, remove those System.out.println lines from the 'damage' method. Now, in the 'addedToWorld' method, add the following line
System.out.println("Added to world");
and report back.
danpost danpost

2013/1/5

#
And just a thought, what is the heirarchy chain of the Bar class? Bar >> Interface >> .... >> Actor? And is the Interface class an abstract class for all interface objects?
erdelf erdelf

2013/1/5

#
Added to world the same nullpointerexception, we had at the start, in the setValue line. btw Interface is just a class for organization, it has no own methods or constructors, Interface is a subclass of actor
danpost danpost

2013/1/5

#
OK, try these at the end of the 'addedToWorld' method:
System.out.println("bar exists: " + (bar instanceof Bar));
System.out.println("bar in world: " + (bar.getWorld() != null));
System.out.println("bar value: " + bar.getValue());
erdelf erdelf

2013/1/5

#
Added to world bar exists: true bar in world: true bar value: 50 after that nullpointer
danpost danpost

2013/1/5

#
So somewhere after creating the ActorL object and the Bar object, at the very least, the value of 'bar' is being changed back to null. (even if the bar object is removed from the world, the reference 'bar' should still hold the Bar object created and setting or changing the value of the Bar object would still be functional).
erdelf erdelf

2013/1/5

#
after creating I only use it at the damage method
danpost danpost

2013/1/5

#
Comment out line 33 of the Turret class and report what happens. EDIT: no, that should do anything, sorry.
erdelf erdelf

2013/1/5

#
33 is? we changed the code a bit, i dont know which line you mean?
danpost danpost

2013/1/5

#
Let us simplify your act method of the Bar class to:
public void act()
{
    if(actor.getWorld() != null)
    {
        if(actor instanceof Offender)
        {
            setValue(((Offender)actor).getHealth());
            setLocation(actor.getX(),actor.getY()-15);
        }
        if(actor instanceof Turret)
        { 
            setValue(((Turret)actor).getHealth());
            setLocation(actor.getX()-5,actor.getY()-20); 
        }
    }
}
See if that does anything (cannot say for sure if it would).
danpost danpost

2013/1/5

#
I do not really understand why you added the act method in the Bar class. Your Turret and Offender classes should be calling 'bar.setLocation(getX()-5, getY()-20);' (or using the other offsets) and 'bar.setValue(health);' when they are moved or the health value changes.
There are more replies on the next page.
1
2
3