Java~Service Layer无法调用DAO层并抛出NullPointerException

时间:2013-12-10 08:50:34

标签: java

我错误地实现了调用DAO层的方法吗?断点不会在DAO方法内部被触发,异常抛出只会提到NullPointerException.Kindly建议。谢谢

服务层:     公共类BookServiceImpl {

    private static BookServiceImpl myInstance;
    private BookDAO dao;

    public static BookServiceImpl getInstance() {
        if (myInstance == null) {
            myInstance = new BookServiceImpl();
        }
        return myInstance;
    }

    public List<Book> getBookList(BookDTO dto) {
        return dao.getBookList(dto);
    }

}

DAO图层     公共课BookDAO {         私人BookDAO(){             sqlService = Services.get(SqlService.class);         }

    public static BookDAO getInstance() {
        if (myInstance == null) {
            myInstance = new BookDAO ();
        }
        return myInstance;
    }

}

2 个答案:

答案 0 :(得分:1)

您似乎永远不会将值设置为:

private BookDAO dao;
<{1>}中的

,导致此行:

BookServiceImpl

return dao.getBookList(dto); 的NPE抛出为空。

您需要在访问它之前对其进行实例化。

答案 1 :(得分:1)

BookServiceImpl

private BookDAO dao;  // not initialize 

然后你正在打电话

return dao.getBookList(dto);  // here dao is null.

然后你会得到NullPointerException