简体   繁体   English

看似好的代码获得NullPointerException?

[英]Getting a NullPointerException on seemingly good code?

I'm having an issue where I have a player object that I'm trying to send to a method, and it doesn't work. 我有一个问题,我有一个玩家对象,我试图发送到一个方法,它不起作用。 As far as I can tell, however, it should. 然而,据我所知,它应该。 Any ideas? 有任何想法吗?

The line on which I find the error: 我发现错误的行:

keybl.playerUpdate(player);

Wherein "keybl" is a KeyListener that is initialized and is functioning in code before this runs, "playerUpdate" is a method seen below, and "player" is a Player object which is, in fact, initialized properly. 其中“keybl”是一个KeyListener,它初始化并在运行之前在代码中运行,“playerUpdate”是下面看到的方法,“player”是一个Player对象,实际上是正确初始化的。

public void playerUpdate(Player somePlayer) {

    user = somePlayer;

}

Wherein "user" is a class variable of type "Player" that is set to a different player object when this is run. 其中“user”是“Player”类型的类变量,在运行时设置为不同的播放器对象。

Any ideas? 有任何想法吗?

If the exception is on this line: 如果异常在这一行:

keybl.playerUpdate(player);

then it must be that keybl is null. 那么它必须是keybl为null。 Add one System.out.println("keybl: " + keybl); 添加一个System.out.println("keybl: " + keybl); before this line and you should see it printing null at some point (or to be precise - before the exception is thrown). 在这一行之前你应该看到它在某个时刻打印为null (或者确切地说 - 在抛出异常之前)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM