Untitled
Untitled

Reputation: 113

Copying values of array

I'm trying to copy the value of pos even indexed value to words, an array. And I am getting a nullpointerexpection in this code

    for (String a : token)
    {
        temp = temp + " " + a;
        pos = a.split("[_\\s]+");

    }
    for (int i=0;i<pos.length;i=i+2)
    {
        int c=0;
        words[c]=pos[i]; //in this line
        c++;
    }

Upvotes: 0

Views: 29

Answers (2)

Christian Hapgood
Christian Hapgood

Reputation: 31

You need to make sure that your words array has been initialized with at least as many positions as the size of pos.length for example if you do not have the words array initialized with 7 spots for characters and pos.length == 8 then its going to point to a position in the words array that doesn't exist. Maybe try initializing the words array with more memory locations than you need? Also make sure you are taking into account the steps of 2 in the second for loop.

edit: maybe try initializing the array with values?

Upvotes: 0

Patricia Shanahan
Patricia Shanahan

Reputation: 26185

Given the NullPointerException, it is very likely that words is null. When you allocate it, you do need to make sure it is big enough, or you will get an ArrayIndexOutOfBoundsException.

Upvotes: 1

Related Questions