Java NullPointerException致命异常:线程-5023错误
我试图在android上创建一个xmpp/jabber客户端,我正在使用“对话”的开源代码,现在我遇到了以下错误 我尝试过搜索,但得到了不同的答案,我知道值不是空的,因为我在崩溃前将它打印到屏幕上,我对android/java场景还是很陌生,不熟悉线程错误 我读到的是,这可能是由于线程更新了ui,但找不到确切的问题 Logcat:Java NullPointerException致命异常:线程-5023错误,java,android,Java,Android,我试图在android上创建一个xmpp/jabber客户端,我正在使用“对话”的开源代码,现在我遇到了以下错误 我尝试过搜索,但得到了不同的答案,我知道值不是空的,因为我在崩溃前将它打印到屏幕上,我对android/java场景还是很陌生,不熟悉线程错误 我读到的是,这可能是由于线程更新了ui,但找不到确切的问题 Logcat: 06-24 16:13:27.287 9388-9415/com.test E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-
06-24 16:13:27.287 9388-9415/com.test E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-5023
Process: com.test, PID: 9388
java.lang.NullPointerException
at com.test.entities.MucOptions.processPacket(MucOptions.java:131)
at com.test.parser.PresenceParser.parseConferencePresence(PresenceParser.java:35)
at com.test.services.XmppConnectionService$5.onPresencePacketReceived(XmppConnectionService.java:275)
at com.test.xmpp.XmppConnection.processPresence(XmppConnection.java:414)
at com.test.xmpp.XmppConnection.processStream(XmppConnection.java:303)
at com.test.xmpp.XmppConnection.processStream(XmppConnection.java:244)
at com.test.xmpp.XmppConnection.switchOverToTls(XmppConnection.java:516)
at com.test.xmpp.XmppConnection.processStream(XmppConnection.java:236)
at com.test.xmpp.XmppConnection.connect(XmppConnection.java:175)
at com.test.xmpp.XmppConnection.run(XmppConnection.java:219)
at java.lang.Thread.run(Thread.java:841)
public void processPacket(PresencePacket packet, PgpEngine pgp) {
String[] fromParts = packet.getFrom().split("/");
if (fromParts.length>=2) {
String name = fromParts[1];
Log.i("MUC packet", packet.toString()+"");
String type = packet.getAttribute("type");
Element item;
if (type==null) {
User user = new User();
item = packet.findChild("x","http://jabber.org/protocol/muc#user").findChild("item");
user.setName(name);
user.setAffiliation(item.getAttribute("affiliation"));
user.setRole(item.getAttribute("role"));
user.setName(name);
if (name.equals(getNick())) {
this.isOnline = true;
this.error = 0;
self = user;
} else {
addUser(user);
}
if (pgp != null) {
Element x = packet.findChild("x",
"jabber:x:signed");
if (x != null) {
Element status = packet.findChild("status");
String msg;
if (status != null) {
msg = status.getContent();
} else {
msg = "";
}
user.setPgpKeyId(pgp.fetchKeyId(account,msg, x.getContent()));
}
}
} else if (type.equals("unavailable")) {
if (name.equals(getNick())) {
item = packet.findChild("x","http://jabber.org/protocol/muc#user").findChild("item");
String nick = item.getAttribute("nick");
if (nick!=null) {
aboutToRename = false;
if (renameListener!=null) {
renameListener.onRename(true);
}
this.setNick(nick);
}
}
deleteUser(packet.getAttribute("from").split("/")[1]);
} else if (type.equals("error")) {
Element error = packet.findChild("error");
if (error.hasChild("conflict")) {
if (aboutToRename) {
if (renameListener!=null) {
renameListener.onRename(false);
}
aboutToRename = false;
} else {
this.error = ERROR_NICK_IN_USE;
}
}
}
}
}
粘液选项:131
item = packet.findChild("x","http://jabber.org/protocol/muc#user").findChild("item");
完整的MucOptions.java:
06-24 16:13:27.287 9388-9415/com.test E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-5023
Process: com.test, PID: 9388
java.lang.NullPointerException
at com.test.entities.MucOptions.processPacket(MucOptions.java:131)
at com.test.parser.PresenceParser.parseConferencePresence(PresenceParser.java:35)
at com.test.services.XmppConnectionService$5.onPresencePacketReceived(XmppConnectionService.java:275)
at com.test.xmpp.XmppConnection.processPresence(XmppConnection.java:414)
at com.test.xmpp.XmppConnection.processStream(XmppConnection.java:303)
at com.test.xmpp.XmppConnection.processStream(XmppConnection.java:244)
at com.test.xmpp.XmppConnection.switchOverToTls(XmppConnection.java:516)
at com.test.xmpp.XmppConnection.processStream(XmppConnection.java:236)
at com.test.xmpp.XmppConnection.connect(XmppConnection.java:175)
at com.test.xmpp.XmppConnection.run(XmppConnection.java:219)
at java.lang.Thread.run(Thread.java:841)
public void processPacket(PresencePacket packet, PgpEngine pgp) {
String[] fromParts = packet.getFrom().split("/");
if (fromParts.length>=2) {
String name = fromParts[1];
Log.i("MUC packet", packet.toString()+"");
String type = packet.getAttribute("type");
Element item;
if (type==null) {
User user = new User();
item = packet.findChild("x","http://jabber.org/protocol/muc#user").findChild("item");
user.setName(name);
user.setAffiliation(item.getAttribute("affiliation"));
user.setRole(item.getAttribute("role"));
user.setName(name);
if (name.equals(getNick())) {
this.isOnline = true;
this.error = 0;
self = user;
} else {
addUser(user);
}
if (pgp != null) {
Element x = packet.findChild("x",
"jabber:x:signed");
if (x != null) {
Element status = packet.findChild("status");
String msg;
if (status != null) {
msg = status.getContent();
} else {
msg = "";
}
user.setPgpKeyId(pgp.fetchKeyId(account,msg, x.getContent()));
}
}
} else if (type.equals("unavailable")) {
if (name.equals(getNick())) {
item = packet.findChild("x","http://jabber.org/protocol/muc#user").findChild("item");
String nick = item.getAttribute("nick");
if (nick!=null) {
aboutToRename = false;
if (renameListener!=null) {
renameListener.onRename(true);
}
this.setNick(nick);
}
}
deleteUser(packet.getAttribute("from").split("/")[1]);
} else if (type.equals("error")) {
Element error = packet.findChild("error");
if (error.hasChild("conflict")) {
if (aboutToRename) {
if (renameListener!=null) {
renameListener.onRename(false);
}
aboutToRename = false;
} else {
this.error = ERROR_NICK_IN_USE;
}
}
}
}
}
似乎
muoptions
变量是null
。如果item=packet.findChild(“x”),请在对其调用方法之前对其进行初始化http://jabber.org/protocol/muc#userfindChild(“项目”)
是您的第131行,然后是packet
或packet.findChild(“x”http://jabber.org/protocol/muc#user“
为null
请使用以下内容进行检查:
if (packet == null) {
throw new NullPointerException("packet is null")
} else if (packet.findChild("x","http://jabber.org/protocol/muc#user") == null) {
throw new NullPointerException("packet.findChild(\"x\",\"http://jabber.org/protocol/muc#user\") is null")
}
item = packet.findChild("x","http://jabber.org/protocol/muc#user").findChild("item");
检查第131行,确保该行上的所有变量均为非空。到目前为止,其中一个(或多个)为空。非常感谢,似乎是由于muc#user不在字符串中而导致的子项为空,我现在已修复该问题,并学会了一种新的疑难解答方法。发生错误时,链式调用可能会令人困惑。:)