Java 删除元素数组会导致NullPointerException

Java 删除元素数组会导致NullPointerException,java,Java,我是Java的初学者。我得做购物车。但我一直坚持删除方法 请帮帮我 我试着用移除的方法。但是它不起作用 Customer.java ShoppingCart.java 我想删除元素数组。但产出是有限的 线程“main”java.lang.NullPointerException中的异常 如果编译器工作正常,我该怎么办。我认为remove方法是一个问题。NullPointerException可能是由于这行if(cart[I].getTitle().equals(title))引起的,而不是通过访

我是Java的初学者。我得做购物车。但我一直坚持删除方法

请帮帮我

我试着用移除的方法。但是它不起作用

Customer.java ShoppingCart.java 我想删除元素数组。但产出是有限的

线程“main”java.lang.NullPointerException中的异常


如果编译器工作正常,我该怎么办。我认为remove方法是一个问题。

NullPointerException可能是由于这行
if(cart[I].getTitle().equals(title))
引起的,而不是通过访问
cart
对象本身,而是调用
getTitle
返回值上的
equals
方法。在比较之前,请检查以确保返回值不为空:

if (cart[i] != null)
{
    /* Assign the value to a local variable and then
     * check if it's null before accessing it.
     */
    String value = cart[i].getTitle();
    if (value != null && value.equals(title)) {
        totalPrice -= cart[i].getPrice();
        cart[i] = null;
        itemCount -= 1;
        found = true;
    }
}

在任何情况下,您都应该使用某种调试技术来确定到底是什么导致了错误。了解有关在Java中为Eclipse和IntelliJ进行调试的更多信息。

出现Java.lang.NullPointerException是因为您删除了购物车中的第一个项目并将其设置为null,而不是在调用shopCart.toString()时尝试从Customer.Java通过toString方法访问它

因此,
c1.getShopCart()之前。从购物车(“哈利·波特”)中移除项目您有:
[《哈利波特》、《艾比路》、《阿甘正传》]在你的车里

之后
c1.getShopCart().removietemfromCart(“哈利波特”)您有:

[null,“Abbey Road”,“Forrest Gump”]您正在访问购物车[0]上的空指针。

我不相信stackoverflow是来解决您的家庭作业的。但是无论如何,你可以从stacktrace开始,回溯到空指针实际所在的位置,然后决定为什么会发生这种情况。我完全同意上面的评论,在寻求帮助之前,你应该试着在调试代码方面投入更多的精力。如果您不熟悉调试,请查看我的答案以了解更多详细信息。@JoãoCosta感谢您回答我的问题。但有一种问题我可以选择。我选择了在家庭作业问题上需要帮助。所以我认为我用stackoverflow询问家庭作业问题并不重要。为什么不呢?对不起,我不知道stacktrace和backtrack。这不是关于stackoverflow的问题,而是关于你发布问题的方式。在我看来,如果你问如何调试当前的问题,而不是如何解决
NullPointerException
,答案总是一样的,那么这个问题会更好:找出代码的哪一部分导致了调试。@Matthew Ah。。好啊非常感谢。实际上,我知道为什么会产生NullPointerException,也就是说,如果cart[I]为null,我就不应该访问getTitle()。但我不知道如何更改我的代码。。这正是我想问的。是 啊你说得对。我同意。对不起,你知道我没有用stackoverflow写太多的问题。我应该把问题写得更具体些。谢谢你的建议。我下次再做。你的回答对我很有帮助!非常感谢。
public class ShoppingCart {
    private int itemCount;
    private double totalPrice;
    private static int capacity;
    private Item[] cart;
    public ShoppingCart()
    {       
        capacity = 5;
        itemCount = 0;
        totalPrice = 0.0;
        cart = new Item[capacity];
    }

    public void addBooktoCart(String title, String description, double price, int pageCount) {
        if (itemCount < 5) {

        Item item = new Book(title, description, price, pageCount);
        totalPrice += price;
        cart[itemCount] = item;
        itemCount += 1;       
        } 
        else {
            System.out.println("The maximum number that you can input is 5." +
                     "You cannot add item anymore");
        }

    }
//This remove method may be a problem.
    public void removeItemFromCart(String title) {
        boolean found = false;
        for (int i = 0; i < cart.length; i++) {            
            if (cart[i] != null)                 
               {
                   if (cart[i].getTitle().equals(title)) {
                   totalPrice -= cart[i].getPrice();
                   cart[i] = null;
                   itemCount -= 1;
                   found = true;
                   }
               }
        }
if(found != true) {

            System.out.println("Item is not found in cart. Nothing removed");

        }
    }
    @Override
    public String toString() {
        String contents = "";

        for (int i = 0; i < itemCount; i++)
        {
            contents += cart[i].toString() + "\n\n";
        }

        String strDouble = String.format("%.2f", totalPrice);

        contents += "The total price is: $" + strDouble + "\n\n"
                + "How many items do you have in your shopping cart? \n"
                + "There are " + this.itemCount + " items in my shopping cart. "+ "\n";
        return contents;

    }
}

public class Test {
        public static void main(String[] args) {

        Customer c1 = new Customer("12345", "Hyun Min", "Lim");
        c1.getShopCart().addBooktoCart("Harry Potter", "Fantasy Genre", 10.99, 309);
        c1.getShopCart().addCDtoCart("Abbey Road", "Pop Genre", 6.50, 18);
        c1.getShopCart().addMovietoCart("Forrest Gump", "Drama Genre", 13.23, 141);
        c1.getShopCart().removeItemFromCart("Harry Potter");
        System.out.println(c1.toString());     
    }
if (cart[i] != null)
{
    /* Assign the value to a local variable and then
     * check if it's null before accessing it.
     */
    String value = cart[i].getTitle();
    if (value != null && value.equals(title)) {
        totalPrice -= cart[i].getPrice();
        cart[i] = null;
        itemCount -= 1;
        found = true;
    }
}