Why am I getting a NullPointerException? [closed]
I posted this code earlier, but was told to clean it up and repost. The errors I'm getting are:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at FVolume.createCustomers(FVolume.java:74)
at FVolume.<init>(FVolume.java:21)
at FVolume.main(FVolume.java:270)
Here is the code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class FVolume extends JFrame implements ActionListener{
private JTabbedPane jtabbedPane;
private JPanel Customers;
JTextArea NameTextCustomers, ExistTextCustomers, NameTextContractors, ExistTextContractors;
public FVolume(){
setTitle("Volume Calculator");
setSize (300, 200);
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );
createCustomers();
jtabbedPane = new JTabbedPane();
jtabbedPane.addTab("Customers", Customers);
topPanel.add(jtabbedPane, BorderLayout.CENTER);
}
/* CREATE CUSTOMERS */
public JPanel createCustomers(){
Customers = new JPanel();
Customers.setLayout(null);
NameTextCustomers = new JTextArea();
NameTextCustomers.setBounds(10, 10, 350, 150);
NameTextCustomers.setLineWrap(true);
Customers.add(NameTextCustomers);
JButton Exit = new JButton("Exit");
Exit.setBounds(30,170,80,20);
Exit.addActionListener(this);
Exit.setBackground(Color.white);
Customers.add(Exit);
JButton AddCustomers = new JButton("Add Customer");
AddCustomers.setBounds(130,170,120,20);
AddCustomers.setBackground(Color.white);
Customers.add(AddCustomers);
JButton Refresh = new JButton("Refresh");
Refresh.setBounds(260,170,80,20);
Refresh.setBackground(Color.white);
Customers.add(Refresh);
ExistTextCustomers = new JTextArea();
ExistTextCustomers.setBounds(10, 200, 350, 60);
ExistTextCustomers.setLineWrap(true);
Customers.add(ExistTextCustomers);
final JTextArea custArea = new JTextArea(6, 30);
final JTextArea custMessage = null;
AddCustomers.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
new Customer("Customer");
}
});
Customers.add(custArea);
Customers.add(AddCustomers);
Customers.add(Refresh);
Customers.add(custMessage);
Refresh.setMnemonic('R');
Refresh.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
custMessage.setText("");
try
{
File custOpen = new File("customer.txt");
FileReader custAreaIn = new FileReader(custOpen);
custArea.read(custAreaIn, custOpen.toString());
custMessage.setText("The file exists and can be read from.");
}
catch (IOException e3){
custMessage.setText("The file could not be read. " + e3.getMessage());
}
}
}
);
return Customers;
}
class Customer extends JFrame
{
private String[] states = {"AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE",
"FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME",
"MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ",
"NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD",
"TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"};
private JComboBox StateList = new JComboBox(states);
private JTextField NameText = new JTextField(25);
private JTextField AddressText = new JTextField(25);
private JTextField CityText = new JTextField(25);
private JTextField ZipText = new JTextField(9);
private JTextField PhoneText = new JTextField(10);
private JTextField PopMessageText = new JTextField(30);
private static final long serialVersionUID = 1L;
private AddCustButtonHandler addCusHandler = new AddCustButtonHandler();
public Customer(String who)
{
popUpWindow (who);
}
public void popUpWindow(final String who) {
final JFrame popWindow;
popWindow = new JFrame(who);
popWindow.setSize(425, 350);
popWindow.setLocation(100, 100);
popWindow.setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container c = new Container();
popWindow.add(c);
c.setLayout(new FlowLayout());
JPanel one = new JPanel();
JPanel two = new JPanel();
JPanel three = new JPanel();
JPanel four = new JPanel();
JPanel five = new JPanel();
JPanel six = new JPanel();
one.add(new JLabel(who + " Name "));
one.add(NameText);
two.add(new JLabel("Address "));
two.add(AddressText);
three.add(new JLabel("City "));
three.add(CityText);
four.add(new JLabel("State "));
StateList.setSelectedIndex(0);
four.add(StateList);
four.add(new JLabel("ZIP"));
four.add(ZipText);
four.add(new JLabel("Phone"));
four.add(PhoneText);
JButton addwho = new JButton("Add " + who);
addwho.setMnemonic('A');
JButton close = new JButton("Close");
close.setMnemonic('C');
JButton deleteFile = new JButton("Delete File");
deleteFile.setMnemonic('D');
five.add(addwho);
five.add(close);
five.add(deleteFile);
PopMessageText.setEditable(false);
PopMessageText.setHorizontalAlignment(JTextField.CENTER);
six.add(PopMessageText);
c.add(one);
c.add(two);
c.add(three);
c.add(four);
c.add(five);
c.add(six);
deleteFile.setToolTipText("Delete File");
addwho.setToolTipText("Add "+ who);
close.setToolTipText("Close");
if (who == "Customer")
addwho.addActionListener(addCusHandler);
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
NameText.setText("");
AddressText.setText("");
CityText.setText("");
ZipText.setText("");
PhoneText.setText("");
PopMessageText.setText("");
popWindow.dispose();
}
}
);
deleteFile.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PopMessageText.setText("");
if (who == "Customer") {
File file = new File("Customer.txt");
boolean cusFileDeleted = file.delete();
if (cusFileDeleted) {
PopMessageText
.setText("Customer file has been deleted");
} else {
PopMessageText
.setText("There was an error in deleting file");
}
}
}
}
);
}
class AddCustButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent addCusHandler) {
int StateIndex;
try {
File file = new File("Customer.txt");
boolean success = file.createNewFile();
if (success) {
PopMessageText
.setText("Customer.txt file created file added");
} else if (file.canWrite()) {
PopMessageText
.setText("Writing data to Customer.txt, file added");
} else {
PopMessageText.setText("Cannot create file: Customer.txt");
}
try {
FileWriter fileW = new FileWriter("Customer.txt", true);
fileW.write(NameText.getText());
fileW.write(",");
fileW.write(AddressText.getText());
fileW.write(",");
fileW.write(CityText.getText());
fileW.write(",");
StateIndex = StateList.getSelectedIndex();
fileW.write(states[StateIndex]);
开发者_如何转开发 fileW.write(",");
fileW.write(ZipText.getText());
fileW.write(",");
fileW.write(PhoneText.getText());
fileW.write("\r\n");
fileW.close();
PopMessageText.setText("A new Customer has been added!");
FileReader fileR = new FileReader("Customer.txt");
BufferedReader buffIn = new BufferedReader(fileR);
String textData = buffIn.readLine();
buffIn.close();
}
catch (IOException e1) {
JOptionPane.showMessageDialog(null, e1.getMessage(), "ERROR", 2);
}
NameText.setText("");
AddressText.setText("");
CityText.setText("");
ZipText.setText("");
PhoneText.setText("");
}
catch (IOException e1) {
}
}
}
public void actionPerformed(ActionEvent event){
}
private void Exit_pressed(){
System.exit(0);
}
}
public void actionPerformed(ActionEvent e) {
}
public static void main(String[] args){
JFrame frame = new FVolume();
frame.setSize(380, 350);
frame.setVisible(true);
}
}
This is the problem:
final JTextArea custMessage = null;
...
Customers.add(custMessage);
What did you hope that would do? You're not allowed to pass in a null reference to the add
method, basically.
For future reference, the clue to diagnosing this yourself is to find the first bit of the stack trace that refers to your code - which in this case is the line:
Customers.add(custMessage);
Then look what it's using (Customers
and custMessage
) and work out whether they're okay. In this case it's pretty clear that the value of custMessage
is null, which isn't allowed.
精彩评论