S
steve_marjoribanks
I am having some problems with part of my code. It is a couple of
methods which are part of a much larger class but I'm fairly certain
this is where the problems are. Specifically the 'buffer.close();'
line. It all compiles fine but then throws a NullPointerException
runtime exception saying 'Exception in thread "AWT-EventQueue-0" . I'm
new to Java and have tried a few things but it keeps on doing the same
thing. I think it's probably me not quite fully understanding one of
the methods I've used or something.
Thanks in advance for any help!
Steve
public void loadFileChooser()
{
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = chooser.showOpenDialog(this);
if (result == JFileChooser.CANCEL_OPTION) return;
File filename = chooser.getSelectedFile();
if (filename == null || filename.getName().equals("") )
{
JOptionPane.showMessageDialog(this,"Invalid File Name","Invalid File
Name",JOptionPane.ERROR_MESSAGE);
}
else
{
try
{
BufferedReader buffer = new BufferedReader(new
FileReader(filename));
// enable close button and disable load button (currently, only one
buffer may be open at once)
menuLoad.setEnabled(false);
menuClose.setEnabled(true);
toolbarLoad.setEnabled(false);
toolbarClose.setEnabled(true);
// test
boolean eof = false;
while (!eof)
{
String lineInput = buffer.readLine();
String printLine = "\n" + lineInput;
if (lineInput == null)
{
eof = true;
}
else
{
blank.append(printLine);
}
}
}
catch (IOException openError)
{
JOptionPane.showMessageDialog(this, "Error Opening File", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
public void closeFile()
{
try
{
blank.setText("");
buffer.close();
menuLoad.setEnabled(true);
menuClose.setEnabled(false);
toolbarLoad.setEnabled(true);
toolbarClose.setEnabled(false);
}
catch (IOException closeError)
{
JOptionPane.showMessageDialog(this, "Error Closing File", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
methods which are part of a much larger class but I'm fairly certain
this is where the problems are. Specifically the 'buffer.close();'
line. It all compiles fine but then throws a NullPointerException
runtime exception saying 'Exception in thread "AWT-EventQueue-0" . I'm
new to Java and have tried a few things but it keeps on doing the same
thing. I think it's probably me not quite fully understanding one of
the methods I've used or something.
Thanks in advance for any help!
Steve
public void loadFileChooser()
{
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = chooser.showOpenDialog(this);
if (result == JFileChooser.CANCEL_OPTION) return;
File filename = chooser.getSelectedFile();
if (filename == null || filename.getName().equals("") )
{
JOptionPane.showMessageDialog(this,"Invalid File Name","Invalid File
Name",JOptionPane.ERROR_MESSAGE);
}
else
{
try
{
BufferedReader buffer = new BufferedReader(new
FileReader(filename));
// enable close button and disable load button (currently, only one
buffer may be open at once)
menuLoad.setEnabled(false);
menuClose.setEnabled(true);
toolbarLoad.setEnabled(false);
toolbarClose.setEnabled(true);
// test
boolean eof = false;
while (!eof)
{
String lineInput = buffer.readLine();
String printLine = "\n" + lineInput;
if (lineInput == null)
{
eof = true;
}
else
{
blank.append(printLine);
}
}
}
catch (IOException openError)
{
JOptionPane.showMessageDialog(this, "Error Opening File", "Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
public void closeFile()
{
try
{
blank.setText("");
buffer.close();
menuLoad.setEnabled(true);
menuClose.setEnabled(false);
toolbarLoad.setEnabled(true);
toolbarClose.setEnabled(false);
}
catch (IOException closeError)
{
JOptionPane.showMessageDialog(this, "Error Closing File", "Error",
JOptionPane.ERROR_MESSAGE);
}
}