0
  {
    public JSONObject getVisitorDetailsForGrid(List<Integer> requestid) {
    JSONObject _data1 = new JSONObject();
    DBLib dbObj = new DBLib();
    Connection con = dbObj.getConnection();
    if (con != null) {

        String _tmpReqID = "";

        for (int i = 0; i < requestid.size(); i++) {
            Integer rID = requestid.get(i);
            if (i > 0) {
                _tmpReqID += ",";
            }
            _tmpReqID += rID;
        }

        String query;
        query = "SELECT * FROM visitor_pass.visitor_tran A,visitor_pass.visitor_request B, \n"
                + "visitor_pass.visitor_info C,visitor_pass.visitor_org D WHERE C.ORGID=D.ORGID  AND 
         A.REQUESTID=B.REQUESTID AND A.VISITORID=C.VISITORID  AND A.REQUESTID in (" + _tmpReqID + 
              ")";

        try {
            PreparedStatement pstmt = con.prepareStatement(query);
            System.out.println("query:\n" + pstmt.toString());
            ResultSet rSet = pstmt.executeQuery();
            ResultSetMetaData rsmd = rSet.getMetaData();

            while (rSet.next()) {
                int _reqID = rSet.getInt("REQUESTID");


                JSONObject _tmpVisitor = new JSONObject();
                for (int i = 1; i <= rsmd.getColumnCount(); i++) {

                    try {
                        {
                            _tmpVisitor.put(rsmd.getColumnName(i), rSet.getString(i).trim());
                        }
                    } catch (JSONException ex) {
                        Logger.getLogger(VisitorDetails.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NullPointerException npe) {
                    }
                }
                try {
                    _data1.accumulate(String.valueOf(_reqID), _tmpVisitor);


                } catch (JSONException ex) {
                    Logger.getLogger(VisitorDetails.class.getName()).log(Level.SEVERE, null, ex);
                }
            }


        } catch (SQLException ex ) {
            System.out.println(ex.getMessage());
            // Logger.getLogger(coinsIndentsServlet.class.getName()).log(Level.SEVERE, null, ex);
        }

        dbObj.closeConnection(con);

    } else {
        System.out.println("DB Connection fail");
    }
    
    return _data1;
}}

Here iam retrieving data using sql query but it is showing null pointer exception can anyone suggest. here i want to retrieve the data and display in a grid when iam loading the grid it showing null pointer exception. What are Null Pointer Exceptions (java.lang.NullPointerException) and what causes them?

What methods/tools can be used to determine the cause so that you stop the exception from causing the program to terminate prematurely?

following error is displayed:

java.lang.NullPointerException at com.visitor.info.VisitorDetails.getVisitorDetailsForGrid(VisitorDetails.java:209) at com.visitor.info.GridServletDetails.processRequest(GridServletDetails.java:49) at com.visitor.info.GridServletDetails.doGet(GridServletDetails.java:105) at javax.servlet.http.HttpServlet.service(HttpServlet.java:620) at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2462) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2451) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745)

splrs
  • 2,424
  • 2
  • 19
  • 29

2 Answers2

1

Based on the issue I can suggest some points to you, i hope they will help. as i can see the code only not the flow.

Null pointer exception : When ever a program code tried to access the data/object which is not present or not set or set to null, will throw nullPointerException.

case 1 : try to get the data belongs to a column/field which is not present in table/a result set.

case 2 : we create object like Book b = null; and forget to initialize the object . and if we try to access the data like b.getBookName() will throw nullPointer as object itself is null.

How to find the issue or line causing exception ?

  1. just put one break point to your code and run your code in debug mode, and analyze the code step by step.

  2. Whenever you fine the next line is going to access any data or object. use watch/inspect tool to execute the that statement separately , which will give the result but will not break the execution.

as you show that you are getting at loop so you request list is null, and you are calling size method on null list. please check the list in debug mode inside this method, will resolve the issue. or validate for null before use request list.

requestId != null

please let us know , if we can help you more on this.

1

The requestid that you're passing to the method is null.

From the info you've provided, that can be worked out from:

  • The stack trace with the affected line number
  • What line of code 209 corresponds to.

So that's one of the main techniques you can use.

On the question of what a NullPointerException is:

In this case, the exception arose because you tried to call a method on a null object i.e. one that wasn't initiated (or retrieved, or was set to null).

How you work around one of these has any number of answers. Often you'd check whether the object is null, as you've done with if (con != null). So to avoid crashing in this case you could change your statement from:

`if (con != null)`

to

`if (con != null && requestid != null)`

For a much more comprehensive explanation on null pointer exceptions, see What is a NullPointerException, and how do I fix it?

splrs
  • 2,424
  • 2
  • 19
  • 29
  • [Is it my duty to check for duplicate questions before answering?](https://meta.stackoverflow.com/questions/326622/is-it-my-duty-to-check-for-duplicate-questions-before-answering) – Abra Feb 10 '21 at 11:36