0

I am having some trouble looping through an array with objects, inside the class. I wrote a little demo here so you can follow:

Tank tanks[] = new Tank[2];
tanks[0] = new Tank();
tanks[1] = new Tank();
tanks[0].doStuff(tanks);

doStuff(Tank[] tanks) {
    for (int i = 0; i < tanks.length; i++) {
        if (tanks[i].equals(this)) continue;
        // Do stuff
    }
}

So, I have an array with the type Tank. Then I call the method doStuff inside the Tank class. The method takes the array and loops through it. And then I want to do stuff to every tank that is not the current instance of the class. I hope you can make sense out of my code and this description.

The problem is that I get nullPointerException for if (tanks[i].equals(this))

What am I doing wrong here?

4
  • 2
    That code won't compile anyway, as you're trying to treat a single value (Tank tanks) as an array. Please edit your question to provide a short but complete program demonstrating the problem. Commented May 8, 2013 at 15:45
  • Have you reimplemented equals method? If yes, can you show it? That's the only place I think that this null may be comming from, but only if you reimplemented it and done something wrong. The rest is clean. Commented May 8, 2013 at 15:46
  • @JonSkeet you can use that syntax to create an array. It's strange, but he declared it as an array in the variable name. Commented May 8, 2013 at 15:48
  • 1
    @RalfHoppen: The code has been edited. The original declaration had doStuff(Tank tanks). It wasn't the call site I was worried about - it was the method declaration. (It's still not a valid declaration as it has no return type, but the error I mentioned before has been fixed.) A short but complete program demonstrating the problem would still be preferable... Commented May 8, 2013 at 15:49

3 Answers 3

8

That means that tanks[i] is null. (or that your overridden equals() method has a bug)

You need to check for that.

Sign up to request clarification or add additional context in comments.

2 Comments

More likely the latter, given the relatively basic nature of the code provided.
@AnthonyGrist: Given that the code provided wouldn't compile (even after being silently edited), I strongly suspect it's not representative of the real code being run.
1

if you want to compare the IDs of your object you can use == instead of .equals()

doStuff(Tank tanks) {
  for (int i = 0; i < tanks.length; i++) {
    if (tanks[i] == this) {
        continue;
      }
    // Do stuff
  }
}

Comments

1

When I run this code:

public class Tank {
  public static void main(String[] args) {
    Tank tanks[] = new Tank[2];
    tanks[0] = new Tank();
    tanks[1] = new Tank();
    tanks[0].doStuff(tanks);
  }

  public void doStuff(Tank[] tanks) {
      for (int i = 0; i < tanks.length; i++) {
          if (tanks[i].equals(this)) continue;
          // Do stuff
      }
  }
}

No error happens. Therefore, you've probably overridden .equals, and that is where the NullPointerException is occurring. The other possibility is that your simple example doesn't accurately reflect where your bug is occurring.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.