1

im having trouble with null pointer errors in java, im trying to make a breakout game and i know what the problem is its this.

    GObject collider = getElementAt( ballX, ballspeed);

    if(collider.equals(paddle) && collider){
        ballspeed = -ballspeed;
    }

When the if statement it null the program gets the error.

Ive try this too but no luck

    GObject collider = getElementAt( ballX, ballspeed);

    if(collider.equals(paddle) && collider != null){
        ballspeed = -ballspeed;
    }
Steve Kuo
  • 58,491
  • 75
  • 189
  • 247
  • 1
    It should be `collider != null && collider.equals(paddle)` – Mikhail Vladimirov Feb 06 '13 at 20:16
  • 3
    Nearly everyday same question is asked, i am not sure you query the old questions with java null pointer exception equals method or like that. You can check this [http://stackoverflow.com/questions/4501061/java-null-check-why-use-instead-of-equals] for detail explanation. – erhun Feb 06 '13 at 20:18
  • Your bug involves more code than this. The main issue to answer is why does `getElementAt(...)` keep returning null? Is it supposed to return null? If not, you have to dig into this code to see why. Use a debugger or logger to check program state. – Hovercraft Full Of Eels Feb 06 '13 at 22:10

5 Answers5

4

Try other way:

if(collider != null && collider.equals(paddle) ){
        ballspeed = -ballspeed;
    }

Read more about operators in java

kosa
  • 63,683
  • 12
  • 118
  • 157
1

You must do the null-check before you try to call equals(..). The conditions are evaluated from left to right: if the first one is false, the others are not evaluated at all, because the && cannot be true anymore independent what the other conditions are.

  if(collider != null && collider.equals(paddle)){
    ballspeed = -ballspeed;
  }

If you feel insecure about the evaluation order, you can also do the following:

  if(collider != null){
      if(collider.equals(paddle)){
          ballspeed = -ballspeed;
      }
  }
MrSmith42
  • 9,218
  • 6
  • 35
  • 48
  • ive try that but then ballspeed = -ballspeed does nothing – James Hudson Feb 06 '13 at 20:22
  • Have you checked that both conditions are true? (e.g. by using a debugger or logging `collider` and `paddle`) – MrSmith42 Feb 06 '13 at 20:24
  • i just check with the debugger and collider != ( is false) and collider still gets the null pointer – James Hudson Feb 06 '13 at 20:32
  • and now i getting a error with the collider != null. the error is collider cannot be resolved to a variable – James Hudson Feb 06 '13 at 20:36
  • `collider` is declared directly in the line before the condition. If you use *eclipse*, try menu: *Project -> Clean* to force eclipse to build the new [eclipse sometimes get confused ;-) ]. – MrSmith42 Feb 06 '13 at 21:19
  • Thanks for the suggestion but it didn't work. collider != ( is false) – James Hudson Feb 06 '13 at 21:26
  • java.lang.NullPointerException at equals(Object) – James Hudson Feb 06 '13 at 21:26
  • `collider != ( is false) ` Do you mean `collider != null` is `false`? If yes, than the `equals` method is never called. I think you must show us your current code, I cannot find the error, without seeing the current source. – MrSmith42 Feb 06 '13 at 21:34
1

Like others said :

if(collider != null && collider.equals(paddle) ) {

should do the trick. so an expression of this form:

if (condition1 && condition2)

Condition2 will be evaluation only if condition1 is true. In this case, if condition 1 is null, condition2 will not be evaluated and this would prevent null pointer exception.

GJ13
  • 468
  • 3
  • 11
  • if i do that and then run the debugger to the ballspeed = -ballspeed line. i get the following errors in the debugger. – James Hudson Feb 06 '13 at 20:42
  • "collider" (pending) collider cannot be resolved to a variable "collider != null" (pending) collider cannot be resolved to a variable "collider.equals(paddle)" (pending) collider cannot be resolved "private void checkForHit" (pending) Syntax error, insert ";" to complete BlockStatements "GObject collide...lX, ballspeed);" (pending) – James Hudson Feb 06 '13 at 20:43
0

Try changing the order of your second if statement to

if(collider != null && collider.equals(paddle))

If statements are evaluated left to right, if collider is null, the first statement will evaluate to false and the second statement won't be executed. Alternatively you could do something like

if(collider == null) {
    //do nothing or attempt to get non null collider
}
else if(collider.equals(paddle))
{
   ballspeed = -ballspeed;
}
Robert Prior
  • 498
  • 4
  • 14
0

Try this

if(collider != null  &&  collider.equals(paddle) )  

This should work.

user1760178
  • 5,637
  • 5
  • 23
  • 54
  • hi i try that and nothing i used the debugger to find out whats going on and the collider.equals(paddle) is never true. – James Hudson Feb 06 '13 at 21:14