Question

My problem is that I can't find mistake in this code. I want to fill a field of my program(Set of strings) in constructor with Strings readen from a file.

`

public AnagramUserInput(){
    Set<String> result = new TreeSet<String>();
    Set<String> lexic = new TreeSet<String>();
    File lexicon = new File("C:/Users/Konstanty Orzeszko/Desktop/do testu/english_words_WIN.txt");
    try{
        BufferedReader br = new BufferedReader(new FileReader(lexicon));
        String word;

        while(((word = br.readLine()) != null)) {
            this.lexic.add(word);//Exception is throwned right in this line
         }
        br.close();
    }catch(IOException exc) {
        System.out.println(exc.toString());
        System.exit(1);
    }
}`

Could you tell me what is wrong/how to fix it? Thanks a lot.

Was it helpful?

Solution

this.lexic is evaluated to null. Note that this.lexis doesn't point to the constructor's local lexic variable, but to the instance's one.

If you want to add the String to the constructor's lexic variable, just get rid of the this keyword:

lexic.add(word);

OTHER TIPS

The only problem I see here is

this.lexic.add(word); // this.lexic

Remove the this. Because the constructor instantiates the class. Even before the object is created, you're tryin to use this, which is wrong.

More than likely you have another variable named lexic as a class instance variable. (The above code would not compile if this is not the case)

Therefore its likely you're shadowing the result variable. Replace

Set<String> lexic = new TreeSet<String>();

with

lexic = new TreeSet<String>();
this.lexic.add(word);

You are using this in constructor, that means, you are using the object during its creation. That is why you get NPE. remove this, it will work.

if you put filling part in a method also, that means

Set<String> lexic = new TreeSet<String>();

is also in same method, then also "this" will not work because "this" is not for local level variable. But in that case also, it will not give NPE but compiler error.

This code is working fine, you can check once :

public AnagramUserInput()
{
    Set<String> result = new TreeSet<String>();
    Set<String> lexic = new TreeSet<String>();
    File lexicon = new File("output.txt");
    BufferedReader br = null;
    try{
        br = new BufferedReader(new FileReader(lexicon));
        String word;

        while(((word = br.readLine()) != null)) {
            lexic.add(word);//Exception is throwned right in this line
         }

    }catch(IOException exc) {
        System.out.println(exc.toString());
        System.exit(1);
    }

    finally
    {
       if (br != null)
       {
            try
            {
                br.close();
            }
            catch (IOException e)
            {
               // TODO Auto-generated catch block
               e.printStackTrace();
            }
       }
    }

    System.out.println(lexic);
}

just make sure, file is on proper location as required.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top