在for循环中向对象添加对象。与集合和得到。

时间:2013-10-07 18:58:10

标签: java arrays setter getter

我对将对象添加到7个对象的数组感到困惑。

我有一个for循环,我想添加一个带有3个参数的item对象。我已经使用了set并得到了这个。在for循环结束时,id喜欢将.add项目对象添加到数组列表中。当我尝试这样做时,我收到一个错误:

  

线程“main”java.lang.NullPointerException中的异常       在item.add(item.java:88)       在homework3main.main(homework3main.java:38)

项目类中的88行没有错误标记,main中有38个错误标志,所以我不知道如何纠正。

   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++;
    }








 }

    }

---------------------------------主

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 个答案:

答案 0 :(得分:2)

main方法是初始化item数组的错误位置。这里,

item list[]=new item [7];

您已声明一个本身名为list的本地数组。相反,在其构造函数中初始化list item实例变量或作为声明的一部分。

此外,我认为您使用main方法homework3main的课程延伸item的原因有任何理由。另外,Java中的命名约定是命名以大写字符开头的类,例如ItemHomework3Main