简体   繁体   English

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

[英]Java ~ Service Layer failed to call DAO layer and throw NullPointerException

Am i wrongly implemented the way to call DAO layer? 我错误地实现了调用DAO层的方式吗? The breakpoint is not hit inside the DAO method and exception throw only mentions about NullPointerException.Kindly advice. 断点不会在DAO方法内部被触发,异常抛出只会提到NullPointerException.Kindly建议。 Thank You 谢谢

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

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

}

Looks like you never set value to: 看起来你从来没有设定价值:

private BookDAO dao;

in BookServiceImpl , causing this line: BookServiceImpl ,导致此行:

return dao.getBookList(dto);

to throw NPE as dao is null. 抛出NPE,因为dao为null。

You need to instantiate it before accessing it. 您需要在访问它之前对其进行实例化。

In BookServiceImpl BookServiceImpl

private BookDAO dao;  // not initialize 

and then you are calling 然后你在打电话

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

Then you will get NullPointerException . 然后你会得到NullPointerException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM