gpt4 book ai didi

Java~Service层调用DAO层失败并抛出NullPointerException

转载 作者:行者123 更新时间:2023-12-02 06:26:22 25 4
gpt4 key购买 nike

我是否错误地实现了调用DAO层的方式? DAO 方法内未命中断点,异常抛出仅提及 NullPointerException.Kindly 建议。谢谢您

服务层: 公共(public)类 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 层 公共(public)类 BookDAO { 私有(private) BookDAO () { sqlService = Services.get(SqlService.class); }

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

}

最佳答案

看起来您从未将值设置为:

private BookDAO dao;

BookServiceImpl 中,导致这一行:

return dao.getBookList(dto);

dao 为 null 时抛出 NPE。

您需要在访问它之前实例化它。

关于Java~Service层调用DAO层失败并抛出NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20489825/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com