Tuesday, 10 September 2013

Recieving a null pointer exception

Recieving a null pointer exception

I'm receiving a null pointer exception from some code I've wrote, I cannot
see the reason for the exception. This is my code:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
public class SearchingFilesMain {
public static void main(String[] arg) {
int checker4 = 0;
String checker3 = "";
String checker2 = "";
String checker1 = "";
String checker = "";
try {
Scanner scan = new Scanner(new BufferedReader(new FileReader(
"C:\\Users\\User\\Desktop\\asciiTracks.txt")));
while (checker != null) {
String pattern = "Array Start";
checker = scan.findWithinHorizon(pattern, 0);
if(checker.equals("Array Start")){
String pattern2 = "Array Size";
checker3 = scan.findWithinHorizon(pattern2, 300);
System.out.println(checker3);
if(checker3.equals("Array Size")){
checker4 =
Integer.parseInt(scan.findInLine("(10000|\\d{1,4})"));
System.out.println(checker4);
}else{System.out.println("no array size");}
System.out.println(checker4);
}else{System.out.println("no array size");}}}catch
(FileNotFoundException e) {}}}
This is the error i get on the console:
Exception in thread "main" java.lang.NullPointerException
at SearchingFilesMain.main(SearchingFilesMain.java:31)
This error occurs at this line of the code:
if(checker3.equals("Array Size")){
This makes no sense to me, as surely if checker3 is not equal to the text
it will simply move onto the else statement and go from there, rather than
just throw null pointer exception and stop the program? Am i wrong in
thinking this?
Anyone got any ideas on why this is happening?

No comments:

Post a Comment