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