1

So basically what I'm trying to do is have a boss spawn (ever 10 levels) and have every other enemy currently on screen removed. All object are in a linkedlist and extend GameObject.

In GameObject I have removed as a boolean and:

public GameObject(float x, float y, ID id) {
    this.x = x;
    this.y = y;
    this.removed = removed;
    this.id = id;
}

Going off another suggestion from a previous thread, in the handler I have:

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

The way I'm checking the level and removing the enemy is (basic enemy line 41) This is in the basic enemies class:

if(hud.level % 10 == 0) {
        this.removed = true;
    }

Every time I run the code and an enemy spawns in it throws a nullPointerException at line 41 of basic enemy. Full error is:

Exception in thread "Thread-0" java.lang.NullPointerException
at BasicEnemy.tick(BasicEnemy.java:41)
at Handler.tick(Handler.java:13)
at Game.tick(Game.java:92)
at Game.run(Game.java:73)
at java.base/java.lang.Thread.run(Thread.java:844)

The int level is public and set to 1 at the start

I'm not very good with LinkedLists so this is pretty confusing atm.

edit

hud.level is

public int level = 1;

HUD is the class which keeps track of overlays, current level, and score the code for increasing score and level is:

public void tick() {

    HEALTH = Game.clamp(HEALTH, 0, 100);
    greenValue = Game.clamp(greenValue, 0, 255);

    greenValue = HEALTH * 2;

    score++;

}

Level:

public void tick() {
    scoreKeep ++;

    if(scoreKeep >= 100) {
        scoreKeep = 0;
        hud.setLevel(hud.getLevel() + 1);
John-
  • 33
  • 2

1 Answers1

1

Since you are calling this.removed = true;, we know this is a boolean or a Boolean. I

public GameObject(float x, float y, ID id) {
    this.x = x;
    this.y = y;
    this.removed = removed; // ####
    this.id = id;
}

But you don't have a removed parameter, so it is the same as this.removed = this.removed or the same as not doing it.

Now, there is two possibitlies for removed type :.

  • a boolean default false
  • a Boolean, default null

Calling object.get(i).removed == true would trigger a NullPointerException if this is a Boolean (because of the out boxing from Boolean to a boolean).

FYI:

The loop to removed every flag value could be changed to use Collection.removeIf that takes a predicate (here, the getter returning a boolean) to remove every flagged items (the method iterate the collection)

object.removeIf(GamObject::isRemoved)

Assuming you have a getter public boolean isRemoved() in GameObject.

AxelH
  • 14,325
  • 2
  • 25
  • 55