I posted this a couple of days ago, but I still need help. I tried to fix the null pointer error, but have not been successful. I am getting:
Exception in thread "main" java.lang.NullPointerException
at testproduct.Product.getTotalInvValue(Product.java:40)
at testproduct.Product.printInventory(Product.java:53)
at testproduct.TestProduct.main(TestProduct.java:57)
I think it has to do with the way my getTotalInvValue method is written. Also, I have a method for sorting, but I do not know how to write the code in my TestProduct application. I have tried several codes like items.itemsort();, but it keeps saying that method can not be found. Please help!
CODE
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testproduct;
import javax.swing.JOptionPane;
/**
*
* @author shannon.singleton
*/
public class Product
{
int inventorySize = 15;
protected Product items[] = new Product[inventorySize];
public void addProduct(Product item) {
for (int i = 0; i < inventorySize; i++) {
if (items[i] == null) {
items[i] = item;
return;
}
}
}
public double getTotalInvValue()
{
double sumOfInventory = 0;
for (Product item : items)
{
sumOfInventory += item.getProductValue();
}
return sumOfInventory;
}
public void printInventory()
{
for (Product item : items)
{
String str = item.toString() + " Item Number:" + item.getItemNum();
str += " Units In Stock:" + item.getUnitsInStock();
str += " Value:" + item.getProductValue() + "\nThe total DVD inventory collection is: " + item.getTotalInvValue(); //
JOptionPane.showMessageDialog(null, str, "Title", JOptionPane.PLAIN_MESSAGE);
}
}
protected String productName;
protected int itemNum;
protected int unitsInStock;
protected double unitPrice;
public Product(String dvd, int number, int stock, double price)
{
productName = dvd;
itemNum = number;
unitsInStock = stock;
unitPrice = price;
}
public Product()
{
productName = " ";
itemNum = 0;
unitsInStock = 0;
unitPrice = 0;
}
// String representation of the product
@Override
public String toString()
{
return productName + " Price:" + unitPrice;
}
public double getProductValue()
{
return (double) unitsInStock * unitPrice;
}
public String getproductName()
{
return productName;
}
public void setProductName(String strProductName)
{
productName = strProductName;
}
public void setUnitPrice(double strUnitPrice)
{
unitPrice = strUnitPrice;
}
public double getUnitPrice()
{
return unitPrice;//retrieves unit price
}
public void setUnitsInStock(int strUnitsInStock)
{
unitsInStock = strUnitsInStock;//sets product name
}
public int getUnitsInStock()
{
return unitsInStock;//retrieves units in stock
}
public void setItemNum(int strItemNum)
{
itemNum = strItemNum;//sets product name
}
public int getItemNum()
{
return itemNum;//retrieves item number
}
public static void sortItems(Product items[])
{
String test, itemName;
Product prodTemp;
for(int i=0; i<items.length; i++)
{
test = items[i].toString();
for(int j=i; j<items.length; j++)
{
itemName = items[j].toString();
if (itemName.compareToIgnoreCase(test) < 0)
{
prodTemp = items[i];
items[i] = items[j];
items[j] = prodTemp;
}
}
}
}
}
CODE
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testproduct;
/**
*
* @author shannon.singleton
*/
public class Subclass extends Product
{
protected String movieActor;
public Subclass(String productName, int itemNum, int unitsInStock, double unitPrice, String actor)
{
// Pass on the values needed for creating the Product class
super(productName, itemNum, unitsInStock, unitPrice);
movieActor = actor;
}
public Subclass()
{
productName = " ";
itemNum = 0;
unitsInStock = 0;
unitPrice = 0;
movieActor = " ";
}
public void setActor(String strMovieActor)
{
movieActor = strMovieActor;
}
public String getActor()
{
return movieActor;
}
@Override
public double getProductValue()
{
return super.getProductValue() * 1.05;
}
public double getRestockingFee()
{
return super.getProductValue() * .05;
}
@Override
public String toString()
{
return super.toString() + " Actor:" + movieActor;
}
}
CODE
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package testproduct;
/**
*
* @author shannon.singleton
*/
public class TestProduct {
/**
* @param args the command line arguments
*/
public static void main(String[] arg)
{
Subclass item1 = new Subclass("Indiana Jones", 23457, 12, 14.99, "Harrison Ford");//assigns values to items
Subclass item2 = new Subclass("The Hulk", 67842, 17, 14.99, "Edward Norton"); //assigns values to items
Subclass item3 = new Subclass("Iron Man", 77089, 10, 14.99, "Robert Downey JR."); //assigns values items
Subclass item4 = new Subclass("Spiderman 3", 22547, 8, 10.99, "Tobey Maguire"); //assigns values to items
Subclass item5 = new Subclass ("Superman Returns", 40439, 11, 11.99, "Brandon Routh");//assigns values to items
Subclass item6 = new Subclass("Spiderwick Chronicles", 50459, 9, 12.99, "Freddie Highmore"); //assigns values to items
Subclass item7 = new Subclass ("Kung Fu Panda", 47865, 14, 17.99, "Jack Black"); //assigns values to items
Subclass item8 = new Subclass ("Speed Racer", 23969, 6, 15.99, "Emile Hirsch");//assigns values to items
Subclass item9 = new Subclass ("Get Smart", 60571, 18, 19.99, "Steve Carrell"); //assigns values to items
Subclass item10 = new Subclass ("Batman The Dark Knight", 18590, 19, 19.99,"Christian Bale");//assigns values to items
Subclass items = new Subclass();//creates object items
items.addProduct(item1);
items.addProduct(item2);
items.addProduct(item3);
items.addProduct(item4);
items.addProduct(item5);
items.addProduct(item6);
items.addProduct(item7);
items.addProduct(item8);
items.addProduct(item9);
items.addProduct(item10);
items.printInventory();//prints inventory list
System.out.println();
}
}