3

I'm having a little trouble converting an array list to an array in java.

I have a class called Table which contains an array list called applicants which takes Strings. When I use my toArray() function it doesn't seem to convert the array list into an array.

I know this because when i run the listArray() function it gives the error:

java.lang.NullPointerException.

Here is the code. Any help would be greatly appreciated.

public class Table {
    public ArrayList<String> applicants;
    public String appArray[];

    public Table() {
        applicants = new ArrayList<String>();
    }

    public void addApplicant(String app) {
        applicants.add(app);
    }

    public void toArray() {
        int x = applicants.size();
        String[] appArray = new String[x];
        appArray = applicants.toArray(appArray);
    }

    public void list() {
        for (int i = 0; i < applicants.size(); i++) {
            System.out.println(applicants.get(i));
        }
    }

    public void listArray() {
        for (int i = appArray.length; i > 0; i--) {
            System.out.println(appArray[i]);
        }
    }
}
0

3 Answers 3

16

This is because you are shadowing the appArray field in your method here:

 int x = applicants.size();
 String[] appArray = new String[x];
 appArray = applicants.toArray(appArray);

You create a new local variable called appArray when you should be using the appArray field you already have declared in your class.

You should do

public void toArray()
{
    appArray = applicants.toArray(new String[0]);
}
Sign up to request clarification or add additional context in comments.

5 Comments

Can I ask as someone interested in this answer, why do you need to cast the ArrayList to a String[] type? As it is already this type, why won't applicants.toArray() work?
@AndrewMartin the function returns Object[] so you should cast it to your class type array.in that way the function will know the type of array to return. Another alternative would be using List#toArray(T[] array), look at the ArrayList#toArray code for an example: grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/…
This code throws java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String; I think you need to say appArray = applicants.toArray(new String[application.size()]);
@krico You're correct. I've fixed my code to use the overloaded toArray(T[]) method. I thought you were referring to the first snippet.
1
appArray = new String[applicants.size()];
int index = 0;
for(String app:applicants){
    appArray [index] = app;
    index++;
}

Comments

1

Because both your ArrayList and Array should only have strings you can do this.

appArray = applicants.toArray(new String[0]);

More information in the javadoc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.