1

struggling a bit with something. I have built a proof of concept and googled but can't find reason.

  1. I am currently trying to use an ArrayList as a static property in a class to store series of entries. The only problem is that everytime I try add to the Totalentries arraylist I get a nullPointerError.

Would appreciate some guidance as to where I am going wrong?

My Class:

import java.util.ArrayList;


public class Competition {
private static ArrayList totalentries;

public Competition(){


}
public void newEntry(){
    totalentries.add("an Entry");
}

}

My Test Code:

public class testEntries {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Competition myComp=new Competition();
        myComp.newEntry(); //Null Pointer comes here!
        myComp.newEntry();
        myComp.newEntry();
        myComp.newEntry();
        myComp.newEntry();
        myComp.newEntry();

        myComp.toString();
    }

}
2
  • As noted by many below (let's see how many answer this thread!) -- you never instantiate the ArrayList. Why is it static to begin with? Commented Jan 8, 2012 at 18:53
  • This variable shouldn't be static. And its type should be List<String>. Commented Jan 8, 2012 at 18:54

4 Answers 4

1

You never instantiated totalentries in your Competition class.

You would need something like:

private static ArrayList totalentries = new ArrayList();

However, note that I would advise against keeping this "static". Otherwise, every "Competition" you create will be sharing the same list of entries, which is likely not what you really want.

Also, declare your types using interfaces, than instantiate with types. You may also want to use Generics here. So even better (and following standard naming conventions):

private List<String> totalEntries = new ArrayList<String>();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for clearing it up. I have done some more research, and now understand this better! Thanks :)
1

You never make an ArrayList. Try this:

private static ArrayList totalentries = new ArrayList();

though it would be better to use generics and get compile-time safety:

private static ArrayList<String> totalentries = new ArrayList<String>();

Since this list holds properties you wouldn't want it to be replaced so it would be even better if you were to define it like this:

private static final ArrayList<String> totalentries = new ArrayList<String>();

Really, though, none of these are good ideas because you could have multiple instances of your class changing totalentries at the same time. If that is your intent, that multiple Competitions use the one static totalentries for storage then you are better off keeping track of that data in a separate class.

If you are only using one Competition at a time then remove the static keyword.

1 Comment

Thanks for the amazing detailed help! I read up more and now see my mistake!
0

totalentries is not initialized and it points to null. Make it like this:

private static List<String> totalentries = new ArrayList<String>();

Comments

0

The list must be created prior to usage, try totalentries = new ArrayList();

You should also use List instead for the totalentries variable instead to allow exchanging te ArrayList with for example LinkedList.

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.