user1484136
user1484136

Reputation:

Java method returning null pointer?

So I have a method that is checked when a user runs a command, well I get getting this wretched error that it's returning a NullPointer...

Heres my method,

   public boolean openReferal(String name) {
    for(String s : m.refferalSessions) {
        if(s.equalsIgnoreCase(name)) {
            return true;
        }
    }
    return false;
}

And heres how i'm implementing it...

   if(check.openReferal(sender.getName() + ":" + args[0])) {
            sender.sendMessage(openReferal);
            return true;
        }

Upvotes: 0

Views: 2588

Answers (3)

FThompson
FThompson

Reputation: 28687

Methods do not, in practice, return NullPointerExceptions; rather, they throw them. However, methods can return null.

However, boolean methods cannot return null, as primitives cannot be null. The NullPointerException here could be the result of sender or check being null when you invoke their corresponding methods on them.

Upvotes: 0

HJW
HJW

Reputation: 23443

s could possibly be null.

Can you do a debug to see the value of s.

Upvotes: 0

Cameron Skinner
Cameron Skinner

Reputation: 54326

A NullPointerException means that you are dereferencing a null variable somewhere. The stack trace will tell you exactly which line is causing the problem, and from that you can deduce which variable is set to null.

From your code, there are only a few suspects: m, m.referralSessions, check and sender. Check that they are all initialized properly, and use the information in the exception to help track down the problem.

Upvotes: 1

Related Questions