添加一个目的是在对循环阵列。用set和get。 [英] adding an object to an array in for loop. with set and get.

查看:113
本文介绍了添加一个目的是在对循环阵列。用set和get。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IM上的对象添加到7对象的数组有点困惑。

我有一个循环,我想用3个参数添加项目对象。我已经使用set和get这一点。在fo​​r循环结束后,ID喜欢。新增项目对象的数组列表。当我尝试这样做,我得到一个错误:


  

异常线程main显示java.lang.NullPointerException
      在item.add(item.java:88)
      在homework3main.main(homework3main.java:38)


有在项目类中的线88和38主没有错误标志,所以我不知道如何纠正。

 公共类项目{公共静态INT X = 0;
公共静态字符串的setName;
公共静态双setPrice;
公共静态INT setPrioirty;
公众诠释优先= -1;
公共静态双价;
公共静态字符串名称;
私有静态项目[]列表;项目(){
    这个(-1,0,NULL);
    优先= -1;
    价= 0;
    NAME =没有名字。
} //默认构造函数。
公共项目(INT I,双J,串K){
    setitem(I,J,K); //构造函数3个参数。
}公共无效setitem(INT I,双J,串K){//设置项有3个属性。
    setPriority(ⅰ);
    setPrice(J);
    的setName(K);}公共无效setname可以(字符串K){//设置项各个属性。    // TODO自动生成方法存根// 378页
    名字= K;
    优先= -1;
    价= 0;
}
公共无效setPrice(双J){//在项目设置单独的属性。
    // TODO自动生成方法存根
    如果(J&℃,放大器;&放大器; J&→100){
        的System.out.println(错误:价格过高或过低);    }    其他
        价格= j的;    }公共无效setPriority(int i)以{//在项目设置单独的属性。
    // TODO自动生成方法存根
    优先=((I'GT; = 0&放大器;&放大器;我7;)I:0);
    }
公共双用getPrice(){
    返回的价格;}
公共字符串的getName(){    返回名称;}
公共双getPriority(){
    返回优先;}//蚀让我创建这个方法时,我想添加itemObject到列表
 公共静态无效的添加(项目itemObject){     的System.out.println(输入项目+ X);
    如果(X 7;)
    {
        列表[X] = itemObject;
    的System.out.println(项目补充索引+ X);    X ++;
    }
 }    }

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

 公共类homework3main扩展项目{公共静态无效的主要(字串[] args){
    项目列表[] =新项目[7]; //数组对象
    扫描仪键盘=新的扫描仪(System.in);
    为(中间体X = 0; X&下; List.length的数字; X ++){
        项目itemObject =新项目(setPrioirty,setPrice,setname可以);
        // 3个变量创建新的对象,名称,价格,优先        //列表[X] =新项目(); //这是正确的?
        的System.out.println(输入你想添加到您的列表中的项目);
        列表[X] .setName = keyboard.next();        的System.out.println(输入一口价);
        扫描仪pricedouble =新的扫描仪(System.in);
        列表[X] .setPrice = pricedouble.nextDouble();        的System.out.println(请输入项目的优先权);
        扫描仪priorityint =新的扫描仪(System.in);
        列表[X] .setPrioirty = priorityint.nextInt();        //项目itemObject =新项目(setPrioirty,setPrice,setname可以);        列表[X]。新增(itemObject);
    }


解决方案

的方法是错误的地方来初始化项目的数组。在这里,

 项目列表[] =新项目[7];

您已经声明了一个本地阵列本身就是一个名为列表。相反,初始化列表 项目在其构造或声明的一部分。的实例变量

此外,我看不出有任何理由与方法类, homework3main ,应该扩展项目。此外,Java中的命名约定是在命名类的开头大写字符,例如项目 Homework3Main

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);


    } 

解决方案

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.

这篇关于添加一个目的是在对循环阵列。用set和get。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆