首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JTextField getText() NullPointerException

JTextField getText() NullPointerException
EN

Stack Overflow用户
提问于 2013-11-12 05:41:16
回答 3查看 7.8K关注 0票数 0

我的代码如下:

代码语言:javascript
复制
 /*
 * 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);

        }
    }


} 

我的错误如下:

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

我试着用谷歌搜索和研究为什么我会得到这个错误。常见的响应似乎是带有局部变量或未初始化的文本字段/变量的影子实例变量,但据我所知,我没有这样做。任何帮助都将不胜感激。

EN

回答 3

Stack Overflow用户

发布于 2013-11-12 05:46:47

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

代码语言:javascript
复制
 JTextField date = new JTextField(10);
 JTextField name = new JTextField(10);
 JTextField number = new JTextField(10);
 JTextArea body = new JTextArea();

因此,您的类变量date,name,number,body为空。为了避免这种情况,请使用下面的代码:

代码语言:javascript
复制
 date = new JTextField(10);
 name = new JTextField(10);
 number = new JTextField(10);
 body = new JTextArea();

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

编辑:将您的initComponents()方法更改为next:

代码语言:javascript
复制
 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的更多信息

票数 2
EN

Stack Overflow用户

发布于 2013-11-12 05:46:26

代码语言:javascript
复制
JTextField number = new JTextField(10);

initComponents()函数中,您将创建一个新的局部变量number,而不是使用成员变量。因此,当您进入要使用成员变量number的代码时,它仍然为null。

您应该做以下两件事之一:要么在创建变量后调用this.number = number;,要么立即实例化成员变量,方法是将其更改为

代码语言:javascript
复制
number = new JTextField(10);
票数 0
EN

Stack Overflow用户

发布于 2013-11-12 05:48:14

你的问题实际上很容易被忽视。在initComponents()函数中,只能初始化本地对象。这就是我的意思:

代码语言:javascript
复制
JButton deleteButton = new JButton("Delete");

该代码将创建一个名为deleteButton的新JButton,但是您希望该变量是全局变量。当您在initComponents()函数中重新声明它时,实际上是创建了一个全新的对象,并且不会弄乱全局变量。

因此,对于所有的初始化,请删除这些类型。例如:

代码语言:javascript
复制
deleteButton = new JButton("Delete");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19916507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档