I am having trouble updating a text area.
I declare textArea in gui.java:
JTextArea textArea;
I start up the GUI..
public void startGUI() {
// These are all essential GUI pieces
JLabel jLabInstruction, jLaberror;
JLabel copyright = new JLabel("");
JTextField uI = new JTextField("");
JTextArea textArea = new JTextArea("");
JButton jbtnSubmit;
final JFrame jfrm = new JFrame("app name!");
jfrm.setLayout(new FlowLayout());
jfrm.setSize(300, 300);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
textArea = new JTextArea(5, 20);
textArea.setEditable(false);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
jLabInstruction = new JLabel("SYSTEM: Please type in a command: ");
jbtnSubmit = new JButton("Submit");
jLaberror = new JLabel("");
textArea.setMargin(new Insets(10,10,10,10));
jfrm.add(jLaberror);
jfrm.add(textArea);
jfrm.add(jLabInstruction);
jfrm.add(uI);
jfrm.add(jbtnSubmit);
jfrm.add(new JSeparator(SwingConstants.HORIZONTAL));
jfrm.add(copyright);
jfrm.setVisible(true);
}
And I have a method that writes to the textArea above:
public void writeToTextArea(String userInputText) {
textArea.append("\nSYSTEM: "
+ userInputText);
}
Also, in tasks.java, I am able to call on this last method:
gui.writeToTextArea("PROGRAM STARTED!");
My problem is that the text area field is not updating. nothing is being inputted. I am thinking it is because it can’t find what textArea is. I am getting an:
Exception in thread "main" java.lang.NullPointerException
You are declaring another variable called
textAreain yourstartGUIfunction, which is hiding the class leveltextArea. Which is why you get an NPE when you try to write to the text area later on in your program.