I’m having a nullpointer error and don’t know how to fix

Asked

Viewed 58 times

2

The code I’m using is this:

public ForumGroups getMainGroup() {
        if (Constants.SQL_ENABLED) {
            int lowest = -1;
            ForumGroups g = null;
            for (ForumGroup group : forumGroups) {
                if (group.getGroup().ordinal() < lowest || g == null) {
                    lowest = group.getGroup().ordinal();
                    g = group.getGroup();
                }
            }
            if (g == null)
                return ForumGroups.MEMBER;
            else
                return g;
        }
        return ForumGroups.OWNER;
    }

And I’m getting error on this line

if (group.getGroup().ordinal() < lowest || g == null) {

What I do?

mistake here:

java.lang.NullPointerException
    at ophion.rs.game.player.Player.getMainGroup(Player.java:4118)
    at ophion.rs.game.player.Player.InterfaceManager$3.run(InterfaceManager.java:472)
    at ophion.rs.game.tasks.WorldTasksManager.processTasks(WorldTasksManager.java:18)

inserir a descrição da imagem aqui

  • 2

    post the error stacktrace to make it easier to help...

  • What object is the group? Without other relevant parts it is difficult to help.

  • 2

    not so... post in your question the stacktrace

  • I’m new at this kk sorry

  • Run a debug there probably the group.getGroup(). ordinal() method is returning a NULL. Debugging and checking the group object, you may be able to detect the error.

  • I’ve been around but I can’t detect you, what I do?

Show 1 more comment

2 answers

2

You need to dismember that if in pieces, ie:

if( (group.getGroup() != null && group.getGroup().ordinal() < lowest) || g == null )

What is probably occurring is that getGroup() is returning null (Demeter’s law).

1


To NullPointerException happens when you try to access a member(variable or method) of a object that has the null reference.

In your code if (group.getGroup().ordinal() < lowest || g == null) {, 2 objects can be null: group or group.getGroup().

Thus, the exception occurs or when it tries to execute the method group.getGroup() or group.getGroup().ordinal().

Therefore, you need to check if these values are null before checking the method group.getGroup().ordinal(), being like this:

if( (null != group && null != group.getGroup() && group.getGroup.ordinal() < lowest) || g == null)

Note that the if has become too extensive and this can be difficult to understand. So you can create a method for null checks:

private boolean isGroupNull(Group g){
  return null != g && null != g.getGroup();
}

and use this in your previous if:

if ( (!isGroupNull(group) && group.getGroup.ordinal() < lowest) || g == null)

Note that we had to put an extra parenthesis, because previously we checked A || B.. now we’re checking out (C && A) || B

UPDATE:

Inside the if you try to make 2 assignments that depend on the same check we did to see if the objects are null. If they are null will give error again, as you reported.

So let’s change the logic of if:

for (ForumGroup group : forumGroups) {
  if(!isGroupNull(group)){
    if (group.getGroup().ordinal() < lowest || g == null) {
      lowest = group.getGroup().ordinal();
      g = group.getGroup();
    }
  }
}

So you "enjoy" the check we made for the 2 situations.

  • I did so, now the error has made this line: lowest = group.getGroup(). ordinal(); EDIT: Next I commented on this line and the code ran perfectly! Thank you guys so much you’re amazing!

  • I’ll edit the answer

  • 2

    It worked perfectly Peter, thank you very much for the full explanation and the functioning of what I needed. How do you give people points ? :)

  • 1

    Click the up arrow next to my answer... And, if you have permission already, click the little checkbox below to indicate the correct answer. Enjoy and make a tour through the site to learn these things.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.