-2

I have local String array and i try to fill it with the returned tokens from Stringtokenizer.nexttoken

when i was declaring the string array as local variable i got i warning

Null pointer access: The variable words can only be null at this location

so i make the sting array as field in the class and i instantiate it

like this

 String[] words =new String [12000];

the warning disappear but i still get the same exception

StringTokenizer st=new StringTokenizer(text);


int j=0;
while(st.hasMoreTokens())
{
  words[j++]=st.nextToken();    

}

i use callable and future to execute the part of code that contain this block

and the exception that i get it is

java.lang.NullPointerException
java.util.concurrent.ExecutionException:

{the code work perfectly when i use split method to get the tokens array}

Antwan
  • 3,837
  • 9
  • 41
  • 62
  • 1
    What is the value of `text`? – Mike Koch May 15 '14 at 16:58
  • it's take it's value from text file and i'm sure that is not null – Antwan May 15 '14 at 17:03
  • This code should work fine providing the variable text isn't null. Are you sure you're reading from the text file correctly? I have a feeling that you need to provide more context (more code) in order for your problem to be properly evaluated. – wickstopher May 15 '14 at 17:10
  • yes i'm sure i tried to debug my code and i saw that text is not null beside that i used text.split and it's work – Antwan May 15 '14 at 17:10
  • Do you, by any chance, initialize the words array in one thread, and use it in another? Could you show us what exactly does the callable do and how you invoke it? – Ordous May 15 '14 at 17:18

1 Answers1

0

My problem fixed but replacing the String array with the arraylist<String>

while(st.hasMoreTokens())
    {
     words.add(st.nextToken()); 

    }
Antwan
  • 3,837
  • 9
  • 41
  • 62