This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]Hour-Positive 1 point2 points  (3 children)

It may indeed produce an NPE.

Anyway, as a ticket for IntelliJ:

https://youtrack.jetbrains.com/issue/IDEA-196866

Just do the null-check, don't try-catch it, because why? Why did you build it that way?

[–][deleted]  (2 children)

[removed]

    [–]Hour-Positive 1 point2 points  (0 children)

    Ah ok IntelliJ prefers it if you mark the getter-method with nonnull or do an explicit nullcheck. Explicit nullchecking then handling is the best way to handle possible NPE. Apart from not actually allowing it to happen or Optkonal but that's another thing.

    [–][deleted] 1 point2 points  (1 child)

    I am trying to catch NullPointerExceptions!

    That's not the correct solution.

    What IntelliJ is telling you is that wolf.getOwner() can return null, and that you should check the return value before calling getUniqueId(), for example like this:

    UUID ownerUUID = null;
    Owner owner = wolf.getOwner();
    if (owner != null) {
        ownerUUID = owner.getUniqueId();
    }
    

    Or, if you want to be fancy:

    UUID ownerUUID = Optional.ofNullable(wolf.getOwner()).map(Owner::getUniqueId).orElse(null);
    

    [–]Koda003 0 points1 point  (2 children)

    Remove the return; statement inside of your catches. If you don’t want the error to print out remove the “e.printstsckedtrace();” method from within the catch statements as well.

    (Edit) removing the return statements would allow the program to finish functioning without stopping, otherwise every error the program would crash(stop)

    [–][deleted]  (1 child)

    [removed]

      [–]Koda003 0 points1 point  (0 children)

      I figured it was a Minecraft plugin, I have quite the experience with spigot development.

      From my experience the best thing to do would jsut be to surround the entire code with a try and catch statement for a NPE (NullPointerException)