在找了几个小时之后,我仍然无法弄清楚Null-pointer-Exception的来源。此代码用于比较包含单词的两个.txt文件中的单词(每行1个)。来自" readlist.txt" -file的单词在比较之前将被解扰。如果找到匹配,则将未加扰的单词附加到输出String。问题在于: " scrambled_words [scrambled_iterator] = scrambled_line_yup;"
感谢您提前帮助我。
//Try any "char-combination"-method
public static void iterate(char[] chars, int len, char[] build, int pos, String wordlist_line_yup, String ausgabe) {
if (pos == len) {
String word = new String(build);
if ( word.equals(wordlist_line_yup)) {
output += word+",";
}
return;
}
for (int i = 0; i < chars.length; i++) {
build[pos] = chars[i];
iterate(chars, len, build, pos + 1,wordlist_line_yup, ausgabe);
}
}
//main method
public static void main (String[] args)throws IOException {
//Initialisiere Ausgabe String
String output = "";
//File Initialisation
File path1 = new File ("c:/users/xxx/Desktop/wordlist.txt");
File path2 = new File ("c:/users/xxx/Desktop/readlist.txt");
System.out.print(path1.exists()? "exists" : "doesnt exist");
System.out.print(path2.exists()? "exists" : "doesnt exist");
//Read the readList with scrambled words and put lines into string-array
BufferedReader scrambledreader = new BufferedReader(new FileReader("c:/users/xxx/Desktop/readlist.txt"));
String[] scrambled_words = {};
int scrambled_iterator = 0;
String scrambled_line;
while ((scrambled_line = scrambledreader.readLine()) != null) {
String scrambled_line_yup = "";
scrambled_line_yup += scrambled_line;
scrambled_line_yup.trim();
if(! "".equals(scrambled_line_yup)){
scrambled_words[scrambled_iterator] = scrambled_line_yup;
scrambled_iterator++;
}
}
scrambledreader.close();
//Read the wordList and compare to array with scrambled words
BufferedReader wordlistreader = new BufferedReader(new FileReader("c:/users/xxx/Desktop/wordlist.txt"));
String wordlist_line;
while ((wordlist_line = wordlistreader.readLine()) != null) {
String wordlist_line_yup = "";
wordlist_line_yup += wordlist_line;
wordlist_line_yup.trim();
if(! "".equals(wordlist_line_yup)){
for (String x : scrambled_words) {
if(! "".equals(x)){
char[] chars = x.toCharArray();
int len = chars.length;
iterate(chars, len, new char[len], 0, wordlist_line_yup, ausgabe);
}
}
}
}
wordlistreader.close();
//Output in Cmd
System.out.println(output);
}
}