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#";