I'm trying to learn GUI programing and I didn't find why and how to fix my code. my problem is that I'm using JFileChooser to open a file and when I want to cancel the option or close the JFileChooser window, it throws me this exceptions:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at myForm.OpenFile(myForm.java:296)
at myForm.openVActionPerformed(myForm.java:230)
at myForm.access$1(myForm.java:222)
at myForm$2.actionPerformed(myForm.java:102)
this is my code:
private void OpenFile() {
try {
File thisFile;
JFileChooser of = new JFileChooser();
int option = of.showOpenDialog(of);
while (!of.getSelectedFile().getName().endsWith(".xml")) {
String error = "Error, Please select xml file";
JOptionPane.showMessageDialog(this, error, "Wrong type of file", JOptionPane.INFORMATION_MESSAGE);
of = new JFileChooser();
option = of.showOpenDialog(of);
}
if (option == JFileChooser.APPROVE_OPTION){
thisFileName = of.getSelectedFile().getPath();
thisFile = new File(thisFileName);
pointsList.clear();
}
else {
Iterator<Point> i = pointsList.iterator();
while(i.hasNext()) {
p = i.next();
drewPoints(p.x, p.y);
}
return;
}
//.... the next lines irrelevant for my question...