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!