在JSF中获取NullPointerException,我无法解释或找到

时间:2015-02-21 20:36:10

标签: jdbc nullpointerexception jsf-2.2

我正在尝试在JSF中创建一个购物车,然后是一个显示存储在数据库中的所有订单的按钮。我已完成所有编码,但当我尝试将书添加到我的ShoppingCart类时,我得到一个空指针异常,我不能为我的生活找出原因。请任何帮助将不胜感激。

当我调用c.addBook(b)

时发生错误
public List<ShoppingCart> getAllOrders() throws SQLException {
    Connection conn = ds.getConnection();
    try {

        Statement insertQuery = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

        ResultSet info = insertQuery.executeQuery("select TITLE, AUTHOR, PRICE, QUANTITY, "
                + "ORDERLIST.ORDERNUMBER, TOTALPRICE from "
                + "PURCHASED left join ORDERLIST on "
                + "PURCHASED.ORDERNUMBER = "
                + "ORDERLIST.ORDERNUMBER ORDER BY "
                + "PURCHASED.ORDERNUMBER");
        Statement ordnum = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        ResultSet orders = ordnum.executeQuery("SELECT "
                + "ORDERNUMBER FROM ORDERLIST");
        while (orders.next()) {
            System.out.print("first while");
            ShoppingCart c = new ShoppingCart();
            int orderNumber = orders.getInt("ORDERNUMBER");
            System.out.print("after first while");

            while (info.next()) {
                System.out.print("second while");

                int currentOrd = info.getInt("ORDERNUMBER");
                if (orderNumber == currentOrd) {
                    System.out.print("if statement");
                    Book b = new Book();
                    b.setAuthor(info.getString("AUTHOR"));
                    b.setTitle(info.getString("TITLE"));
                    b.setPrice(info.getDouble("PRICE"));
                    System.out.print("1" + info.getString("AUTHOR") + info.getString("TITLE") + info.getDouble("PRICE"));
                    System.out.print("1" + b.getAuthor());
                    System.out.print("2" + b.getTitle());
                    System.out.print("3" + b.getPrice());
                    c.addBook(b);

                }

            }
            System.out.print("before before first");
            info.beforeFirst();
            System.out.print("after before first");
            carts.add(c);

        }

    } finally {
        conn.close();
    }

    return carts;
}

这是似乎是错误原因的addBook函数

public void addBook(Book book) {
    boolean inList = false;
    System.out.print(book.getAuthor());
    System.out.print(book.getTitle());
    System.out.print(book.getPrice());
    System.out.print("pre for loop");
    for (Book temp : books) {
        System.out.print("made a book");
        if (book.title.equals(temp.title)) {
            inList = true;
            switch (temp.title) {
                case "Intro to Java Serverlets":
                    serverlet++;
                    break;
                case "Intro to JSP":
                    jsp++;
                    break;
                case "Intro to JSF":
                    jsf++;
                    break;
                default:
                    ee++;
                    break;
            }

        }
    }
if (!(inList)) {
        if (book.title.equals("Intro to Java Serverlets")) {
            serverlet++;
        } else if (book.title.equals("Intro to JSP")) {
            jsp++;
        } else if (book.title.equals("Intro to JSF")) {
            jsf++;
        } else {
            ee++;
        }
        System.out.print("pre add a book");
        books.add(book);
    }
}

1 个答案:

答案 0 :(得分:0)

结果我需要添加

if(books == null)
    {
        books = new ArrayList<>();
    }
在addBook方法之前的

。我不知道为什么,因为我早先实例化但我不会讨厌它摆脱我的空指针