Exception in thread "main" java.lang.NullPointerException
Hi
whenever I try to compile and run the following code, i get this error
Exception in thread "main" java.lang.NullPointerException
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at animalmain.main(animalmain.java:24)
Code:
import java.util.Scanner;
import javax.swing.*;
public class animalmain {
public static void main(String[] args) throws Exception {
int noOfLines = 0;
int b = 1;
int ask;
boolean win = false;
String lineSeparator = System.getProperty("line.separator");
JFileChooser filechooser = new JFileChooser();
java.io.File findNoOfLines = filechooser.getSelectedFile();
Scanner checker = new Scanner(findNoOfLines);
checker.useDelimiter(lineSeparator);
while (checker.hasNext()){
noOfLines++;
}
checker.close();
questionarray[]quest = new questionarray[noOfLines];
for(int i = 0; i < quest.length; i++){
quest[i] = new questionarray();
}
java.io.File file = filechooser.getSelectedFile();
Scanner reader = new Scanner(file);
reader.useDelimiter("|");
while (reader.hasNext()){
int i = reader.nextInt();
quest[i].q = reader.next();
quest[i].yes = reader.nextInt();
quest[i].no = reader.nextInt();
}
reader.close();
while (b <= quest.length){
ask = JOptionPane.showConfirmDialog(null,
quest[b].q,
"Guess 1.0",
JOptionPane.QUESTION_MESSAGE);
if (ask == JOptionPane.YES_OPTION)
b = quest[b].yes;
else
b = quest[b].no;
if(b == -1){
win = true;
break;
}
else if (b == -2){
win = false;
break;
}
}
if (win == true){
JOptionPane.showMessageDialog(null, "I win");
}
else if (win == false){
java.io.PrintWriter toFile = new java.io.PrintWriter(file);
Scanner writeToFile = new Scanner(file);
String write = JOptionPane.showInputDialog("Enter the animal you were thinking of");
String writer = JOptionPane.showInputDialog("Enter a question that you could answer yes for " + write + " and no for " + quest[b].q);
while (writeToFile.hasNext()){
String s1 = writeToFile.nextLine();
String s2 = s1.replaceAll(quest[b].q + writeToFile.nextInt() + writeToFile.nextInt(), writer + (quest.length+1) + (quest.length+2) );
}
toFile.print((quest.length+1) + "|" + quest[b].q + "|");
toFile.print(-1);
toFile.print("|");
toFile.println(-2);
}
}
}
class questionarray{
int qnum;
String q;
int yes;
int no;
questionarray(){
}
}
What is causing this error?:confused:
Please help