0

今天我有点轻微,看,我正在尝试运行我的代码,但我得到了 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,以防你需要它:http: //pastebin.com/hbdd7j84

编辑:这是堆栈跟踪,抱歉之前缺少它!

gui.GUIJuego 的 java.awt.Container.add(Unknown Source) 处的 java.awt.Container.addImpl(Unknown Source) 处的线程“AWT-EventQueue-0”java.lang.NullPointerException 中的异常。(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 (未知来源)在 javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) 在 javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) 在 javax.swing.DefaultButtonModel.setPressed(Unknown Source) 在 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 ) 在 java.awt.EventQueue$3 的 java.awt.EventQueue.access$200(Unknown Source)。在 java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 在 java.security.AccessController.doPrivileged(Native Method) 在 java.security.AccessController.doPrivileged(Native Method) 在 java.security.ProtectionDomain$1 运行(Unknown Source) .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。 java.awt 中的 pumpEventsForHierarchy(未知来源)。java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) 的 EventDispatchThread.pumpEvents(Unknown Source)

感谢阅读,顺便说一句!

4

1 回答 1

1

NPE 是从 GUIJuego.java 的第 113 行抛出的,堆栈跟踪表明:

panelDatosCiudad.add(arcaLabel);

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

于 2013-11-11T19:19:11.403 回答