0

我的代码如下:

 /*
 * 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 处的线程“AWT-EventQueue-0”java.lang.NullPointerException 中的异常(NoteDetailsUI.java:103)

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

4

3 回答 3

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由类变量引起的问题。

阅读有关变量的更多信息

于 2013-11-11T21:46:46.717 回答
0
JTextField number = new JTextField(10);

在您的initComponents()函数中,您正在创建一个新的局部变量编号,而不是使用成员变量。number因此,当您到达要使用它的代码时,成员变量仍然为空。

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

number = new JTextField(10);
于 2013-11-11T21:46:25.820 回答
0

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

JButton deleteButton = new JButton("Delete");

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

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

deleteButton = new JButton("Delete");
于 2013-11-11T21:48:14.370 回答