0

I am following this POJO's example to output a list of users with the Object id but show nothing in the list. I'm not sure if the connection is correct with the CodecRegistry or something wrong in one of my below classes. Tried to use the Eclipse debugger but got no hints with that.

Firstmost the connection

ConnectionString connectiontring = new ConnectionString(
                    "my connection to database");
            MongoClientSettings clientSettings = MongoClientSettings.builder().codecRegistry(pojoCodecRegistry)                     
                    .applyToConnectionPoolSettings(builder -> builder.maxWaitTime(20000, TimeUnit.MILLISECONDS))
                    .applyConnectionString(connectiontring).retryWrites(false).build();

            MongoClient mongoClient = MongoClients.create(clientSettings);

            MongoDatabase database = mongoClient.getDatabase("DATABASE");

Here is my list user method in DAO class

    @Override
    public List<User> listAll() {

        List<User> userList = new ArrayList<User>();

        // 1. get the table from dB
        MongoCollection<Document> userTbl = database.getCollection("User");

        // loop through the table to find, then add
        for (Document doc : userTbl.find()) {
            User user = new User((String) doc.get("fullname"), (String) doc.get("email"), (String) doc.get("password"));

    userList.add(user);
}

return userList;

}

Service class just call the method from DAO class

    public List<User> listUser() {

        List<User> userList = userDAO.listAll();        
        
        return userList;

    }

The Controller

public class ListUsersServlet extends HttpServlet {

    public ListUsersServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        UserService userService = new UserService(request, response);

        List<User> userList = userService.listUser();

        String list_user_page = "user_list.jsp";
        RequestDispatcher rd = request.getRequestDispatcher(list_user_page);
        rd.forward(request, response);
    }

}
Stu_Dent
  • 370
  • 1
  • 4
  • 19
  • 1
    Try adding something into the user list. It should work. – Spectric Oct 05 '20 at 13:11
  • It's hard to understand what happens here. You should at least provide line numbers for files if you reference them. – Andrei Kovrov Oct 05 '20 at 13:15
  • @AndreiKovrovy the service class has only that few lines to call the method from DAO class. Do you mean put a line number next to that syntax where the NPE occurred? My apology as I'm not sure how to do that. – Stu_Dent Oct 05 '20 at 13:41
  • As long as you can't ask "Why is variable X null?" while pointing out the exact variable X in a normal [mcve], but you can only ask "Why do I get a NullPointerException?", then the abovelinked duplicate totally answers your real question. – BalusC Oct 05 '20 at 21:31
  • My apology, will try follow the standard to ask again differently. – Stu_Dent Oct 05 '20 at 22:02

1 Answers1

0

As I understood you have NPE in the UserService.listUser() method. Maybe UserDAO is null. May you check it?