0

I am working on a game that uses a custom console-style GUI to play. I previously asked a question to find out what was causing a NullPointerException. See this link for context.

I now have the error eliminated, but I have a new issue: When I start the game, the GUI only loads the JFrame (EDIT: JPanel not JFrame) but none of the other components. In the original post, the GUI components loaded up normally, but starting the game itself would cause a NullPointerException due to a call to the JTextArea before the GUI dispatch thread was complete.

Here is the current code:

Classic.java

import javax.swing.*;
import java.util.*;
import static javax.swing.SwingUtilities.invokeLater;
public class Classic extends Game{
    private static JFrame gui;
    private static GUIClassic newContentPane;
    ...
    public void play() {
        invokeLater(Classic::startGUI);
        invokeLater(Classic::startGame);
    }
    public static void startGame() {
        //Game processes
    ...
    }
    private static void startGUI() {
        gui = new JFrame("Karma :: Classic Mode");
        gui.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        newContentPane = new GUIClassic();
        newContentPane.setOpaque(true);
        gui.setContentPane(newContentPane);
        gui.pack();
        gui.setVisible(true);
    }
    ...
}

GUIClassic.java

...
public class GUIClassic extends JPanel implements ActionListener {
    private JTextArea output;
    private JTextField input;
    private boolean inputReady;
    private String inputText;
    public GUIClassic() {
        super();
        setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
        output = new JTextArea(15, 15);
        output.setEditable(false);
        JScrollPane outputScroll = new JScrollPane(output);
        input = new JTextField("",40);
        add(outputScroll);
        add(Box.createRigidArea(new Dimension(0,5)));
        add(input);
    }
}

In the play() method of Classic.java, I tried removing the invokeLater(...) methods, that came up with the same result.

public void play() {
    startGUI();
    startGame();
}

I also tried moving the call to startGame within startGUI:

public static void startGUI() {
    ...
    startGame();
}

However, removing startGame altogether allows it to start up normally:

public void play() {
    invokeLater(Classic::startGUI);
}

I am completely at a loss. I don't understand why loading the GUI without playing the game loads it fine, but beginning the game suddenly makes the components disappear.

Please note: there are no exceptions thrown during runtime.

As a side note, the GUI also does not allow me to close it via the [X] button in the current version, but in the cases where only 'startGUI' is called, the components pop up and the [X] allows you to exit.

Community
  • 1
  • 1
greatmastermario
  • 69
  • 1
  • 1
  • 9
  • 2
    For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Aug 26 '15 at 15:20
  • You don't show us the content of startGame(), so we can't tell what you are doing in that method that will cause the problem. – FredK Aug 26 '15 at 16:16

2 Answers2

1

Try to invoke the startGame() in a new Thread, because all of the painting stuff is done in the main-thread like that

  new Thread(new Runnable() {

     @Override
     public void run() {
        Classic.startGame();
     }
  }).start();
David
  • 1,006
  • 1
  • 11
  • 26
0

You never set the size of your Panel. Try to use the setPreferredSize(Dimension)-method of your GUIClassic before you call the pack()-method of your JFrame.

David
  • 1,006
  • 1
  • 11
  • 26
  • All it does is change the size. It does not make the components show up. I just tried it. However, it does show that it's a problem with the JPanel, so I will amend that in the post. – greatmastermario Aug 26 '15 at 15:42
  • Also, the GUI components load when `startGame` is not called. If that method is called, then the components don't load. – greatmastermario Aug 26 '15 at 15:47