java - Loop through array of objects inside the class -
i having trouble looping through array objects, inside class. wrote little demo here can follow:
tank tanks[] = new tank[2]; tanks[0] = new tank(); tanks[1] = new tank(); tanks[0].dostuff(tanks); dostuff(tank[] tanks) { (int = 0; < tanks.length; i++) { if (tanks[i].equals(this)) continue; // stuff } }
so, have array type tank. call method dostuff inside tank class. method takes array , loops through it. , want stuff every tank not current instance of class. hope can make sense out of code , description.
the problem nullpointerexception if (tanks[i].equals(this))
what doing wrong here?
that means tanks[i]
null. (or overridden equals()
method has bug)
you need check that.
Comments
Post a Comment