-3

I have this if statement:

if (!this.updateTime.equals(ProductionBlocking
          .get(this.keyReader.getUpdateTime(this.config.configUuid.toString(),
              KeyType.PLUSSTAR)))) {
...
}

Sometimes the below is null so I get a NullPointerError.

ProductionBlocking
          .get(this.keyReader.getUpdateTime(this.config.configUuid.toString(),
              KeyType.PLUSSTAR))`

Is there any way I can let the program run on even if there is a null pointer? Basically in this case I'd like the if to determine they arent equal.

runnerpaul
  • 5,942
  • 8
  • 49
  • 118
  • 1
    Please post the stacktrace you get and which exacly field is null. Also, I think what you're getting is a `NullPointerException`, not some mysterious `NullPointerError`. – Dmitriy Popov Jan 31 '20 at 14:28
  • 1
    `ProductionBlocking.get(...)` being null wouldn't raise a NullPointerException. `this.updateTime`, `this.keyReader`, `this.config` or `this.config.configUuid` being null would – Aaron Jan 31 '20 at 14:28
  • 1
    What exact part is `null`? Handle that specific part differently. – f1sh Jan 31 '20 at 14:29
  • If the code you posted would be null, then the equals would evaluate to `false`, because `anything.equals(null)` is always supposed to be `false`, as described [here](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#equals-java.lang.Object-). I highly suspect that sth else in this statement is `null`, please go over your code with a debugger and update the question. – Nightara Jan 31 '20 at 14:31
  • 1
    Rather than finding a way to ignore the NPE, you should figure out what is causing the NPE and fix it. Something is null, and you need to identify what. – khelwood Jan 31 '20 at 14:31
  • @Nightara you don't know what `updateTime` is and how it's class might override `equals`. But it *shouldn't* throw an NPE. – f1sh Jan 31 '20 at 14:32
  • 1
    See [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – khelwood Jan 31 '20 at 14:32
  • 1
    @f1sh Edited my comment, realized that myself right after typing it, but thanks for pointing it out. – Nightara Jan 31 '20 at 14:33

1 Answers1

0

You can catch the NullPointerException and then do nothing with it... although this is generally not recommended. You should try to handle this exception in the catch block and try to figure out why you are getting a npe and fix that.

try {
    // your code that throws the npe
} catch (NullPointerException e) {
    // catch it and do nothing (or handle it!)
}
// ... the rest of your code
rhowell
  • 1,165
  • 8
  • 21
  • 2
    The code should be refactored in a way so that the `null` case is handled and caught before it throws any exceptions. – f1sh Jan 31 '20 at 14:30
  • @f1sh that's true, but that doesn't sound like what the poster is asking. They're asking how to let the program to continue running after the npe is thrown. I agree though, they should take care of the npe rather than ignore it. – rhowell Jan 31 '20 at 14:31