问题
Im a little confused on adding objects to an array of 7 objects.
I have a for loop and I want to add an item object with 3 arguments. I have used set and get for this. At the end of the for loop, id like to .add item object to the array list. When I try to do this I get an error:
Exception in thread "main" java.lang.NullPointerException at item.add(item.java:88) at homework3main.main(homework3main.java:38)
There are no errors flags on the lines 88 in item class and 38 in main, so I dont know how to correct.
public class item {
public static int x = 0;
public static String setName;
public static double setPrice;
public static int setPrioirty;
public int priority=-1;
public static double price;
public static String name;
private static item[] list;
item(){
this(-1,0, null);
priority=-1;
price=0;
name="No Name yet.";
}// default constructor.
public item(int i, double j, String k) {
setitem(i,j,k);//constructor with 3 arguments.
}
public void setitem (int i, double j, String k){// setting item with 3 attributes.
setPriority(i);
setPrice(j);
setName(k);
}
public void setName(String k) {//setting individual attributes in item.
// TODO Auto-generated method stub //page 378
name=k;
priority=-1;
price=0;
}
public void setPrice(double j) {//setting individual attributes in item.
// TODO Auto-generated method stub
if (j<0&&j>100){
System.out.println("Error: price is too low or high");
}
else
price=j;
}
public void setPriority(int i) {//setting individual attributes in item.
// TODO Auto-generated method stub
priority =((i>=0&&i<7)?i:0);
}
public double getPrice(){
return price;
}
public String getName(){
return name;
}
public double getPriority(){
return priority;
}
// eclipse made me create this method when I wanted to "add an itemObject to the list"
public static void add(item itemObject) {
System.out.println("Enter an item"+x);
if (x<7)
{
list[x]=itemObject;
System.out.println("Item added at index " + x);
x++;
}
}
}
---------------------------------main
public class homework3main extends item {
public static void main(String[] args) {
item list[]=new item [7]; // array object
Scanner keyboard= new Scanner(System.in);
for(int x=0; x<list.length;x++){
item itemObject=new item (setPrioirty,setPrice,setName);
//creating new object with 3 variables, name, price, priority
//list[x]=new item();// is this right?
System.out.println("Enter an item you want to add to your list");
list[x].setName=keyboard.next();
System.out.println("Enter a price");
Scanner pricedouble= new Scanner(System.in);
list[x].setPrice=pricedouble.nextDouble();
System.out.println("Enter the priority of the item");
Scanner priorityint= new Scanner(System.in);
list[x].setPrioirty=priorityint.nextInt();
//item itemObject=new item (setPrioirty,setPrice,setName);
list[x].add(itemObject);
}
回答1:
The main
method is the wrong place to initialize an item
's array. Here,
item list[]=new item [7];
you have declared a local array that is itself called list
. Instead, initialize the list
instance variable of item
in its constructor or as part of the declaration.
Additionally, I don't see any reason why your class with the main
method, homework3main
, should extend item
. Plus, the naming convention in Java is to name classes starting with an uppercase character, e.g. Item
and Homework3Main
.
来源:https://stackoverflow.com/questions/19232472/adding-an-object-to-an-array-in-for-loop-with-set-and-get