Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
给出空指针异常的Java JFrame代码_Java_Swing_Jframe_Key Bindings - Fatal编程技术网

给出空指针异常的Java JFrame代码

给出空指针异常的Java JFrame代码,java,swing,jframe,key-bindings,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

因此,我的问题与我的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时会出现空指针异常,以及如何防止它

提前谢谢你

编辑:- 在这里,只需调用这个方法
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);
}


在主菜单中添加
frame=newjframe()由于您尚未创建新的安装,因此在向帧的空值添加组件时会收到空指针异常

如果您创建非基本类字段(参考)但未对其进行初始化,则默认情况下将使用
null
对其进行初始化

因此,请执行以下操作之一

  • 取消对下一行的注释

    //JFrame frame=new JFrame()

    并删除以下行

    静态JFrame

  • 更新以下行

    静态JFrame

    staticjframe=newjframe()


  • 你的
    静态JFrame
    null
    ,您在
    main
    方法中执行的第一个操作是
    frame.setTitle
    ,这意味着您正在调用
    null
    上的
    setTitle
    (而null没有此类方法,或者更精确地说,没有任何方法)。首先初始化
    。您可以将其声明为
    staticjframe=newjframe()或在主方法中调用
    frame=newjframe()。感谢您对其工作原理的解释!谢谢我很感激你回答了第二个问题!