this is the class for the objects that fill my array
class InventoryItem implements Comparable<InventoryItem>{
String name;
double price = 0.0;
int quantity = 0;
int itemCount = 0;
public InventoryItem(String newName, double newPrice, int newQuantity){
name = newName;
price = newPrice;
quantity = newQuantity;
itemCount++;
}
public String toString(){
String output = name + ", at " + Program4.moneyString(price) + " each.";
output += " We have " + getQuantity() + " of them.\n";
return output;
}
public String getName(){
return name;
}
public int getQuantity(){
return quantity;
}
public double getPrice(){
return price;
}
public int compareTo(InventoryItem other){
return getName().compareTo(other.getName());
}
}
and my call to sort the array
public static void sortByName(InventoryItem[] array){
Arrays.sort(array);
}
this gives me the errors
Exception in thread "main" java.lang.NullPointerException
at java.util.ComparableTimSort.binarySort(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at CSC120Program4.Program4.sortByName(Program4.java:97)
at CSC120Program4.Program4.processUserChoice(Program4.java:121)
at CSC120Program4.Program4.main(Program4.java:134)
I'm not quite experienced enough to know how to fix this or really what I'm doing wrong.
edit: array being built:
static InventoryItem[] inventoryItems = new InventoryItem[100];
public static void readAndStoreInventory(InventoryItem[] items, Scanner input){
String[] params = new String[3];
while(input.hasNextLine()){
String name = "";
double value = 0.0;
int quantity = 0;
params = (input.nextLine().split("#"));
name = params[0];
value = Double.parseDouble(params[1]);
quantity = Integer.parseInt(params[2]);
items[lastInUse + 1] = new InventoryItem(name, value, quantity);
lastInUse++;
}
input.close();
the file being read is as follows:
wooden baseball bat#3.67#10
rubber mallet#5.50#20
duffel bag#10.20#11
gold bar#9999.99#1
hitman rental#20000.00#99
minivan#15000.21#3
half full cup#2.00#10
half empty cup#10.00#10
lawn chair#12.34#100
used golf ball#0.99#2
cosby sweater#250.00#1
CSC120 cheatsheet#0.01#1