0

So the problem I am having has to do with my JFrame. First I am going to post my code here and then explain the problem.

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);



}
}

So the problem is that when I call the JFrame outside the main method I get this error:

Exception in thread "main" java.lang.NullPointerException at evenMoreTesting.MyKeyBindings.main(MyKeyBindings.java:27)

Line 27 is this:

frame.setTitle("we still testing");

And actually a few days ago this problem was not here, I'm not sure how the exception just started now.

I decided to comment it out to see if that was the only problem. I did and there was still an error, but this time on line 28, so I commented out line 28 and then the error was on 29 and that happened for the rest of the code with frame in it.

Now you may say, just put the JFrame in the main method, but there is a reason it needs to be outside the main method.

Inside my actions I am going to be calling the JFrame to put images on the screen. When JFrame is in the main method, it is restricted to that method,from what I know that is.

So I decided to call the JFrame outside the main so it will be accessible to the actions as well. And this is how I will be calling the images onto the screen, for anyone interested or if it will help solve this problem.

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

Frames being the source folder and img01 being my first image on the screen

If there is another way of approaching drawing images onto the screen, please do tell.

In short my question is, why am I getting a Null Pointer Exception when calling the JFrame outside the main method, and how can I prevent it.

Thank you in advance!

cei-ge
  • 3
  • 4
  • Your `static JFrame frame;` is by default `null` and first action you do in `main` method is `frame.setTitle` which means you are invoking `setTitle` on `null` (and null doesn't have such method, or any method to be more precise). Initialize `frame` first. You can declare it as `static JFrame frame = new JFrame();` or invoke in your main method `frame = new JFrame();`. – Pshemo Feb 17 '16 at 17:28
  • Thanks for the explanation of how it works! – cei-ge Feb 19 '16 at 16:25

2 Answers2

0

Edit :- Here, just call this method showImage("c:/image.png"); and pass the path to your image and it will create a JFrame and load your image from the path adn display it to you :-

// 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

In your main add frame = new JFrame(); as you have not created a new instace of it yet, hence you receive the null pointer exception as you are adding components to a null value of the frame

0

If you create a non-primitive class field (reference) and do not initialize it, then by default it will be initialized with null.

So do one of the below

  1. Uncomment following line

    //JFrame frame = new JFrame();

    and remove following line

    static JFrame frame;

OR

  1. Update following line

    static JFrame frame;

    to

    static JFrame frame = new JFrame();

Pshemo
  • 113,402
  • 22
  • 170
  • 242