I'm getting a NullPointerException when trying to call a method in a GUI class from another GUI class. I'm trying to call a method from RejectData.class in MainWindow.class. Method is called at the end of MainWindow.class code snippet.
RejectData.class
public class RejectData extends JFrame{
MainWindow window = null;
/**
* Creates new form CountData
*/
public RejectData() {
initComponents();
this.setSize(245,420);
this.setLocationRelativeTo(window);
this.setContentPane(jPanel2);
}
public RejectData(MainWindow window){
this.window = window;
}
public void RejectMIBrsd(){
jTextField1.setText("10RSD");
jTextField3.setText("20RSD");
jTextField5.setText("50RSD");
jTextField7.setText("100RSD");
jTextField9.setText("200RSD");
jTextField11.setText("500RSD");
jTextField13.setText("1000RSD");
jTextField15.setText("2000RSD");
jTextField15.setVisible(true);
jTextField16.setVisible(true);
jTextField17.setText("5000RSD");
jTextField17.setVisible(true);
jTextField18.setVisible(true);
}
` MainWindow.class
public class MainWindow extends javax.swing.JFrame {
public SerialPort serialPort;
List<String> line;
JComm jcomm = null;
RejectData rejectData = null;
Thread thread;
public MainWindow() {
createObjects();
initComponents();
this.setLocationRelativeTo(null);
cb_portovi.setEnabled(true);
btn_start.setEnabled(true);
btn_stop.setEnabled(false);
}
private void createObjects() {
jcomm = new JComm(this);
rejectData = new RejectData(this);
}public class MainWindow extends javax.swing.JFrame {
public SerialPort serialPort;
List<String> line;
JComm jcomm = null;
RejectData rejectData = null;
Thread thread;
public MainWindow() {
createObjects();
initComponents();
this.setLocationRelativeTo(null);
cb_portovi.setEnabled(true);
btn_start.setEnabled(true);
btn_stop.setEnabled(false);
}
private void createObjects() {
jcomm = new JComm(this);
rejectData = new RejectData(this);
}
private void btn_startActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (btn_start.getText().equals("START")) {
try {
SerialPort[] allAvailableComPorts = SerialPort.getCommPorts();
serialPort = allAvailableComPorts[cb_portovi.getSelectedIndex()];
serialPort.setBaudRate(Integer.parseInt(cb_brzina.getSelectedItem().toString()));
serialPort.openPort();
if (serialPort.openPort()) {
JOptionPane.showMessageDialog(this, serialPort.getDescriptivePortName() + " je povezan.");
serialPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);
cb_portovi.setEnabled(false);
cb_brzina.setEnabled(false);
cb_brojacica.setEnabled(false);
btn_start.setEnabled(false);
btn_stop.setEnabled(true);
thread = new Thread() {
@Override
public void run() {
boolean running = true;
while (running) {
if (cb_brojacica.getSelectedItem().equals("SB7/SB9/MIB9")) {
line = readFromPort(serialPort);
JOptionPane.showMessageDialog(null, "Apoenska struktura primljena.");
switch (line.get(15)) {
case "RSD":
new RejectData().setVisible(true);
***rejectData.RejectMIBrsd();***
jcomm.toTableMIB9rsd(line);
break;
Exception:
Exception in thread "Thread-3" java.lang.NullPointerException at masterteam.RejectData.RejectMIBrsd(RejectData.java:36) at masterteam.MainWindow$10.run(MainWindow.java:458)
I can't see what I'm doing wrong.