写入Robocode中的文件时出现NullPointerException

时间:2013-11-20 21:22:38

标签: file file-io nullpointerexception robocode

尝试写入文件时出现NullPointerException:

        RobocodeFileWriter fileWriter=new RobocodeFileWriter("test.txt");
        for (int i = 0; i < state.countstates; i++)
            for (int j = 0; j < state.countactions; j++)
                fileWriter.write((int) table[i][j]);
        }

例外:

java.lang.NullPointerException
    at net.sf.robocode.host.security.ThreadManager.createRobotFileStream(ThreadManager.java:136)
    at robocode.RobocodeFileOutputStream.<init>(RobocodeFileOutputStream.java:88)
    at robocode.RobocodeFileOutputStream.<init>(RobocodeFileOutputStream.java:69)
    at robocode.RobocodeFileWriter.<init>(RobocodeFileWriter.java:69)
    at QTable.saveData(QTable.java:79)
    at MyFirstRobot.saveToFile(MyFirstRobot.java:471)
    at MyFirstRobot.onDeath(MyFirstRobot.java:414)
    at robocode.DeathEvent.dispatch(DeathEvent.java:63)
    at robocode.Event$HiddenEventHelper.dispatch(Event.java:259)
    at net.sf.robocode.security.HiddenAccess.dispatch(HiddenAccess.java:191)
    at net.sf.robocode.host.events.EventManager.dispatch(EventManager.java:422)
    at net.sf.robocode.host.events.EventManager.processEvents(EventManager.java:376)
    at net.sf.robocode.host.proxies.BasicRobotProxy.executeImpl(BasicRobotProxy.java:412)
    at net.sf.robocode.host.proxies.BasicRobotProxy.execute(BasicRobotProxy.java:121)
    at robocode.AdvancedRobot.execute(AdvancedRobot.java:549)
    at MyFirstRobot.run(MyFirstRobot.java:68)
    at net.sf.robocode.host.proxies.HostingRobotProxy.callUserCode(HostingRobotProxy.java:274)
    at net.sf.robocode.host.proxies.HostingRobotProxy.run(HostingRobotProxy.java:221)
    at java.lang.Thread.run(Thread.java:695)

即使我通过传递-DNOSECURITY = true来禁用安全管理器,也会发生此异常 我的robocode版本是1.8.3.0并在Mac上进行开发。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,我在开始时添加了包,它确实有效。你刚开始添加包裹了吗?您可以创建一个包来将所有代码放在该包中,然后您需要在代码中的每个类中包含该包,它应该可以正常工作。