Reputation: 1
My code is the following:
/*
* 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);
}
}
}
My error is the following:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at swagpad.NoteDetailsUI$SaveButtonListener.actionPerformed(NoteDetailsUI.java:103)
I've tried googling and researching as to why I'm getting this error. Common responses seem to be shadowed instance variables with local variables or non-initialized textfields/variables, but as far as I can tell I'm not doing those. Any assistance would be greatly appreciated.
Upvotes: 0
Views: 8478
Reputation: 10994
Your problem in next you initialized date,name,number,body
as local fields in initComponents
method.
JTextField date = new JTextField(10);
JTextField name = new JTextField(10);
JTextField number = new JTextField(10);
JTextArea body = new JTextArea();
because of that your class variables date,name,number,body
is null.
To avoid it use next code:
date = new JTextField(10);
name = new JTextField(10);
number = new JTextField(10);
body = new JTextArea();
Seems it problem of all your class variables. Initialize them not as local variables.
EDIT: change your initComponents()
method to the next:
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);
}
it solves all NPE
caused by class variables.
Read more about variables
Upvotes: 2
Reputation: 66
Your problem is actually very easy to overlook. In your initComponents() function, you initialize only local objects. Here's what I mean by that:
JButton deleteButton = new JButton("Delete");
That code will make a new JButton called deleteButton, BUT you want this variable to be global. When you redeclare it in your initComponents() function, you actually make an entirely new object and don't mess with the global variable.
So, for all of your initializations, remove the types. For example:
deleteButton = new JButton("Delete");
Upvotes: 0
Reputation: 245
JTextField number = new JTextField(10);
In your initComponents()
function, you're creating a new local variable number rather than using the member variable. So the member variable number
is still null when you get to the code where you're wanting to use it.
You should do one of two things: either call this.number = number;
after creating the variable, or just instantiate the member variable right off the bat, by changing it to
number = new JTextField(10);
Upvotes: 0