0

After looking for hours I still can't figure out where the Null-pointer-Exception is originating. This code is supposed to compare the words from two .txt-files containing words (1 per line). The words from the "readlist.txt"-file are to be unscrambled before comparison. If a match is found the unscrambled word is appended to the output String. The problematic line is: "scrambled_words[scrambled_iterator] = scrambled_line_yup;"

Thanks for helping me in advance.

//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);     
}

}

BigJ4vage
  • 11
  • 2
  • 1
    Put the exception you received on your question. – Jorge Campos Feb 06 '16 at 16:56
  • I think `if(!"".equals(x)) {...}` is a bit weird. You could use `if(!x.isEmpty()) {...}` instead. – pzaenger Feb 06 '16 at 16:59
  • Don't look, check. Add explicit checks just before where the exception is being thrown to see what variable is null. Add more checks to your code to trace the offending null pointer back to its source. Help the computer to help you find the error. Remove the checks once you have fixed the error of course. – rossum Feb 06 '16 at 17:00
  • I guess that `String[] scrambled_words = {};` and later on `scrambled_words[scrambled_iterator] = scrambled_line_yup;` won't work. – pzaenger Feb 06 '16 at 17:06
  • Can't you debug it with an ide? Anyway if the line is the one you tell the problem must be the array that maybe it is not initialized correctly – Jkike Feb 06 '16 at 17:07
  • Search this site for "how to read a stack trace." – Kevin Krumwiede Feb 06 '16 at 17:18
  • 1
    @pzaenger `if(!"".equals(x))` is recommended since it avoid NPE, in your suggestion if `x` is null then you have an NPE! – Jorge Campos Feb 06 '16 at 19:46
  • 1
    @JorgeCampos Thanks for your comment. Didn't think about this and you are right. It is good to know; so I have learned something to pay attention for :) I won't delete my comment since others might find this useful as well. – pzaenger Feb 06 '16 at 19:55
  • @pzaenger always glad to help and even more when a suggestion is so well accepted :) – Jorge Campos Feb 07 '16 at 13:35

0 Answers0