NullPointerException FATAL EXCEPTION:Thread-5023错误

时间:2014-06-24 14:30:31

标签: java android

我正在尝试在android上创建一个xmpp / jabber客户端,我正在使用#34;对话的开源代码"现在我遇到了以下错误。

我已经尝试过搜索,但得到了不同的答案,我知道因为我在崩溃之前将它打印到屏幕上,因此值不为空,我在android / java场景中仍然很新并且不熟悉与线程错误。

我所看到的是,它可能是由于线程更新了ui但无法找到确切的问题。

logcat的:

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)

MucOptions:131

item = packet.findChild("x","http://jabber.org/protocol/muc#user").findChild("item");

完成MucOptions.java:

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;
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

似乎MucOptions变量是null。请在调用方法之前初始化它

答案 1 :(得分:1)

如果您的第131行为item = packet.findChild("x","http://jabber.org/protocol/muc#user").findChild("item");,则packetpacket.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");