将对象添加到ArrayList中,从而在Java中提供NullPointerException

时间:2019-01-12 12:22:49

标签: 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;
}

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

我认为您已将cartArrayList初始化为null并添加到其中。这导致空指针异常。将该行替换为:

ArrayList<Product> cartArrayList = new ArrayList<Product>();

答案 2 :(得分:0)

初始化您的cartArrayList

ArrayList<Product> cartArrayList = new ArrayList<Product>();

答案 3 :(得分:0)

在使用它之前,您需要初始化集合(在您的情况下为ArrayList)。 像这样:

ArrayList<Product> cartArrayList = new ArrayList<Product>();

如果使用Java 7+,则类型推断功能可让您在初始化时忽略类型。

ArrayList<Product> cartArrayList = new ArrayList<>();

此外,在Java中,我们更喜欢使用接口类型而不是特定的工具。像这样:

List<Product> cartArrayList = new ArrayList<>();