Removing an object from a Java game (eclipse)

lets get straight into it, I have a "handler" class that is filled with getters and setters, and it includes the code that adds and removes an object, looks like this:

public void addObject(GameObject object){
    this.object.add(object);
}
public void removeObject(GameObject object){
    this.object.remove(object);

      

Note that 'GameObject' is a class and all objects extend this class And then I create an object here,

if(mouseOver(mx, my, 840/2-100, 149, 200, 64)){
        game.gameState = STATE.Game;
        handler.addObject(new Player(0, 300, ID.Player,game.playerImg, game));
        handler.addObject(new BasicEnemy(700, 300, ID.BasicEnemy, game.enemyImg, game));
        handler.addObject(new F1Jutsu(400, 300, ID.F1Jutsu, game.f1jutsuImg, game));
    }

      

Internal material parameters are parameters of the object that I want to add. Of course, each object is its own class. Now I want to delete the F1Jutsu object if that x value is out of play and the object moves to the right every second (which works, so I won't insert it here, will do if given)

        if(x > 800){
        handler.removeObject(this);
    }

      

I have it inside the "tick" function (inside the F1Jutsu class), which is similar to the launch function. The problem is that as soon as the removeObject method is called, I get a nullpointer exception, the following error:

Exception in thread "Thread-2" java.lang.NullPointerException
at com.ninja.main.F1Jutsu.tick(F1Jutsu.java:24)
at com.ninja.main.Handler.tick(Handler.java:14)
at com.ninja.main.Game.tick(Game.java:110)
at com.ninja.main.Game.run(Game.java:87)
at java.lang.Thread.run(Unknown Source)

      

Basically, there is a NullPointerException (im removing null, but its an object) in the F1Jutsu class where it is prompted to delete the object, then all the places calling the object's delete method and everywhere else calling the place calling the deleting object, etc. raises an error.
I think the idea might be that the value of the object x is now null, that is, nullpointer (?), But not sure and if so how would I fix this?
I'm sorry for the long post (potatoes?)
EDIT:

line 24 F1Jutsu:

if(x > 800){
        handler.removeObject(this);
    }

      

MRK im not sure what you mean, i have included the part where im adds an object and where im deletes in the code above.
EDIT:
Ok, after a lot of work I've come to the conclusion that when I add an object, it is added as a null image. I have to ask how should I set anything (which is not null) without changing the fundamentals of my code (parameters)

+3


source to share


1 answer


You shouldn't call remove in the middle of a code block on the object you are removing; although it will be removed, it will try to end the code, but will fail because all of its variables will be null

. To fix this error, I suggest adding boolean

under the title removed

to your class GameObject

. Instead of calling, handler.removeObject(this)

just set removed

equal true

. Then you need to add a few lines of code to your Handler

class in the method tick()

to check and remove all objects with a removed

value true

. Something like that



for (int i = 0; i < object.size(); i++) {
     if (object.get(i).removed)
          object.remove(i);
}

      

+1


source







All Articles