gpt4 book ai didi

java - 更换JPanels时遇到错误

转载 作者:行者123 更新时间:2023-12-04 06:13:16 26 4
gpt4 key购买 nike

我正在创建一个菜单,其中包含多个面板,这些面板在用户通过单击按钮导航时被删除/添加。

在尝试了各种事情之后,我找到了一个对我来说最有意义但给我错误的方法。

我产生错误的“解决方案”:

public void actionPerformed (ActionEvent evt) {
Object source = evt.getSource();
if (source == jButton1) {
changePanels(jPanel1, singlePanel1);
}

}

public void changePanels (JPanel a, JPanel b) {
getContentPane().removeAll();
getContentPane().add(b);
validate();
repaint();
}

出于某种原因,它会产生以下错误:
$Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.awt.Container.addImpl(Container.java:1041)
at java.awt.Container.add(Container.java:365)
at phantasma.OriginalFrame.changePanels(OriginalFrame.java:156)
at phantasma.OriginalFrame.actionPerformed(OriginalFrame.java:149)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6288)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6053)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4651)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:616)
at java.awt.EventQueue$2.run(EventQueue.java:614)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

我已经经历了各种故障排除,但我不确定问题是什么。非常感谢您的帮助,谢谢。

最佳答案

NullPointerException 是最容易诊断的问题之一,因为它会告诉您源文件及其发生的行号。

在你的情况下,它是

phantasma.OriginalFrame.changePanels(OriginalFrame.java:156)

打开您的 IDE,转到该行,并查找取消引用的对象引用。其中之一为空;你以为不是。

如果这还不够,设置一个断点并导航到调试器中的那个位置。它会准确地告诉你什么是空值。

一旦你弄清楚了,想想为什么那个对象没有正确初始化并修复它。

关于java - 更换JPanels时遇到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7540296/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com