0

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.

Med Elgarnaoui
  • 1,612
  • 1
  • 18
  • 35
  • how does that even compile? You are calling setText("..") and setVisible(..) on variables you don't even declare – Stultuske Mar 18 '22 at 13:44
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Stultuske Mar 18 '22 at 13:47
  • It seems, that one of your jTextFieldXX not set to an instance of an Object. Try debugging it. – Valerij Dobler Mar 18 '22 at 13:48
  • In general it is bad practice to create Swing Components that extend JFrame: If you extend from another class (like JPanel) it gives you the power later to use this GUI component in a JFrame or a JDialog or a simple JWindow, and you can also visualize it using JOptionPane – ControlAltDel Mar 18 '22 at 13:54
  • All variables are declared, I just didn't want to paste all of the code, as it would be too much. All GUI components were created by NetBeans builder and automatically generated and declared. Everything works to the point where I call the method from the other class, thats where I get the exception. – Danilo Djurovic Mar 18 '22 at 14:27

0 Answers0