java - Null Pointer Exception when Creating Object -


i wondering me figure out null pointer exception. i'm making program find anagrams of english word. i've created class englishdictionary that, when given length of english word along first letter, scans text file containing english words , selects add same length , contain first letter arraylist. here's code:

import java.io.filereader; import java.io.bufferedreader; import java.io.ioexception; import java.util.arraylist;  public class englishdictionary {      public englishdictionary(string firstletter, int wordlength) {         arraylist<string> words = new arraylist<string>();         processdict(firstletter, wordlength);     }      public void processdict(string firstletter, int wordlength) {         try {             filereader freader = new filereader("englishwords.txt");             bufferedreader reader = new bufferedreader(freader);             while (true) {                 string line = reader.readline();                 if (line == null) {                     break;                 } else {                     if (line.length() == wordlength) {                         words.add(line);                     }                 }             }         } catch (ioexception e) {             system.out.println("file not exist.");         }     }      public arraylist<string> getwords() {         return words;     }      public void printwords() {         (string word : words) {             system.out.println(word);         }     }      private arraylist<string> words;  } 

as can see, haven't yet added functionality of checking whether or not first letter in selected english words. when create englishdictionary object class, null pointer exception. says:

exception in thread "main" java.lang.nullpointerexception     @ englishdictionary.processdict(englishdictionary.java:23)     @ englishdictionary.<init>(englishdictionary.java:10)     @ main.main(main.java:6) 

i can't seem figure out coming from. can else see? in advance.

words not initialized correctly. initializing local variable words hiding class variable words.

do in constructor:

public englishdictionary(string firstletter, int wordlength) {   //arraylist<string> words = new arraylist<string>();  // creates local variable called 'words'; class member not initialized.   words = new arraylist<string>(); // initializes class variable.   processdict(firstletter, wordlength); 

}


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -