java - JTextField getText() NullPointerException

标签 java swing nullpointerexception jtextfield gettext

我的代码如下:

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package swagpad;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class NoteDetailsUI extends JFrame{

    NoteCntl theNoteCntl;
    Note theCurrentNote;
    JButton backButton;
    JButton newButton;
    JButton savebutton;
    JButton deleteButton;
    JTextField date;
    JTextField name;
    JTextField number;
    JLabel dateLabel;
    JLabel nameLabel;
    JLabel bodyLabel;
    JLabel numberLabel;
    JTextArea body;
    JPanel mainPanel;
    JPanel buttonPanel;
    JPanel fieldPanel;
    JPanel areaPanel;

        public NoteDetailsUI(NoteCntl theParentNoteCntl, Note theSelectedNote){
        theNoteCntl = theParentNoteCntl;
        theCurrentNote = theSelectedNote;
        this.initComponents();
        this.setSize(400, 500);
        this.setLocationRelativeTo(null);
        this.setTitle("Notes List");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    public void initComponents(){


        //Component initializations
        JButton deleteButton = new JButton("Delete");
        JButton backButton = new JButton("Back");
        backButton.addActionListener(new BackButtonListener());
        JButton saveButton = new JButton("Save");
        saveButton.addActionListener(new SaveButtonListener());
        JButton newButton = new JButton("New");
        JTextField date = new JTextField(10);
        JTextField name = new JTextField(10);
        JTextField number = new JTextField(10);
        JTextArea body = new JTextArea();
        JPanel mainPanel = new JPanel(new BorderLayout());
        JPanel buttonPanel = new JPanel();
        JPanel fieldPanel = new JPanel();
        JPanel areaPanel = new JPanel(new GridBagLayout());
        JLabel dateLabel = new JLabel("Date");
        JLabel nameLabel = new JLabel("Note Title");
        JLabel bodyLabel = new JLabel("Enter Note Text Here");
        JLabel numberLabel = new JLabel("Note Number");
        //Setup for the various panels. The button/textfield panels are default flow layouts, the areaPanel is a GridBag because I like the fill function
        //of Gridbag for this application. Mainpanel is a borderlayout. 
        //Button Panel setup.
        buttonPanel.add(backButton);
        buttonPanel.add(saveButton);
        buttonPanel.add(newButton);
        buttonPanel.add(deleteButton);

        // TextField Panel setup
        fieldPanel.add(numberLabel);
        fieldPanel.add(number);
        fieldPanel.add(nameLabel);
        fieldPanel.add(name);
        fieldPanel.add(dateLabel);        
        fieldPanel.add(date);

        //AreaPanel setup with Constraints for GridBag.
        GridBagConstraints bodyConstraints = new GridBagConstraints();
        bodyConstraints.fill = GridBagConstraints.BOTH;
        bodyConstraints.ipadx = 200;
        bodyConstraints.ipady = 300;
        bodyConstraints.weightx = 1;
        bodyConstraints.weighty = 1;
        areaPanel.add(bodyLabel);
        areaPanel.add(body, bodyConstraints);

        //MainPanel setup
        this.add(mainPanel);
        mainPanel.add(buttonPanel, BorderLayout.NORTH);
        mainPanel.add(fieldPanel, BorderLayout.CENTER);
        mainPanel.add(areaPanel, BorderLayout.SOUTH);
    }

    public class SaveButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent evt){

            if(theCurrentNote == null){
            System.out.println("null?");
    //Error is here. Not sure why I'm getting nulls from the getText());
             int noteNum = Integer.parseInt(NoteDetailsUI.this.number.getText());
             int theNoteDate = Integer.parseInt(NoteDetailsUI.this.date.getText());
             String theNoteName = NoteDetailsUI.this.name.getText();      
             String theNoteBody = NoteDetailsUI.this.body.getText();
             NoteDetailsUI.this.theCurrentNote = new EssayNote(noteNum, theNoteDate, theNoteName, theNoteBody);
             NoteDetailsUI.this.theNoteCntl.newNote(theCurrentNote);
             NoteDetailsUI.this.setVisible(false);
             NoteDetailsUI.this.dispose();
             NoteDetailsUI.this.theNoteCntl.getNoteTableModel();
            }

            else{

            }

        }
    }

    //Sends you back to the Note Table UI. 
    public class BackButtonListener implements ActionListener{
        public void actionPerformed(ActionEvent evt){
            NoteDetailsUI.this.theNoteCntl.getNoteTableUI();
            NoteDetailsUI.this.setVisible(false);

        }
    }


} 

我的错误如下:

线程“AWT-EventQueue-0”中出现异常 java.lang.NullPointerException 在 swagpad.NoteDetailsUI$SaveButtonListener.actionPerformed(NoteDetailsUI.java:103)

我尝试通过谷歌搜索并研究为什么会出现此错误。常见的响应似乎是带有局部变量或未初始化文本字段/变量的阴影实例变量,但据我所知,我没有这样做。任何帮助将不胜感激。

最佳答案

接下来您在 initComponents 方法中将 date,name,number,body 初始化为本地字段时出现问题。

 JTextField date = new JTextField(10);
 JTextField name = new JTextField(10);
 JTextField number = new JTextField(10);
 JTextArea body = new JTextArea();

因为你的类变量date,name,number,body为空。 为了避免它使用下面的代码:

 date = new JTextField(10);
 name = new JTextField(10);
 number = new JTextField(10);
 body = new JTextArea();

似乎是所有类变量的问题。不要将它们初始化为局部变量。

编辑:将您的 initComponents() 方法更改为下一个:

 public void initComponents(){
    deleteButton = new JButton("Delete");
    backButton = new JButton("Back");
    backButton.addActionListener(new BackButtonListener());
    savebutton = new JButton("Save");
    savebutton.addActionListener(new SaveButtonListener());
    newButton = new JButton("New");
    date = new JTextField(10);
    name = new JTextField(10);
    number = new JTextField(10);
    body = new JTextArea();
    mainPanel = new JPanel(new BorderLayout());
    buttonPanel = new JPanel();
    fieldPanel = new JPanel();
    areaPanel = new JPanel(new GridBagLayout());
    dateLabel = new JLabel("Date");
    nameLabel = new JLabel("Note Title");
    bodyLabel = new JLabel("Enter Note Text Here");
    numberLabel = new JLabel("Note Number");
    buttonPanel.add(backButton);
    buttonPanel.add(savebutton);
    buttonPanel.add(newButton);
    buttonPanel.add(deleteButton);

    // TextField Panel setup
    fieldPanel.add(numberLabel);
    fieldPanel.add(number);
    fieldPanel.add(nameLabel);
    fieldPanel.add(name);
    fieldPanel.add(dateLabel);        
    fieldPanel.add(date);

    //AreaPanel setup with Constraints for GridBag.
    GridBagConstraints bodyConstraints = new GridBagConstraints();
    bodyConstraints.fill = GridBagConstraints.BOTH;
    bodyConstraints.ipadx = 200;
    bodyConstraints.ipady = 300;
    bodyConstraints.weightx = 1;
    bodyConstraints.weighty = 1;
    areaPanel.add(bodyLabel);
    areaPanel.add(body, bodyConstraints);

    //MainPanel setup
    this.add(mainPanel);
    mainPanel.add(buttonPanel, BorderLayout.NORTH);
    mainPanel.add(fieldPanel, BorderLayout.CENTER);
    mainPanel.add(areaPanel, BorderLayout.SOUTH);
}

它解决了所有由类变量引起的NPE

了解更多关于variables的信息

关于java - JTextField getText() NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19916507/

相关文章:

java - Hibernate afterAfterTransactionCompletion 拦截器

Java包: What is the difference between `oracle.AQ` and `oracle.jdbc.aq` ?

java - 如何将全局 JPanel 添加到 JFrame

android - 按钮不是从 XML 中获取的?空指针

java - 什么是 NullPointerException,我该如何解决?

java - 由于 NullPointerException,我的可执行 jar 文件无法运行

java unmarshalling - NULL 值或缺少标记?

java - JNLP通过文件: but not http:加载Sigar native 库

java - StackOverflowError 由 TableModelListener 引起

java - 使用JPasswordField并使用getPassword +服务器登录过程处理密码