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