Can someone look at this with a new set of eyes and see what I am missing.
I am new to Java and this is kicking my tail.
This is a simple inventory application. I am using an array to store the variables in the Compactdisk class, and sort on the name when done.
I implimented a sub class to that named CdwArtist class since I had never done it before. (I wanted to see inheritance in action)
I have my array now using the CdwAction class like I should, everything compiles fine, but when I run it I get a NullPointerException when I enter STOP and exit my loop.
It looks to me as if I am missing something with my array, possibly crossing the name and the artist somehow, but I have stared at this so long it is just looking like garbage to me. Any help would be welcome.
Inventory class
- import java.util.*;
-
-
public class Inventory
-
{// begin class Inventory
-
-
public static int maxlength = 0;
-
public static CdwArtist[] sort(CdwArtist[] cds)
-
{
-
Arrays.sort(cds, 0, maxlength);
-
return cds;
-
}
-
-
public static String toString(CdwArtist[] cds)
-
{
-
String toSend = "\n\n";
-
-
for(int i = 0; i < maxlength; i ++)
-
toSend = toSend + cds[i].getName() + "\n";
-
return toSend;
-
}
-
-
public static void main(String[] args)
-
{//begin method main
-
-
// create cd Array
-
CdwArtist[] cds = new CdwArtist[100];
-
-
-
float totalValue = 0;
-
-
Scanner input = new Scanner(System.in); // create scanner
-
-
// begin display method
-
System.out.print("Enter up to 99 CD Names or STOP to Exit: ");
-
String nameInput = input.nextLine(); //read cd name
-
-
maxlength ++;
-
-
for(int i = 1; i < cds.length && !nameInput.equalsIgnoreCase("STOP"); i++)
-
{// begin main While
-
cds[i] = new CdwArtist();
-
cds[i].setName(nameInput);
-
-
System.out.print("Enter CD Artist Name: "); // prompt for artist name
-
CdwArtist artist = new CdwArtist(input.nextLine());
-
-
System.out.print("Enter Price of this CD: "); // prompt for price
-
cds[i].setPrice(input.nextFloat()); // price input from user.
-
while (cds[i].getPrice()<= 0)
-
{// begin while
-
System.out.print("Price Must Be Greater Than Zero. Enter Price: ");
-
cds[i].setPrice(input.nextFloat()); // cd price loop from user.
-
} // End while
-
-
System.out.print("Enter CD Item Number: "); // prompt for cd item number
-
cds[i].setItemno(input.nextInt()); // cds item number input from user
-
-
System.out.print("Enter Number of these CDs in Stock: "); // prompt for cd stock
-
cds[i].setNstock(input.nextInt()); // cds in stock input from user
-
-
System.out.print("\n\nCD "+cds[i].getName()+", Item Number "+cds[i].getItemno()+","); // display name
-
System.out.printf(" is worth %c%.2f.",'$', + cds[i].getPrice());//
-
System.out.print("\nWe have "+ cds[i].getNstock()+" copies in stock,");
-
System.out.printf(" making our inventory for this cd worth %c%.2f.\n", '$', + cds[i].getValue()); //inventory value
-
-
if(cds[i].getValue() != -1) totalValue = totalValue + cds[i].getValue();
-
System.out.printf("Combined Inventory for all CDs is Worth %c%.2f.\n\n\n", '$', + totalValue);
-
-
System.out.print("Enter up to 99 CD Names or STOP to Exit: ");
-
input=new Scanner(System.in); // internal loop prompt
-
nameInput = input.nextLine(); //name input from user
-
-
maxlength ++;
-
} // End main While
-
-
//System.out.println(toString(cds));
-
System.out.println(toString(sort(cds)));
-
System.out.print("Ending Program.");
-
-
}// end method main
-
} // end class Payroll
-
then the compactdisk class I first set up (even though this should require no modification at all to impliment a subclass.
- import java.lang.Comparable;
-
-
public class Compactdisk implements Comparable
-
{// begin class
-
-
//InventoryCD class has 5 fields
-
private String name; // Name of cd
-
private float price; // price of cd
-
private int itemno; // item number of cd
-
private int nstock; // how many units in stock
-
private int i; // cd counter for array
-
private float value; // value for single cd inventory
-
-
-
//Compact disk class constructor
-
public Compactdisk()
-
-
// 4 fields need to be set up
-
{
-
name = "";
-
price = 0;
-
itemno = 0;
-
nstock = 0;
-
i = 0;
-
value = 0;
-
}
-
-
// set values
-
public void setName(String diskName)
-
{
-
name = diskName;
-
}
-
public void setPrice(float cdPrice)
-
{
-
price = cdPrice;
-
}
-
public void setItemno(int cdItemno)
-
{
-
itemno = cdItemno;
-
}
-
public void setNstock(int cdStock)
-
{
-
nstock = cdStock;
-
}
-
public void setValue(float cdValue)
-
{
-
value = cdValue;
-
}
-
public void seti(int Count)
-
{
-
i = Count;
-
}
-
-
-
// return values
-
public String getName()
-
{
-
return (name);
-
}
-
public float getPrice()
-
{
-
return (price);
-
}
-
public int getItemno()
-
{
-
return (itemno);
-
}
-
public int getNstock()
-
{
-
return (nstock);
-
}
-
public int compareTo(Object in)
-
{
-
return ((Comparable)name.toLowerCase()).compareTo((Comparable)((Compactdisk)in).getName().toLowerCase());
-
}
-
-
// returns indivudual inventory value for a disk
-
public float getValue()
-
{
-
return(price * nstock);
-
}
-
-
-
-
-
-
}// end class
-
and then my new subclass. everything worked fine before I did this.
- import java.util.*;
-
-
public class CdwArtist extends Compactdisk
-
{
-
-
// CdwArtist class adds one field
-
public String artist; // artist performing on cd
-
-
// Artist constructor
-
public CdwArtist()
-
{
-
artist = "";
-
}
-
-
public CdwArtist(String in)
-
{
-
artist=in;
-
}
-
-
// get value
-
public void setArtist(String in)
-
{
-
artist=in;
-
}
-
-
// return value
-
public String getArtist()
-
{
-
return (artist);
-
}
-
-
} //End Class
Anyone?