Java出错

时间:2013-11-21 15:04:56

标签: java

需要一些帮助。我总是得到这个错误:

ERROR! THREAD NAME: New I/O  worker #2
java.lang.NullPointerException
    at com.rs.game.player.content.DungeoneeringShop.open(DungeoneeringShop.java:12)
    at com.rs.game.player.dialogues.DungTutor.run(DungTutor.java:36)
    at com.rs.game.player.DialogueManager.continueDialogue(DialogueManager.java:31)
    at com.rs.net.decoders.WorldPacketsDecoder.processPackets(WorldPacketsDecoder.java:1130)
    at com.rs.net.decoders.WorldPacketsDecoder.decode(WorldPacketsDecoder.java:268)
    at com.rs.net.ServerChannelHandler.messageReceived(ServerChannelHandler.java:98)
    at org.jboss.netty.channel.SimpleChannelHandler.handleUpstream(SimpleChannelHandler.java:95)
    at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:563)
    at org.jboss.netty.channel.DefaultChannelPipeline.sendUpstream(DefaultChannelPipeline.java:558)
    at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:268)
    at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:255)
    at org.jboss.netty.channel.socket.nio.NioWorker.read(NioWorker.java:91)
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.processSelectedKeys(AbstractNioWorker.java:373)
    at org.jboss.netty.channel.socket.nio.AbstractNioWorker.run(AbstractNioWorker.java:247)
    at org.jboss.netty.channel.socket.nio.NioWorker.run(NioWorker.java:35)
    at org.jboss.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:102)
    at org.jboss.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

这是我的班级:

package com.rs.game.player.content;

import com.rs.game.player.Player;

public class DungeoneeringShop {

    private static final int INTERFACE_ID = 72;
    public static Player player;

    public static void open() {
        for(int i = 69; i < 73; i++) 
            player.getPackets().sendHideIComponent(INTERFACE_ID, i, false);
        player.getInterfaceManager().sendInterface(INTERFACE_ID);
    }
}

4 个答案:

答案 0 :(得分:4)

您尚未宣传player。所以它是null

就像

null.getPackets()  --------------> NullPointerException

因此,在使用

之前,分辨率会初始化
public static Player player = new Player(); // at declaration level

player = new Player(); // or later some where before using.

答案 1 :(得分:3)

您必须实例化变量player。在您的示例中,playernull 例如,如果您有一个无参数构造函数:

public static Player player = new Player();

答案 2 :(得分:1)

您需要实例化

public static Player player;

访问之前。

答案 3 :(得分:1)

因为你还没有初始化玩家对象。它应该是:

package com.rs.game.player.content;

import com.rs.game.player.Player;

public class DungeoneeringShop {

private static final int INTERFACE_ID = 72;
public static Player player;

public static void open() {
    player = new Player(); // or some other constructor of this class
    for(int i = 69; i < 73; i++) 
        player.getPackets().sendHideIComponent(INTERFACE_ID, i, false);
        player.getInterfaceManager().sendInterface(INTERFACE_ID);
    }
}

我认为你是Java World的新人。而你正在学习游戏开发电子书。在开始之前,你应该学习更多基础Java。

希望这有帮助:)