0

I`m trying to save entered in gui information in a text file. The error I get is a

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

As I understand, when I click the OK button, the button doesn't receive anything and that's why I get a nullPointerException.
Where is a mistake?

Here is a JPanel class that manages JTextFields.

import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class LibraryTextfieldsPanel extends JPanel{
private JTextField copyID;
private JTextField ISBN;
private JTextField author;


public LibraryTextfieldsPanel() {
    this.setLayout(new GridLayout(3,1));
    Container choiceBox = new Container();

    this.add(new JLabel("Copy ID "));
copyID = new JTextField();
this.add(copyID);

    this.add(new JLabel("ISBN "));
ISBN = new JTextField();
this.add(ISBN);

    this.add(new JLabel("Author "));
author = new JTextField();
this.add(author);`enter code here`

}

public Integer getCopyID() {
      try {
          return Integer.valueOf(copyID.getText());
      }
      catch(NullPointerException ex) {
          return 0;
      }
      catch(NumberFormatException ex2) {
          return 0;
      }
    }


public Integer getISBN() {
    try {
          return Integer.valueOf(ISBN.getText());
      }
      catch(NullPointerException ex) {
          return 0;
      }
      catch(NumberFormatException ex2) {
          return 0;
      }
    }


public String getAuthor() {
    return author.getText();
}

}

This is my Button Panel class that should receive the data from LibraryTextfieldsPanel.

import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import javax.swing.JButton;
import javax.swing.JPanel;

public class OkButtonPanel extends JPanel {
private JButton ok;
private JButton cancel;
private LibraryRadioPanel radioPanel;
private LibraryTextfieldsPanel textFieldsPanel;
private CategoriesPanel categories; 

public OkButtonPanel(LibraryRadioPanel radioPanel, LibraryTextfieldsPanel textFieldsPanel,
        CategoriesPanel categories) {
    this.setLayout((LayoutManager) new FlowLayout(FlowLayout.RIGHT));
    ok = new JButton("OK");
    cancel = new JButton("CANCEL");
    this.add(ok);
    this.add(cancel);
    this.radioPanel = radioPanel;
    this.textFieldsPanel = textFieldsPanel;
    this.categories = categories;

    ok.addActionListener(new ButtonListener());
    cancel.addActionListener(new ButtonListener());
}

private class ButtonListener implements ActionListener {


    @Override
    public void actionPerformed(ActionEvent arg0) {

        JButton theButton = (JButton) arg0.getSource();

        if (theButton == ok) {
            Integer copyID = textFieldsPanel.getCopyID();
            Integer  isbn = textFieldsPanel.getISBN();
            String   author = textFieldsPanel.getAuthor();
            try {

                PrintWriter out = new PrintWriter(new File("MyLibrary.txt"));
                out.append(copyID + " " + isbn + " "
                        + author);
                out.close();
            } catch (FileNotFoundException e) {
                System.out.println("File Not Found Exception");
            }

        }else if (theButton == cancel) {
            textFieldsPanel.setVisible(false);

        }
    }

}

Thanks in advance

fnklstn
  • 492
  • 4
  • 16
  • 1
    Which line is the exception thrown? The stack trace printed together with the exception should tell you where the exception happened and you can trace your code accordingly. – Judger Apr 16 '18 at 19:10
  • Line 46 in OkButtonPanel which is `Integer copyID = textFieldsPanel.getCopyID();` – fnklstn Apr 16 '18 at 19:13
  • 1
    How do you instantiate OkButtonPanel? If the NPE does happen where you say it does, I'm thinking that textFieldsPanel is null at that point. – akortex Apr 16 '18 at 19:16
  • But again a complete stack trace would be helpful. – akortex Apr 16 '18 at 19:17

1 Answers1

0

Based off what you say where the error is happening, it looks like textFieldsPanel is null. I would check if textFieldsPanel is null when passing it to OkButtonPanel(). If you can, make sure it's not null when passing it into OkButtonPanel.

public OkButtonPanel(LibraryRadioPanel radioPanel, LibraryTextfieldsPanel textFieldsPanel,
        CategoriesPanel categories) {
    this.setLayout((LayoutManager) new FlowLayout(FlowLayout.RIGHT));
    ok = new JButton("OK");
    cancel = new JButton("CANCEL");
    this.add(ok);
    this.add(cancel);
    this.radioPanel = radioPanel;
    if(textFieldsPanel == null) { // Should probably make sure it's not passed as null
        // Do something to handle it
    } else {
        this.textFieldsPanel = textFieldsPanel;
    }

    this.categories = categories;

    ok.addActionListener(new ButtonListener());
    cancel.addActionListener(new ButtonListener());
}
Jimenemex
  • 3,104
  • 3
  • 24
  • 56