Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue with JTextArea in Swing
    primarykey
    data
    text
    <p>I am having trouble updating a text area.</p> <p>I declare <code>textArea</code> in <code>gui.java</code>:</p> <pre><code>JTextArea textArea; </code></pre> <p>I start up the GUI..</p> <pre><code>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); } </code></pre> <p>And I have a method that writes to the <code>textArea</code> above:</p> <pre><code>public void writeToTextArea(String userInputText) { textArea.append("\nSYSTEM: " + userInputText); } </code></pre> <p>Also, in <code>tasks.java</code>, I am able to call on this last method:</p> <pre><code>gui.writeToTextArea("PROGRAM STARTED!"); </code></pre> <p>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 <code>textArea</code> is. I am getting an:</p> <pre><code>Exception in thread "main" java.lang.NullPointerException </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload