我从我编写的一些代码中收到空指针异常,我看不到异常的原因。这是我的代码:
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) {}}}
这是我在控制台上遇到的错误:
Exception in thread "main" java.lang.NullPointerException
at SearchingFilesMain.main(SearchingFilesMain.java:31)
此错误发生在这行代码:
if(checker3.equals("Array Size")){
这对我来说毫无意义,因为如果 checker3 不等于文本,它肯定会简单地移动到 else 语句并从那里开始,而不是仅仅抛出空指针异常并停止程序?我这样想错了吗?
有人对为什么会这样有任何想法吗?