我的代码如下:
/*
* 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)
我已经尝试使用谷歌搜索和研究我为什么会收到此错误。常见的响应似乎是带有局部变量或非初始化的文本字段/变量的实例变量的阴影,但据我所知,我没有这样做。任何帮助将不胜感激。
答案 0 :(得分:2)
接下来您的问题在date,name,number,body
方法中将initComponents
初始化为本地字段。
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
答案 1 :(得分:0)
JTextField number = new JTextField(10);
在initComponents()
函数中,您将创建一个新的局部变量编号,而不是使用成员变量。因此,当您到达想要使用它的代码时,成员变量number
仍为null。
你应该做以下两件事之一:在创建变量后调用this.number = number;
,或者直接实例化成员变量,通过将其更改为
number = new JTextField(10);
答案 2 :(得分:0)
您的问题实际上很容易被忽视。在initComponents()函数中,只初始化本地对象。这就是我的意思:
JButton deleteButton = new JButton("Delete");
该代码将创建一个名为deleteButton的新JButton,但是您希望此变量是全局的。当你在initComponents()函数中重新声明它时,你实际上创建了一个全新的对象,并且不会弄乱全局变量。
因此,对于所有初始化,请删除类型。例如:
deleteButton = new JButton("Delete");