gpt4 book ai didi

java - 打开新框架时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 13:51:34 25 4
gpt4 key购买 nike

今天我遇到了一点小问题,看,我正在尝试运行我的代码,但遇到了 NullPointerException。异常中的线索引导我到这里的这个函数:

private void irGuiJuego(JFrame frame){
SwingConsole.run(new GUIJuego(), 800, 600, true);
frame.dispose();
}

SwingConsole 中包含此代码的位置:

package utiles;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class SwingConsole {

public static void run(final JFrame frame, final int width, final int height, final boolean exitOnClose) {

SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {

if (exitOnClose)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(width, height);
//frame.setResizable(false);
frame.setVisible(true);

}
});
}

public static void run(final JFrame frame, final int width, final int height, final boolean exitOnClose, final String title) {

SwingUtilities.invokeLater(new Runnable() {

@Override
public void run() {

if (exitOnClose)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setTitle(title);
frame.setSize(width, height);
frame.setVisible(true);

}
});
}


}

考虑到我使用相同的方法打开另一个框架,这有点奇怪,具体来说是在这个函数中:

private void volverMenuInicio(JFrame frame){
SwingConsole.run(new MenuInicio(), 300, 150, true);
frame.dispose();
}

我会给你们留下一个 GUIJuego 框架的粘贴箱,因为将其发布在这里有点过分:http://pastebin.com/LSXbc7KE ,还有另一个框架的pastebin,以防万一你需要它:http://pastebin.com/hbdd7j84

编辑:这是堆栈跟踪,很抱歉之前没有它!

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at java.awt.Container.addImpl(Unknown Source) at java.awt.Container.add(Unknown Source) at gui.GUIJuego.(GUIJuego.java:113) at gui.MenuNuevoJuego.irGuiJuego(MenuNuevoJuego.java:95) at gui.MenuNuevoJuego.access$2(MenuNuevoJuego.java:94) at gui.MenuNuevoJuego$2.actionPerformed(MenuNuevoJuego.java:74) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

顺便说一下,感谢您的阅读!

最佳答案

NPE 是从 GUIJuego.java 中的第 113 行抛出的,如堆栈跟踪所示:

panelDatosCiudad.add(arcaLabel);

发生这种情况是因为第 35 行中定义的 arcaLabel 从未设置为值,因此您向容器添加了一个 null JLabel。

关于java - 打开新框架时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19913695/

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