I'm trying to run this code. It reads the file successfully, but breaks at the word search itself.
The error:
Exception in thread "main" java.lang.NullPointerException
at WordSearch.main(WordSearch.java:30)
Here's the code:
import javax.swing.JOptionPane;
import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
public class WordSearch{
public static void main(String[] args) throws FileNotFoundException{
String searchWord, fileName; int i=0, j=0;
fileName = JOptionPane.showInputDialog(null, "Please enter the name of the file to be processes: ");
File textFile = new File(fileName);
Scanner scanner = new Scanner(textFile);
String[] file = new String[500];
String[] found = new String[500];
while(scanner.hasNextLine()){
file[i]=scanner.next();
i++;
}
file[i+1]="EoA"; i=0;
searchWord = JOptionPane.showInputDialog(null, "Please input the string to search for: ");
while(!file[i].equals("EoA")){
if (file[i].equals(searchWord)){
if(i==0){
found[j]=file[i]+file[i+1]+"\n";
}
else if(i==500){
found[j]=file[i-1]+file[i]+"\n";
}
else {
found[j]=file[i-1]+file[i]+file[i+1]+"\n";
}
j++;
}
i++;
}
JOptionPane.showMessageDialog(null, found);
}
}