1

I am trying to retrieve the information from a TextArea which has been defined as follows

JTextArea a1 = new JTextArea(15, 50);
    a1.setEditable(true);

    centerPanel.add(a1);
    a1.setVisible(true);

and in my listener class

    private class JobHandler implements ActionListener {

    boolean c2shortHand = false;
    boolean c1translation = false;
    boolean c3onsite = false;
    String custName = "";
    String jobInfo ="";
    String line = "";


    public void actionPerformed(ActionEvent e) {
        jobInfo=a1.getText();
        if (c1.isSelected()) {
            c1translation = true;
        }
        if (c2.isSelected()) {
            c2shortHand = true;
        }
        if (c3.isSelected()) {
            c3onsite = true;

        }jobInfo=a1.getText();
    }
}

When i run the GUI it is fine until i added the last part, where i am trying to assign the TextArea information to a String variable called jobInfo

The Error i get is, which seems to be complaining about threading. Is the problem the fact that i am trying to retrieve the information without doing something else first?

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at JobsGUI$JobHandler.actionPerformed(JobsGUI.java:202)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)

The declaration of a1

public class JobsGUI {

private Manager mmm = new Branch("Watford");
private JFrame myFrame = new JFrame("Jobs GUI");
private Container contentPane = myFrame.getContentPane();

private JButton quitButton = new JButton("Quit");
private JButton addJobButton = new JButton("Add Job");
private JButton clearListButton = new JButton("Clear List");


private JPanel eastPanel = new JPanel();
private JPanel westPanel = new JPanel();
private JPanel centerPanel = new JPanel();
private JPanel northPanel = new JPanel();
private JPanel southPanel = new JPanel();
JCheckBox c1, c2, c3;
JTextField t1, t2, t3;
JLabel l1, l2;
JTextArea a1;
JScrollPane s1;


public JobsGUI() {
    addAllStaff();
    makeFrame();
    makeMenus(myFrame);
    makeTypes();
    addJobs();
}


private void addJobs() {

    t1 = new JTextField(10);   //Customer name
    northPanel.add(t1);
    l1 = new JLabel("Enter Customer name here");
    northPanel.add(l1);
    c1 = new JCheckBox("Translation");
    westPanel.add(c1);
    c2 = new JCheckBox("ShortHand?");
    westPanel.add(c2);
    c3 = new JCheckBox("OnSite");
    westPanel.add(c3);

    JLabel l2 = new JLabel("Job Information");
    centerPanel.add(l2);
    l2.setVisible(true);

    JTextArea a1 = new JTextArea(15, 50);
    a1.setEditable(true);

    centerPanel.add(a1);
    a1.setVisible(true);

    addJobButton.addActionListener(new JobHandler());
Hannah
  • 57
  • 6
  • I would say, that either `a1`, `c1`, `c2` or `c3` is `null` – F. Lumnitz Dec 30 '16 at 10:28
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – F. Lumnitz Dec 30 '16 at 10:29
  • The error you have has nothing to do with threading, it's a NullPointerException in line 202 of your `JobsGUI` class. – Quagaar Dec 30 '16 at 10:30
  • Just set a breakpoint on public void actionPerformed and debug it to see where you have this NPE. – cybernetic87 Dec 30 '16 at 10:31
  • 1
    Have you declared a1 as a class variable & then initialized the same a1 in the class? – Pradnyarani Dec 30 '16 at 10:38
  • Found the issue....i was declaring it twice – Hannah Dec 30 '16 at 10:55
  • @Hannah Good...!! – Pradnyarani Dec 30 '16 at 11:01
  • I AM A NOOOOOB! – Hannah Dec 30 '16 at 11:02
  • 1
    *"I AM A NOOOOOB!"* That's how we all started and there are days, 16 years in programming Java, when I still feel like a newbie. OTOH with a little push in the right direction, at least you were able to find & fix the source of the problem. So at least you aren't a *clueless* newbie, like I was (& sometimes ***still am***). ;) Now you might write up an answer below, or simply delete the question. – Andrew Thompson Dec 30 '16 at 11:29

0 Answers0