今天我有一点点,看,我正在尝试运行我的代码,我得到一个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 Frame的pastebin,因为在这里发布它有点过分:http://pastebin.com/LSXbc7KE,也有另一帧的pastebin,以防你需要它:{{3 }}
编辑:这是堆栈跟踪,对不起以前没有它!
线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException 在java.awt.Container.addImpl(未知来源)at java.awt.Container.add(未知来源)at gui.GUIJuego。(GUIJuego.java:113)at gui.MenuNuevoJuego.irGuiJuego(MenuNuevoJuego.java:95)at at gui.MenuNuevoJuego.access 2美元(MenuNuevoJuego.java:94)at gui.MenuNuevoJuego $ 2.actionPerformed(MenuNuevoJuego.java:74)at javax.swing.AbstractButton.fireActionPerformed(未知来源)at javax.swing.AbstractButton $ Handler.actionPerformed(Unknown Source)at javax.swing.DefaultButtonModel.fireActionPerformed(未知来源)at javax.swing.DefaultButtonModel.setPressed(未知来源)at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知 来自)java.awt.Component.processMouseEvent(未知来源)at javax.swing.JComponent.processMouseEvent(未知来源)at java.awt.Component.processEvent(未知来源)at java.awt.Container.processEvent(未知来源)at java.awt.Component.dispatchEventImpl(未知来源)at java.awt.Container.dispatchEventImpl(未知来源)at java.awt.Component.dispatchEvent(未知来源)at java.awt.LightweightDispatcher.retargetMouseEvent(未知来源)at java.awt.LightweightDispatcher.processMouseEvent(未知来源)at java.awt.LightweightDispatcher.dispatchEvent(未知来源)at java.awt.Container.dispatchEventImpl(未知来源)at java.awt.Window.dispatchEventImpl(未知来源)at java.awt.Component.dispatchEvent(未知来源)at java.awt.EventQueue.dispatchEventImpl(未知来源)at java.awt.EventQueue.access $ 200(未知来源)at java.awt.EventQueue $ 3.run(未知来源)at java.awt.EventQueue $ 3.run(未知来源)at java.security.AccessController.doPrivileged(Native Method)at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知 来源)at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知 来自)java.awt.EventQueue $ 4.run(未知来源)at java.awt.EventQueue $ 4.run(未知来源)at java.security.AccessController.doPrivileged(Native Method)at java.security.ProtectionDomain $ 1.doIntersectionPrivilege(未知 来自)java.awt.EventQueue.dispatchEvent(未知来源)at java.awt.EventDispatchThread.pumpOneEventForFilters(未知来源) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 在java.awt.EventDispatchThread.pumpEventsForHierarchy(未知 来自java.awt.EventDispatchThread.pumpEvents(未知来源) at java.awt.EventDispatchThread.pumpEvents(Unknown Source)at java.awt.EventDispatchThread.run(未知来源)
顺便感谢阅读!
答案 0 :(得分:1)
NPE从GUIJuego.java的第113行抛出,因为堆栈跟踪指示:
panelDatosCiudad.add(arcaLabel);
这种情况正在发生,因为第35行中定义的arcaLabel
永远不会设置为值,因此您将空JLabel添加到容器中。