1
public void populatePrizeArraylist()
{
    ArrayList <Prize> prizeArrayList = new ArrayList <Prize>();
    // The name of the file which we will read from   
    File filename = new File("prizes.txt");

    try 
    {   
        // Prepare to read from the file, using a Scanner object
        FileReader inputFile = new FileReader(filename);

        Scanner parser = new Scanner(inputFile);
        // Read each line until end of file is reached
        while (parser.hasNextLine())
        {
            // Read an entire line, which contains all the details for 1 prize
            String readFile = parser.nextLine();
            String delimiter = ","; 
            String[] fileList = readFile.trim().split(delimiter);

            Prize newPrize = new Prize();

            newPrize.setPrizeName(fileList[0]);
            newPrize.setPrizeWorth(Integer.parseInt(fileList[1]));
            newPrize.setPrizeCost(Integer.parseInt(fileList[2]));

            this.prizeArrayList.add(newPrize);

        }
    }
}

What am I doing wrong here? I have created a new ArrayList then a new object and set some values to the name variable of that object but when I try to add that object to my arraylist i get a null point exception error

3
  • 1
    Are you sure you get a run-time exception? Your code, as it is, will not compile since you are trying to invoke a non-existent setGoodName() method on the ArrayList instead of your object newItem Commented May 23, 2015 at 2:25
  • hi @kaykay i have put more of the code, are you able to work out what is wrong Commented May 23, 2015 at 2:42
  • This code is totally different from the snippet you had posted earlier. Please consider making your question clearer. Imagine if someone asks you to debug some code, what information would you expect from them Commented May 23, 2015 at 2:49

3 Answers 3

3

This variable declaration is inside the method scope

ArrayList <Prize> prizeArrayList = new ArrayList <Prize>();

and you are trying to add elements using

this.prizeArrayList.add(newPrize);

so I am sure there is a class level variable "prizeArrayList" defined which is not yet initialized and you are trying to add to it and getting NPE.

You have to use

prizeArrayList.add(newPrize);
Sign up to request clarification or add additional context in comments.

5 Comments

how can I initialise it?
looking at your method, you don't want to add to class level variable. Just remove "this" from the line where you add the newPrize to prizeArrayList.
ok that worked but now I can not access it from any other method as the prizeArrayList is not a class level variable, it just creates it inside the method scope and then disappears?
Then let's initialize your class level variable. Use new ArrayList <Prize>(); to assign an empty list to your class level variable. You don't need the method scope prizeArrayList so just go and remove that line from method (the 1st one).
use this.prizeArrayList.add(newPrize);. Your original code will work.
2

If I read your code right. You are populating your array list inside a void function. Array list is created inside that function locally. If you don't return that array list somewhere that array list is deleted after your function endas.

Aditional advice, use constructors it is bad habit to create a class through a default constructor then set its properties to some value through methods.

3 Comments

thanks @Spidey definitely learning a lot here. So what would you recommend instead then?
@Tarinkot Use custom constructors they will allow you to add values for your properties inside a parameter. In case you are not familiar with them I recommend a good tutorial page on java.
@Tarinkot regarding the main issue I recommend returning your array list wherever you need to use it.
2
goodsArrayList.setGoodName("Pants");
goodsArrayList.add(newItem);

is wrong because you need to call setGoodName() to newItem try this:

newItem.setGoodName("Pants");
goodsArrayList.add(newItem);

1 Comment

hi jake, it compiles ok but i get a null pointer exception error

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.