简体   繁体   中英

How to fix NullPointerException

I'm writing java servlet which should get dvd by user_id. But I have problem with NullPointerException . Does anybody have an idea how to fix it? When I tried to fix it by if (nickname != null ) then I had a problem with attribute dvds in request.setAttribute("dvds", dvds); Thanks

List<Dvd> dvds;
    try {
        String nickname = request.getParameter("nickname");
        User user = userDao.getByLogin(nickname);
        Long userId = user.getId();
        dvds = this.dvdDao.getDvdsByUserId(userId);
    } catch (SQLException e) {
        throw new ServletException("Unable to get dvds", e);
    }

    request.setAttribute("dvds", dvds);
    RequestDispatcher dispatcher = request.getRequestDispatcher("WEB-INF/loans.jsp");
    dispatcher.forward(request, response);

}
}   


 public List<Dvd> getDvdsByUserId(Long user_id) throws SQLException {
    List<Dvd> dvds = new ArrayList<Dvd>();
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;

    try {
        connection = getConnection();
        preparedStatement = connection.prepareStatement("SELECT * FROM sedivyj_dvd where user_id = ?;");
        preparedStatement.setLong(1, user_id);
        resultSet = preparedStatement.executeQuery();

        while (resultSet.next()) {
            Dvd dvd = new Dvd();
            dvd.setId(resultSet.getInt("id"));
            dvd.setUser_id(resultSet.getString("user_id"));
            dvd.setName(resultSet.getString("name"));
            dvd.setBorrower(resultSet.getString("borrower"));
            dvd.setMail(resultSet.getString("mail"));
            dvd.setBorrow_date(resultSet.getString("borrow_date"));
            dvd.setBorrow_until(resultSet.getString("borrow_until"));
            dvds.add(dvd);
        }

    } catch (SQLException e) {
        throw e;
    } finally {
        cleanUp(connection, preparedStatement);
    }

    return dvds;
}

Assuming nickname is the problem (also taking into account non-existent user):

List<Dvd> dvds = new ArrayList<Dvd>;
try {
    final String nickname = request.getParameter("nickname");
    if (nickname != null) {
        final User user = this.userDao.getByLogin(nickname);
        if (user != null) {
            dvds = this.dvdDao.getDvdsByUserId(user.getId());
        } else {
            // handle non-existent user
        }
    } else {
        // handle no "nickname" parameter was present
    }
} catch (SQLException e) {
    throw new ServletException("Unable to get dvds", e);
}

request.setAttribute("dvds", dvds);
final RequestDispatcher dispatcher = request.getRequestDispatcher("WEB-INF/loans.jsp");
dispatcher.forward(request, response);

It seems that your code requires a "nickname" parameter in the request in order to work properly. (If you don't have a "nickname", you can't figure out the user id, and retrieve that user's DVDs ...)

It also appears that you are getting a NPE because the request is missing the parameter (or it is misspelled or something). This is definitely a bug in whatever is sending the request .

The correct solution would be either:

  • send an error response of some kind if getParameter("nickname") returns null , AND

  • fix the web form that is sending the requests with the parameter missing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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