1

Firstly, i have done research on what Null pointer means and i have read the below but i am still completely lost.

What is a NullPointerException, and how do I fix it?

My error is

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at EBIAlarmUI$2.alarm1ButtonActionPerformed(EBIAlarmUI.java:113)
at EBIAlarmUI$2.actionPerformed(EBIAlarmUI.java:109)

And this is the code that is causing it.

alarm1Button = new JButton();
    alarm1Button.setText("Alarm 1");
    GridBagConstraints gbc_alarm1Button = new GridBagConstraints();
    gbc_alarm1Button.insets = new Insets(0, 0, 5, 0);
    gbc_alarm1Button.gridx = 9;
    gbc_alarm1Button.gridy = 2;
    getContentPane().add(alarm1Button, gbc_alarm1Button);

    alarm1Button.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            alarm1ButtonActionPerformed(evt);
        }
        public synchronized void alarm1ButtonActionPerformed(
                java.awt.event.ActionEvent evt) {
            resultsTextArea.setText("Alarm 1 Activated, String: " + alarm1);

            try {
                byte[] b = alarm1.getBytes("ASCII");

                TwoWaySerialComm.SerialWriter sw = new 
    TwoWaySerialComm.SerialWriter(
                        twoWaySerCom.serialPort.getOutputStream());

                sw.out.write(b);
            } catch (IOException e) {
                // Do something to handle the exception
                e.printStackTrace();
            }
        }
    });

Specifically these 2 lines.

alarm1ButtonActionPerformed(evt);

and

resultsTextArea.setText("Alarm 1 Activated, String: " + alarm1);

The exact code worked a few hours ago before i moved from a GroupLayout to a GridBag layout.

So either resultsTextArea or alarm1 are Null? I dont understand how that can be. They appear in my GUI

JTextArea resultsTextArea = new JTextArea();
    GridBagConstraints gbc_resultsTextArea = new GridBagConstraints();
    gbc_resultsTextArea.gridwidth = 3;
    gbc_resultsTextArea.fill = GridBagConstraints.BOTH;
    gbc_resultsTextArea.gridx = 7;
    gbc_resultsTextArea.gridy = 7;
    getContentPane().add(resultsTextArea, gbc_resultsTextArea);

and i even tried adding them to my variables declaration

public javax.swing.JTextArea resultsTextArea;
public javax.swing.JTextArea customStringTextArea;
public javax.swing.JButton alarm1Button;
String alarm1 = "$81,$01,$06,$01,$10,$10,$03,$01,$FF";
String alarm2 = "*993R03,67,6#";
String alarm3 = "*994R14,67,1#";
Community
  • 1
  • 1
Display Name
  • 405
  • 6
  • 26

1 Answers1

2

resultsTextArea is null. It's null because you're shadowing your variables.

First of all, you decleare an instance field...

public javax.swing.JTextArea resultsTextArea;

But when you initialise it, you're creating a local version...

JTextArea resultsTextArea = new JTextArea();

Which means that when you try to reference the instance field from the ActionListener, it is null.

Remove the second decleration...

//JTextArea resultsTextArea = new JTextArea();
resultsTextArea = new JTextArea();     
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks a lot, I see where i went wrong now. I have just started using Eclipse over Netbeans as a lot of tutorials use it. I notice that a lot of problems seem to go away when i restart Eclipse. Is that something that happens to you? – Display Name Feb 26 '15 at 02:34
  • I don't use Eclipse, I use Netbeans. But you might find that the caches used by both tend to get out of sync with what's really happening and need to be "cleaned" up regularly. – MadProgrammer Feb 26 '15 at 02:40