开发者

java.lang.NullPointerException [duplicate]

This question already has answers here: What is a NullPointerException, and how do I fix it? (12 answers) Closed 7 years ago.

I keep getting this exception:

Exception in thread "Thread-1" java.lang.NullPointerException
        at Info.<init>(Info.java:168)
        at Rcon.data(Rcon.java:130)
        at Rcon$1.run(Rcon.java:105)

Line 168 is at bot.redScore = Integer.parseInt(matcher.group(2));

    else if(str.indexOf("current score") != -1) {
        pattern = "Team \\\"(\\w+)\\\" current score \\\"(\\d+)\\\"";
        p = Pattern.compile(pattern);
        matcher = p.matcher(str);
        if(matcher.find()) {
            if(matcher.group(1).equalsIgnoreCase("Red")) {
                bot.redScore = Integer.parseInt(matcher.group(2));

            }
            else if(matcher.group(1).equalsIgnoreCase("Blue")) {
                bot.blueScore = Integer.parseInt(matcher.group(2));
            }
            cmd = "score";
        }
    }

I have no clue开发者_运维技巧 why I keep getting this error. The str that is being parsed using regex is:

Processing: Team "Red" current score "1" with "1" players

When I am running only this part by itself, it works fine. But when I am running the whole program I get this exception.

bot.blueScore and bot.redScore are being declared in another class as follows:

int redScore = 0;
int blueScore = 0;

Also I have checked the contents of matcher.group(2), and it returns an integer, anywhere from 0 to 10. Any ideas? I have been struggling with this for hours now.

Thanks!


bot is likely to be null. Make sure it is not null (initialize it)

That said, you should learn to read exceptions - this is a very core skill. NullPointerException is the most common exception - you already identified the line, and if you check the documentation of the exception, you will see that it usually occurs when a reference is null and you are trying to access methods/fields on it.


If you get a null pointer exception look for expressions on the line of the form x.y and ask yourself why x is null. In your case either bot or matcher must be null. Since you got into that line using matcher on the previous line, your variable bot is null.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜