如何在java(OOP)中设置数组变量的属性,在第43行为什么o[0]。名称不起作用?

如何在java(OOP)中设置数组变量的属性,在第43行为什么o[0]。名称不起作用?,java,oop,Java,Oop,如何在java(OOP)中设置数组变量的属性?无法在for循环中设置o[]的属性,原因是什么?我正在尝试为o[0]设置名称,但o[0]。名称无效。为什么会这样?而且setter和getter也不起作用,你能帮我解释一下为什么不能在o[]中设置属性吗 import java.util.Scanner; public class Orders { private String name; private double price; private int quantity;

如何在java(OOP)中设置数组变量的属性?无法在for循环中设置o[]的属性,原因是什么?我正在尝试为o[0]设置名称,但o[0]。名称无效。为什么会这样?而且setter和getter也不起作用,你能帮我解释一下为什么不能在o[]中设置属性吗

import java.util.Scanner;

public class Orders {
    private String name;
    private double price;
    private int quantity;

    public void setName(String name){
        this.name= name;
    }
    public String getName(){
        return name;
    }
    public void setPrice(double price){
        this.price=price;
    }
    public double getPrice(){
        return price;
    }
    public int getQuantity(){
        return quantity;
    }
    public void setQuantity(int quantity){
        this.quantity=quantity;
    }

    public static void main(String[] args) {
        int num = 0;
        double sum=0;
        Scanner sc = new Scanner(System.in);
        System.out.println("how many rows of order: ");
        num = sc.nextInt();
        Orders[] o = new Orders[num];

        sc.nextLine();

        for(int i = 0;i<=o.length;i++){

            System.out.println("The name of the product: ");
            o[0].name=sc.nextLine();
            o[i].setName(sc.nextLine());
            System.out.println("Price of product: ");
            o[i].setPrice(sc.nextDouble());
            System.out.println("Quantity of product: ");
            o[i].setQuantity(sc.nextInt());
        }
        for(Orders a: o){
            System.out.println("Name: "+a.getName()+". Price: "+a.getPrice()
            +". Quantity :"+a.getQuantity());
            double totalprice= a.getQuantity()*a.getPrice();
            sum = sum + totalprice;
        }

        System.out.println("total price: "+sum);
    }
}
import java.util.Scanner;
公共阶级秩序{
私有字符串名称;
私人双价;
私人整数数量;
公共void集合名(字符串名){
this.name=name;
}
公共字符串getName(){
返回名称;
}
公共定价(双倍价格){
这个。价格=价格;
}
公开双价{
退货价格;
}
公共整数getQuantity(){
退货数量;
}
公共无效设置数量(整数数量){
这个。数量=数量;
}
公共静态void main(字符串[]args){
int num=0;
双和=0;
扫描仪sc=新的扫描仪(System.in);
System.out.println(“订单的行数:”);
num=sc.nextInt();
订单[]o=新订单[num];
sc.nextLine();

对于(int i=0;i您需要在将o[i]对象分配给其属性之前初始化它。
您的循环应该在到达数组长度之前终止,因为您从零开始迭代器。您的for循环应该如下所示:

for(int i = 0;i<o.length;i++){
        o[i] = new Orders();
        System.out.println("The name of the product: ");
        String name = sc.nextLine();
        o[i].setName(name);
        System.out.println("Price of product: ");
        double price = sc.nextDouble();
        o[i].setPrice(price);
        System.out.println("Quantity of product: ");
        int quantity = sc.nextInt();
        o[i].setQuantity(quantity);
        sc.nextLine();
        System.out.println("moveing to the next Order:");
    }

for(int i=0;i您可以共享从该代码执行中获得的堆栈跟踪或错误吗?“为什么这不起作用?”因为您做错了。如果您想了解有关您做错了什么的更具体信息,请提供有关它如何工作的更具体信息,例如您看到的错误消息或不正确的输出。嗨,Daniel,线程“main”中的异常java.lang.NullPointerException,在eclipse中查找没有问题,但是当我运行它时,它有一些错误,我不明白为什么不能为数组变量设置值,比如数组[i]。somethingHi ajb,线程“main”中的异常java.lang.NullPointerException,当我在eclipse中运行时,它显示了这一行,这是我第一次使用stackoverflow提问,我不知道如何回答,你能看到我吗?Hi Maged,在我添加o[i]=new Orders()之后;在for循环中并在eclipse中运行,它显示了这样的错误消息----线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException;我不知道如何修复它(int i=0;iHi Maged,谢谢你!!!!!它很有效,非常感谢,我是编程的一年级新生,哈哈哈我会努力学习!哦,那真的很好。@J.Doe如果这个答案对你有效,那么请接受它。这对我们双方都是一个很好的练习。提前谢谢,欢迎来到美丽的编程世界:)耶:祝你有一个愉快的一天