繁体   English   中英

我不知道为什么我的数组没有填写 Java

[英]I can't figure out why my array isn't filling here in Java

所以,arrays 绝对不是我的强项。 While testing this application in my driver class, I get the thrown exception Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Object.equals(Object)" because "this.order[x]" is null at ics240assignment1. ShoppingCart.add(ShoppingCart.java:37) at ics240assignment1.ShoppingCartDriver.main(ShoppingCartDriver.java:8) 在代码的这个区域中,我刚开始添加购物车永远不会填充任何东西,从而给出“空”结果。 至少,这就是我认为正在发生的事情。 我在这里做错了什么明显的事情吗?

public class ShoppingCart {
    private String customer;
    private GroceryItem order[];
    private int numItems;

    public ShoppingCart() { // must instantiate an array called GroceryItems
        numItems = 0;
        order = new GroceryItem[10];
        customer = "";
    }

    public ShoppingCart(String name) {
        this.customer = name;
    }

    public ShoppingCart(String name, int size) {
        order = new GroceryItem[size];
        this.customer = name;

    }

    public boolean add(String name, double price, int quantity) {
        // changes made here
        String name1 = name;
        double price1 = price;
        int quant = quantity;
        int tempPlace = 0;
        boolean found = false;
        boolean full = false;
        if(order == null) {
            order[numItems++] = new GroceryItem(name, price, quantity);
            return true;
        }
        for (int x = 0; x < order.length; x++) {
            if (order[x].equals(name)) {
                tempPlace = x;// store place in array
                found = true;
            }

            if (found == true) {
                // get current qty, add to it, then update it
                int tempQty = order[tempPlace].getQty();
                tempQty = tempQty + quantity;

                order[tempPlace].setQty(tempQty);
                full = false;

            }

            else if (found == false) {
                for (int y = 0; y < order.length; y++) {
                    if (order[y] == null) {
                        order[y] = new GroceryItem(name, price, quantity);
                        full = false;
                        break;
                    }

                }
                full = true;
            }
        }

        if (full == false) {
            return true;
        }

        if (full == true) {
            return false;
        }
        return false;
    }

    // added all this
    public int find(String name) {
        boolean found = false;
        int location = 0;
        GroceryItem tempItem = new GroceryItem(name, 0, 0);
        for (int x = 0; x < order.length; x++) {
            if (order[x].equals(tempItem)) {
                found = true;
                location = x;

            }
        }

        if (found == false) {
            return -1;
        }
        if (found == true) {
            return location;
        }
        return 0;
    }

    public double getTotalBeforeTax() {
        double total = 0.0;
        for (int x = 0; x < order.length; x++) {
            if (order[x] != null) {
                total += order[x].getPrice() * order[x].getQty();
            }
        }

        return total;
    }

    public double getTax(double taxRate) {
        double tax = 0;
           // Loops
           for(int i = 0; i < numItems; i++)
               // Calculates tax
               tax += (order[i].getQty() * order[i].getPrice()) * (taxRate / 100.0);
           // returns total tax
           return tax;
    }

    public int getNumGroceryItems() {
        int total = 0;
        for (int x = 0; x < order.length; x++) {
            if (order[x] != null) {
                total += 1;
            }
        }
        return total;
    }

    public int getTotalQuantity() {
        int total = 0;
        for (int x = 0; x < order.length; x++) {
            if (order[x] != null) {
                total += order[x].getQty();
            }
        }
        return total;
    }
    public String toString() {
        String output = "";
        output += customer;
        output += order;
        output += numItems;
        output += getNumGroceryItems();
        return output;
    }
}

驱动class供参考

public class ShoppingCartDriver {

    public static void main(String[] args) {
        ShoppingCart Randy = new ShoppingCart();
        ShoppingCart Sharron = new ShoppingCart();
        Randy.add("Eggs", 1.50, 1);
        System.out.println(Randy);

    }

}

两点:

因为您将“订单”数组大小初始化为 10(在默认构造函数下),所以当您遍历数组时,您将得到一些项目为 null。 您应该考虑使用 numitems 作为您的限制循环,或者使用可动态调整大小的替代结构,例如 ArrayList。

但是 - 而且你的数组永远不会填满,因为你的 if 语句检查 order==null,这总是错误的,因为 order 是一个数组。

数组就像一个小地址簿。 new GroceryItem[20]不会生产 20 件杂货。 它只是创建一个新的地址簿,上面有 20 页; 每个页面都有空间写一个指向单个杂货项目的指针。 这本书一开始是空白的:有 20 个参考文献的空间,但它总是以空白页开始( null值)。

order = new GroceryItem[10];

是的,所以,每当您创建购物车时,它的order价值将始终引用一个新创建的带有 10 个空白页的小地址簿。

你的构造函数

第一个构造函数创建一个新的 10 页的小地址簿,然后更新order值以引用这个地址簿(指向它)。

您的第二个构造函数不执行任何操作,这意味着order将保持null (指向任何内容;根本不存在通讯簿)。

您的第三个构造函数创建一个具有规定页数(通过参数提供)的地址簿,全部为空白。

这是你的第一个问题——第二个构造函数是奇怪的,这是没有意义的。

添加方法

if(order == null) {

如果您使用第二个构造函数创建了此 ShoppingCart 则为 true,否则不能为 null。 下一行是:

order[numItems++]

这是没有意义的。 []将“取消引用”-它将遵循引用。 当您对 null 值执行此操作时,您会得到一个空指针异常。 这里null - 您刚刚检查过:您可能必须编写类似: order = new GroceryItem[10]东西。 或者更有可能的是,您永远不想对order进行空检查,并且您总是希望将order设置为指向每个构造函数中的实际 GroceryItem 数组(即更新构造函数 2,并删除if检查,因为它没有意义)。

因此,如果你调用了第二个构造函数,这个 add 方法就会在这里崩溃。 如果你调用了第一个或第三个,这个if块被跳过了,我们 go on,用:

 for (int x = 0; x < order.length; x++) { if (order[x].equals(name)) {

同样的问题 - order[x]存在(这将是通过检查您的小地址簿中的第 x 页来查找通向 GroceryItem 的地址的指令。不幸的是,最初此页面是空白的。

. 也取消引用,因此 NullpointerException 将始终在此处发生。

简单的修复

Arrays 是您不应该使用的低级构造函数。 请改用List<GroceryItem> 这样的清单可以扩大和缩小。 没有大小和空指针的讨厌的事情。

硬修复

如果这是某种家庭作业,并且您必须在此处使用 arrays,请记住页面是空白的,您应该在取消引用之前检查它(使用.取消引用)。 您可以使用if (order[x].= null && order[x].getName().equals....

请注意,任何“真正的”java 项目(一个不是为了教学或探索的项目)都不应该这样做。 使用列表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM