java - 将对象添加到 ArrayList 并在 Java 中给出 NullPointerException

标签 java arraylist nullpointerexception

我有一个整数数组,我试图循环遍历它,然后根据该数组通过 ID 获取产品对象。然后,我尝试将创建的产品对象添加到另一个数组,但这给了我一个空指针异常错误。

将对象添加到数组的代码:

ArrayList<Integer> cartIds = new ArrayList<Integer>();

void viewCart(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException{

        ProductDAO productDAO = new ProductDAO();
        ArrayList<Product> cartArrayList = null;

        for(int i = 0; i< cartIds.size(); i++){
            Product product = productDAO.getProductByIdCart(cartIds.get(i));

            Product cartProduct = new Product(product.getId(), product.getMake(), product.getModel(), product.getEngine(), product.getPower(), product.getSpeed(), 
                    product.getCategory(), product.getYear(), product.getPrice());

            cartArrayList.add(cartProduct);
        }

        request.getSession(true).setAttribute(IConstants.SESSION_KEY_CART, cartArrayList);
        System.out.println(cartArrayList);
        RequestDispatcher rd = request.getRequestDispatcher("/shop-main.jsp");
        rd.forward(request, response);

    }

我的 DAO:

public Product getProductByIdCart(int id) {

    DBManager dmbgr = new DBManager();
    Connection con = dmbgr.getConnection();
    int carId = 0;
    String make = null;
    String model = null;
    String engine = null;
    String power = null;
    String speed = null;
    String category = null;
    String year = null;
    String price = null;
    String location = null;
    Product carData = new Product();

    String query = "SELECT * FROM PRODUCTDATA WHERE PRODUCT_ID = "+ id +"";
    try {
        PreparedStatement stmt = con.prepareStatement(query);
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            carId = (rs.getInt(1));
            make = (rs.getString(2));
            model = (rs.getString(3));
            engine = (rs.getString(4));
            power = (rs.getString(5));
            speed = (rs.getString(6));
            category = (rs.getString(7));
            year = (rs.getString(8));
            price = (rs.getString(9));
            location = (rs.getString(10));
            Product tempCar = new Product();
            tempCar.setId(carId);
            tempCar.setMake(make);
            tempCar.setModel(model);
            tempCar.setEngine(engine);
            tempCar.setPower(power);
            tempCar.setSpeed(speed);
            tempCar.setCategory(category);
            tempCar.setYear(year);
            tempCar.setPrice(price);
            tempCar.setImg_location(location);
            carData = tempCar;
            System.out.println(tempCar);
        }

    } catch (SQLException e) {
        e.printStackTrace();
    }


    return carData;

}

还有我的产品构造函数:

public Product(int id, String make, String model, String engine, String power, String speed, String category, String year, String price){
    this.id = id;
    this.make = make;
    this.model = model;
    this.engine = engine;
    this.power = power;
    this.speed = speed;
    this.category = category;
    this.year = year;
    this.price = price;
}

最佳答案

您应该初始化 cartArrayList: ArrayList<Product> cartArrayList = new ArrayList<>();

关于java - 将对象添加到 ArrayList 并在 Java 中给出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54159530/

相关文章:

java - 将参数传递给嵌套 Controller

java - 一次从所有 arrayList 中获取一个元素

java - 在用java编程时,我得到这个错误,有谁能告诉我到底出了什么问题吗?

java - 设置Excel工作表背景图片

java - Hibernate:在不同线程中查找刚刚持久化的对象

java - Kotlin 文件/类和 Kotlin 脚本有什么不同

java - 删除 arraylist 中的元素时出现问题

java - android-将数组列表转换为自定义格式的一个字符串

java.lang.NullPointerException : Loading three charts (from github) into a 2x2 GridView

java - getWritableDatabase() 中的 nullPointerException