Posted: Sun Aug 07, 2011 11:03 pm Post subject: NullPointerException
For whatever reason, there is a NullPointerException in a GUI Program I made to open files:
code:
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class ReadWriteFile extends JFrame implements ActionListener
{
public JFrame f1 = new JFrame("Open/Save");
public JFrame f2 = new JFrame("Choose file");
public JPanel p1 = new JPanel();
public JPanel p2 = new JPanel();
public JButton b1 = new JButton ("Open");
public JButton b2 = new JButton ("Save");
public JFileChooser fc = new JFileChooser();
public File f;
public int select;
public ReadWriteFile()
{
f1.setContentPane(p1);
f1.setSize(300, 300);
f1.setResizable(true);
p1.add(b1);
p1.add(b2);
f2.setContentPane(p2);
f2.setSize(500, 500);
f2.setResizable(true);
p2.add(fc);
b1.addActionListener(this);
b2.addActionListener(this);
f1.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == b1)
{
f2.setVisible(true);
FileRead();
}
}
public void FileRead()
{
if (select == fc.APPROVE_OPTION)
{
f = fc.getSelectedFile();
}
}
public static void main(String args[]) throws Exception
{
ReadWriteFile rwf = new ReadWriteFile();
}
}
The java.lang.NullPointerException occurs when the user attempts to open a file, I do not know why this is.[/list]
Sponsor Sponsor
Tony
Posted: Sun Aug 07, 2011 11:47 pm Post subject: RE:NullPointerException
Because, on the line where this exception is thrown, you are trying to call a method on NULL. Try to step backwards to figure out why that variable did not have any value assigned to it.