你在这一行上有一个NullPointerException:
String word = sc.next();这意味着您的Scanner sc未初始化。如果您没有找到该文件并且抛出了FileNotFoundException,则会发生这种情况。您无法查看是否因为您在catch块中没有执行任何操作而抛出此类异常。
尝试这个 :
public static void exp(String uttryck){
int counter = 0;
File fil = new File("Walta_corpus1.txt");
Scanner sc = null;
try{
sc = new Scanner(fil);
String word = sc.next();**
Pattern pattern = Pattern.compile(uttryck);
Matcher matcher = pattern.matcher(word);
while(word != null){
if(matcher.find()){
counter++;
System.out.println(word);
}
if(sc.hasNext()){
word=sc.next();
matcher = pattern.matcher(word);
} else
word = null;
}
System.out.println(counter);
} catch(FileNotFoundException foo){
foo.printStackTrace();
}
}我认为拥有空的catch块是一个非常糟糕的主意。尝试至少打印一个打印件,以了解是否有异常。