I am trying to complete an assignment for an introductory java class and need some help in getting past a "Exception in thread "main" java.lang.NullPointerException
at Week5Assignment.PartyStock.main(PartyStock.java:70)" error. I'm sure I've forgotten to initialize something but can't seem to find what it is. Any help that you can provide would be a great help. Here is the code:

import java.util.Scanner; //import the Scanner utility

public class PartyStock  //Begin class for this project

{

    public static void main (String[] args) //Beginning of main method
    {
        
        Partysupply[] pippo = new Partysupply [20]; //create the pippo array with 20 as the length of array
        
        pippo[0] = new Partysupply(); //class used to add to the array
        pippo[0].getItem();
        pippo[0].getQuant();
        pippo[0].getPrice();
        pippo[0].getItemno();
        pippo[0].getLoanAmount();
        
        int i = 0; // declare the counting integer
        double loanAmount = 0; //declare total cost variable
        
        Scanner input = new Scanner (System.in); //create "input" Scanner
        
        System.out.print("Ready to plan a party? Let's start with your name:");
        String partyGuy = input.next();
        
        // Ask for initial input that can then e sued in the "while" loop
        System.out.printf ("OK %s lets get started\n\n",partyGuy);
        System.out.print ("Enter the party item to be cataloged or STOP to end: ");
        String PartyItem = input.next();
        
        while (!PartyItem.equalsIgnoreCase("stop")) //Starts the loop to continue collecting input
            {
        
            pippo[i] = new Partysupply();

            pippo[i].setItemno (i + 1);  //NEED TO REMEMBER TO SET THE ITEM NUMBER AS i + 1
            pippo[i].setItem (PartyItem);
            
            System.out.print ("How much does it cost?: $");
            pippo[i].setPrice (input.nextDouble());
            

            System.out.print ("How many do you want?: ");
            pippo[i].setQuant (input.nextInt());

            if (pippo[i].getQuant() > 10) // Just for fun
                {
                System.out.println("Holy crap dude, this party is going to rock!!");
                } //end if statement
            
            i++; // Add to the counter for next array input

            System.out.printf ("OK, we're getting there %s \n\n",partyGuy);
            System.out.print ("Enter the next item to be cataloged or STOP to end:  ");
            PartyItem = input.next();

            } // end "while" loop

        System.out.printf("OK %s, here is what You said you need\n\n", partyGuy);

        for (int count = 0; count < (i +1); count++ )
{
System.out.printf("Item #%d %s, Quantity %d at $%.2f for a total of $%.2f\n",pippo[count].getItemno(),pippo[count].getItem(),pippo[count].getQuant(),pippo[count].getPrice(),pippo[count].getValue());
}


 //       System.out.printf("\n\nDamn %s,  you're going to have to get a loan for this one!!\n", partyGuy);
   //     System.out.printf("your total bankroll for this party will be $%.2f",pippo[i].getLoanAmount());
        
        
    }// End the main method

}// End PartyStock class

and the next class is:

public class Partysupply // Start of class to be used by PartyStock for input
{

  String Item;
  double Price;
  int Quant;
  int i;
  int ItemNo;
  double currentCost = 0;
  double partyLoan;

          public Partysupply() // Starting up the constructor
            {

      Item = "";
      Price = 0;
      Quant = 0;
      ItemNo = 0;
      i = 0;
      currentCost = 0;
      partyLoan = 0;

            } // End constructor

  public void setItem (String itemName) //Sets name value
  {

            Item = itemName;

  } //End Set name

  public void setPrice (double itemPrice) //Sets price value
  {

            Price = itemPrice;

  } //End set price

  public void setItemno (int itemNumber) //Sets Item number
  {

           ItemNo = itemNumber;

  }

  public void setQuant (int itemQuant) //Sets item quantity
  {

            Quant = itemQuant;

  } // End set quantity

  public void setCost (double partyCost) // Keeps a running tally of current cost
  {

            currentCost = partyCost;
            partyLoan = partyLoan + currentCost;

  } // End tally


  public String getItem()// Returning the Item name
  {

            return Item;

  } //

  public double getPrice() // Returning the item price
  {

            return Price;

  } //

  public int getItemno() // Returns the inventory number
  {

            return ItemNo;

  } //

  public int getQuant() // Returning the item quantity
  {

            return Quant;

  } //

  public double getValue() // Returns the total value for any given tem
  {

            return (Price * Quant);

  } //

  public double getLoanAmount() // Returns the total value of the party supplies
  {return partyLoan;}

}

Thanks again
Pippo

Partysupply[] pippo = new Partysupply [20];

pippo[0] = new Partysupply();

You need to initialize all the elements of the array. With your code, you initialize the pippo[0] but the pippo[1], pippo[2], pippo[3] are null.

Don't forget that when you do this: pippo = new Partysupply [20]; You initialize the array, not the elements of the array.

And I believe that these calls are not needed:

pippo[0] = new Partysupply(); //class used to add to the array

[B]        pippo[0].getItem();
        pippo[0].getQuant();
        pippo[0].getPrice();
        pippo[0].getItemno();
        pippo[0].getLoanAmount();[/B]

Notwithstanding the validity of the above, you cannot simply create the array at the beginning as the program implies you do not know the number of records until the last one is indicated by entering Stop. Your own approach of creating a new variable each time to add to the array is correct.

The problem occurs in the values of index used. The input index 'i' is incremented after the record is saved and the next item requested or the end indicated. When stop is entered the loop exits without creating any new variable or extending the array, so the value of i is 1 beyond the last array element. In the printout you add 1 to i to ensure the for loop index reaches the last value of i which has no entry in the data. This is where the error will occur.

Comments
Thanks for correcting me

Notwithstanding the validity of the above, you cannot simply create the array at the beginning as the program implies you do not know the number of records until the last one is indicated by entering Stop. Your own approach of creating a new variable each time to add to the array is correct.

The problem occurs in the values of index used. The input index 'i' is incremented after the record is saved and the next item requested or the end indicated. When stop is entered the loop exits without creating any new variable or extending the array, so the value of i is 1 beyond the last array element. In the printout you add 1 to i to ensure the for loop index reaches the last value of i which has no entry in the data. This is where the error will occur.

You are right, I didn't see that part of the code: pippo[i] = new Partysupply(); in the loop.

Good job taking some time to look it up

This article has been dead for over six months. Start a new discussion instead.