0

I got NullPointerException when I tried to run this func in JSP page:

public ArrayList<Shortnews> getListNews() {
        Connection cons = DBconnect.getConnecttion();        
        ArrayList<Shortnews> list = new ArrayList<>();
        try {
            CallableStatement statement = cons.prepareCall("{call spshnews_select_6news}");
            ResultSet rs = statement.executeQuery();
            while (rs.next()) {
                Shortnews shnew = new Shortnews();
                shnew.setShortnewsID(rs.getInt("shortnewsID"));
                shnew.setShortnewsTitle(rs.getString("shortnewsTitle"));
                shnew.setShortnewsHead(rs.getString("shortnewsHead"));
                shnew.setShortnewsImg(rs.getString("shortnewsImg"));
                shnew.setAuthorID(rs.getLong("authorID"));
                shnew.setPublishedTime(rs.getTimestamp("publishedTime"));
                shnew.setShortnewURL(rs.getString("shortnewURL"));
                list.add(shnew);
            }
            cons.close();
        } catch (NullPointerException|SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

This exception only occurs when I call this function in JSP file. But I had run my procedure and function in DAO.java file successfully. Is there anything unusual, please? THis is the output:

java.lang.NullPointerException
    com.bgfarm.dao.ShortnewsDAO.getList6News(ShortnewsDAO.java:54)
    org.apache.jsp.index_jsp._jspService(index_jsp.java:164)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

NPE happens as soon as it runs to ResultSet rs = statement.executeQuery(); and fall down catch

I really don't know how it does.Thanks!

leebongee
  • 103
  • 1
  • 10
  • You should add the stack trace of the NPE to your question, and also mark in your source where the line number mentioned in the stack trace is. – RealSkeptic Nov 24 '19 at 17:03
  • I've updated this question. Thanks a lot! – leebongee Nov 24 '19 at 17:17
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Andy Guibert Nov 24 '19 at 17:47
  • no, it doesn't. I'd tried to run procedure and it was ok, exec function in ShortnewDAO.java file and it's ok. But only when I call it in JSP file then NPE occured. – leebongee Nov 25 '19 at 06:20

0 Answers0