GridWorld代码中间的NullPointerException [关闭]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GridWorld代码中间的NullPointerException [关闭]相关的知识,希望对你有一定的参考价值。

我正在使用GridWorld案例研究。我似乎在提供的程序中发现了一个错误。尝试通过GUI创建无效大小的网格并关闭错误消息后,每当我尝试对现有网格执行任何操作时,都会出现NullPointerException:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at info.gridworld.gui.GUIController$3.mousePressed(GUIController.java:119)
        at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:279)
        at java.awt.Component.processMouseEvent(Component.java:6501)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
        at java.awt.Component.processEvent(Component.java:6269)
        at java.awt.Container.processEvent(Container.java:2229)
        at java.awt.Component.dispatchEventImpl(Component.java:4860)
        at java.awt.Container.dispatchEventImpl(Container.java:2287)
        at java.awt.Component.dispatchEvent(Component.java:4686)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4489)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
        at java.awt.Container.dispatchEventImpl(Container.java:2273)
        at java.awt.Window.dispatchEventImpl(Window.java:2713)
        at java.awt.Component.dispatchEvent(Component.java:4686)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
        at java.awt.EventQueue.access$000(EventQueue.java:101)
        at java.awt.EventQueue$3.run(EventQueue.java:666)
        at java.awt.EventQueue$3.run(EventQueue.java:664)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
        at java.awt.EventQueue$4.run(EventQueue.java:680)
        at java.awt.EventQueue$4.run(EventQueue.java:678)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre ad.java:211)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread. java:128)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre   ad.java:117)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

此外,我无法通过提供的GUI将网格更改为其他内容:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at info.gridworld.gui.WorldFrame.setGrid(WorldFrame.java:237)
        at info.gridworld.gui.MenuMaker$GridConstructorItem.actionPerformed(MenuMaker.java:344)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
        at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
        at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
        at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
        at java.awt.Component.processMouseEvent(Component.java:6504)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
        at java.awt.Component.processEvent(Component.java:6269)
        at java.awt.Container.processEvent(Container.java:2229)
        at java.awt.Component.dispatchEventImpl(Component.java:4860)
        at java.awt.Container.dispatchEventImpl(Container.java:2287)
        at java.awt.Component.dispatchEvent(Component.java:4686)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
        at java.awt.Container.dispatchEventImpl(Container.java:2273)
        at java.awt.Window.dispatchEventImpl(Window.java:2713)
        at java.awt.Component.dispatchEvent(Component.java:4686)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
        at java.awt.EventQueue.access$000(EventQueue.java:101)
        at java.awt.EventQueue$3.run(EventQueue.java:666)
        at java.awt.EventQueue$3.run(EventQueue.java:664)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
        at java.awt.EventQueue$4.run(EventQueue.java:680)
        at java.awt.EventQueue$4.run(EventQueue.java:678)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

我认为这是GridWorld程序本身的一个错误,而不是我编写的任何代码中的错误,因为即使我刚刚启动程序而没有给它执行任何代码,这似乎也会发生。我做了一些调查,发现由于某种原因,每当我尝试更改GUI中的网格时,网格都会设置为null,而我尝试创建的网格无效(或者在创建网格时发生任何异常)。为什么网格设置为null?有没有办法可以在我自己提供的源代码中解决这个问题?

我正在使用的构建是GridWorld 1.02,可在GridWorld.info的“非官方代码”链接下获得。 CollegeBoard网站上的代码较旧。我测试了这个,它也适用于旧版本。

答案

这是发生了什么

NullPointer异常发生在源文件GUIController.java中,如stacktrace所示。

Grid<T> gr = parentFrame.getWorld().getGrid();
Location loc = display.locationForPoint(evt.getPoint());
if (loc != null && gr.isValid(loc) && !isRunning())

从qazxsw poi检索的qazxsw poi可以为null,if语句在调用gr之前不检查gr。

追踪来源:

Grid由WorldFrame.java中的getWorld().getGrid()方法设置,该方法不检查其输入,也就是说,如果使用gr.isValid()调用方法,则会将新网格设置为null。

从MenuMaker.java调用setGrid()方法(第343行)

null

并且setGrid()方法在异常时返回null。这是你的null来自的地方

固定

确保WorldFrame.java中的setGrid不接受null值作为参数。

Grid<T> newGrid = (Grid<T>) invokeConstructor(); 
parent.setGrid(newGrid);

一个可能更好但更复杂的解决方案是确保invokeConstructor()中的public void setGrid(Grid<T> newGrid) { if (newGrid == null) { return; } // rest of method goes here 不返回null。通知GUI并处理异常(而不是返回null)将是一个更优雅的解决方案。

希望这可以帮助。

另一答案

我看起来你需要在invokeConstructor()上调用MenuMaker.java时检查newGrid是否为null。我可以通过创建具有负行或列值的网格来重新创建您描述的问题。允许将网格设置为null似乎会导致NPE下游。

对源进行以下更改将修复它。关闭错误消息后,我能够创建一个有效大小的新网格,并使用其他GUI功能。

world frame.Java:

setGrid(Grid<T> newGrid)

以上是关于GridWorld代码中间的NullPointerException [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

Butterknife 片段旋转给出 NullPointer

获取 XMPPConnection 的 Nullpointer 异常

Kotlin NullPointer Exception Android [关闭]

Finder 功能不起作用,基本适配器调用 NullPointer [重复]

在 for 循环中初始化 Clip 后 Clip 上的 NullPointer

nullpointer遍历xml