gpt4 book ai didi

java - JTextField getText() NullPointerException

转载 作者:行者123 更新时间:2023-12-02 06:32:55 25 4
gpt4 key购买 nike

我的代码如下:

 /*
* 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com