首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >删除元素数组会导致NullPointerException

删除元素数组会导致NullPointerException
EN

Stack Overflow用户
提问于 2019-05-28 06:49:29
回答 1查看 169关注 0票数 -1

我是爪哇的初学者。我得做购物车。但是我被remove方法卡住了。

请帮帮我!

我尝试创建了remove方法。但是它不能很好的工作..

Customer.java

public class Customer {
    private String id;
    private String firstName;
    private String lastName;
    private ShoppingCart shopCart;

    public Customer(String id, String firstName, String lastName) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        shopCart = new ShoppingCart();
    }

    public ShoppingCart getShopCart() {
        return this.shopCart;
    }
    @Override
    public String toString() {
        return ("Customer ID is: " + this.id + "\n" + "Customer's name is: "
                 + this.firstName + " " + this.lastName + "\n\n" 
                + "Shopping list: \n\n" + shopCart.toString());

    }

ShoppingCart.java

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;

    }
}

Test.java

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

我想删除元素数组。但是输出是

Exception in thread "main" java.lang.NullPointerException

如果编译器运行得很好,我该怎么办?我认为remove方法是一个问题。

EN

回答 1

Stack Overflow用户

发布于 2019-05-28 07:09:25

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

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 here和IntelliJ here进行调试的信息。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56333183

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档