1

EDITED CODE:

public static void main(String[] args){

    JFrame frame = new JFrame();

    frame.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(new StartPanel());
    frame.add(new InstructionsPanel());
    frame.add(new GamePanel());

    frame.getContentPane().getComponent(1).setVisible(false);
    frame.getContentPane().getComponent(2).setVisible(false);

    frame.setPreferredSize(new Dimension(500, 500));
    frame.pack();
    frame.setVisible(true);

}

No matter what outside class I try to modify the frame from (any of the 3 panel classes above), I get a null pointer exception pointing to the line I am modifying something in the frame.

3
  • Why are you using static variables? First order of business: get rid of all static modifiers that you show above. Bring your code into the instance world. Commented May 12, 2013 at 1:18
  • You've changed the whole question which makes my current answer irrelevant. :( Not good. Commented May 12, 2013 at 2:06
  • Whoops, I apologize, I am new to this site :( Commented May 12, 2013 at 2:11

1 Answer 1

1

Your Panel classes are created before the JFrame is created so the JFrame will be null in the Panel class constructors. But as per my comment, you should bring your code into the instance world by getting rid of those static modifiers. Your whole program design, to put it mildly, smells. Your main method should instead look something like:

private static void createAndShowGui() {
  // Model model = new MyModel();
  View view = new View();
  // Control control = new MyControl(model, view);

  JFrame frame = new JFrame("My GUI");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.getContentPane().add(view.getMainComponent());
  frame.pack();
  frame.setLocationByPlatform(true);
  frame.setVisible(true);
}

public static void main(String[] args) {
  SwingUtilities.invokeLater(new Runnable() {
     public void run() {
        createAndShowGui();
     }
  });
}

And View could look like:

public class View
  private StartPanel s = new StartPanel();
  private InstructionsPanel i = new InstructionsPanel();
  private GamePanel g = new GamePanel();
  private JPanel mainComponent = new JPanel();

  public View() {
    // create your GUI here
    // add components to mainComponent...
  }

  public JComponent getMainComponent() {
    return mainComponent;
  }
}
Sign up to request clarification or add additional context in comments.

14 Comments

Ah thanks. Now I apologize if this is a stupid question, but I am a bit new to the static modifier and all, and just had it there because the compiler told me to (yes I know, never go with the compiler before knowing what you are doing). How can I change my program so my variables are not required to be static?
@user2373733: No, the compiler didn't tell you to use the static modifier. Instead it told you that you can't use a static whatever in that context. The solution is not to use static to but to create instances and to call methods on instances not on classes. Again, the first thing you need to do is get rid of the static modifiers, and then when the compiler complains, fix the problem rather than make the variable static.
So you mean like frame.add(new GamePanel())?
@user2373733: it's hard to say without knowing the structure of your program, but possibly, or frame.add(new GamePanel(frame)); where you pass the frame instance into GamePanel if need be.
Ahh disregard that, im stupid, I think I know how to fix this. Basically I am planning to make this class extend JFrame, then pretty much slap everything I have now into the constructor. Do you think that is a good idea?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.