Java JFrame代码给出空指针异常

时间:2016-02-17 17:21:48

标签: java swing jframe key-bindings

所以我遇到的问题与我的JFrame有关。首先,我将在此处发布我的代码,然后解释问题。

package evenMoreTesting;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class MyKeyBindings {

static int x1=0,x2=0,x3=0;
static JFrame frame;



public static void main(String[] args) {


    //JFrame frame = new JFrame();


    //some kind of problem when im calling JFrame out of the main class
    frame.setTitle("we still testing");
    frame.setSize(750, 500);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    Action a1 = new AbstractAction() {
        public void actionPerformed(ActionEvent AE){

            x1 +=1;


            Action a2 = new AbstractAction() {
                public void actionPerformed(ActionEvent AE){

                    x2 +=1;


                    Action a3 = new AbstractAction() {
                        public void actionPerformed(ActionEvent AE){

                            x3 +=1;

                            if(x3==1){
                                System.out.println("you wot");
                            }
                            if(x3==2){
                                System.out.println("m8");
                            }


                        }
                    };

                    if(x2==1){
                        System.out.println("dude");
                    }
                    if(x2==2){
                        System.out.println("stop");
                    }
                    if(x1==3 && x2==4){
                        System.out.println("woah you broke the goddamn system");
                    }

                    //this game will be sensitive to how many clicks have happened

                    //and it doesnt work unless i call the above code in this action
                    //the a3 action will be in here just like a2 is in a1

                    JPanel content = (JPanel) frame.getContentPane();

                    KeyStroke three = KeyStroke.getKeyStroke("3");

                    InputMap IM = content.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);


                    IM.put(three, "x3++");
                    content.getActionMap().put("x3++", a3);



                }
            };






            JPanel content = (JPanel) frame.getContentPane();

            KeyStroke two = KeyStroke.getKeyStroke("2");

            InputMap IM = content.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);

            IM.put(two, "x2++");
            content.getActionMap().put("x2++",a2);


            if(x1==1){
                System.out.println("ok");
            }
            if(x1==2){
                System.out.println("cool");
            }



        }
    };



    JPanel content = (JPanel) frame.getContentPane();

    KeyStroke one = KeyStroke.getKeyStroke("1");


    InputMap IM = content.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    IM.put(one, "x1++");
    content.getActionMap().put("x1++", a1);



}
}

所以问题是,当我在main方法之外调用JFrame时,我得到了这个错误:

线程“main”java.lang.NullPointerException中的异常     在evenMoreTesting.MyKeyBindings.main(MyKeyBindings.java:27)

第27行是:

frame.setTitle("we still testing");

实际上几天前这个问题不在这里,我不确定异常现在是怎么开始的。

我决定评论一下,看看这是否是唯一的问题。我做了,但仍然有一个错误,但这次是在第28行,所以我注释掉了第28行,然后错误发生在29,而其余的代码中出现了框架。

现在你可以说,只需将JFrame放在main方法中,但是有一个原因需要在main方法之外。

在我的操作中,我将调用JFrame将图像放在屏幕上。当JFrame在main方法中时,它被限制为该方法,据我所知。

所以我决定在main之外调用JFrame,以便操作也可以访问。这就是我将这些图像调用到屏幕上的方式,对于任何有兴趣的人,或者它是否有助于解决这个问题。

frame.add(new JLabel(new ImageIcon("frames/img01.jpg")));

框架是源文件夹,img01是我在屏幕上的第一个图像

如果有其他方法可以在屏幕上绘制图像,请告诉我们。

简而言之,我的问题是,为什么在主方法之外调用JFrame时会出现Null指针异常,如何防止它。

提前谢谢!

2 个答案:

答案 0 :(得分:0)

编辑: - 在这里,只需调用此方法showImage("c:/image.png");并将路径传递给您的图片,它将创建一个JFrame并从显示它的路径加载您的图像: -

// the imports required
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.*;

//paste this method in your class and call it when you want to display image

public void showImage(String path_to_image)
{
 JFrame frame = new JFrame("Image");
 frame.setLayout(new BorderLayout());
 frame.setResizable(false);
 ImageIcon myImage = new ImageIcon(path_to_image);
 frame.getContentPane().add(new JLabel(myImage)); 

 frame.pack();
 frame.setVisible(true);
}

OR

在你的主要添加frame = new JFrame();,因为你还没有创建它的新实例,因此当你将组件添加到框架的空值时,你会收到空指针异常

答案 1 :(得分:0)

如果您创建非基本类字段(引用)并且未初始化它,则默认情况下将使用null初始化它。

以下是其中一个

  1. 取消注释以下行

    //JFrame frame = new JFrame();

    并删除以下行

    static JFrame frame;

  2. OR

    1. 更新以下行

      static JFrame frame;

      static JFrame frame = new JFrame();

相关问题