单击按钮在线程" AWT-EventQueue-0"

时间:2018-04-16 19:04:41

标签: java swing jframe jpanel awt

我试图在文本文件中保存输入gui信息。 我得到的错误是

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

据我了解,当我点击OK按钮时, 按钮没有收到任何内容,这就是我收到nullPointerException的原因 哪里出错?

这是一个管理JTextFields的JPanel类。

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();
}

}

这是我的Button Panel类,它应该从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);

        }
    }

}

提前致谢

1 个答案:

答案 0 :(得分:0)

根据您所说的错误发生地点,看起来textFieldsPanelnull。将textFieldsPanel传递给null时,我会检查OkButtonPanel()是否为null。如果可以,请确保在将其传递到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()); }

{{1}}