-2

I have a code snippet

String sql = "SELECT * FROM merchantlist ORDER BY TransactionCount DESC LIMIT 10";
    Connection conn = null;

    try {
        List<String> merchantList = null;
        conn = dataSource.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
            merchantList.add(new String(rs.getString("MerchantName")));
        }
        rs.close();
        ps.close();
        return new TopMerchants(merchantList);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } finally {
        if (conn != null) {
            try {
            conn.close();
            } catch (SQLException e) {}
        }
    }
}

I get an exception while running this on Tomcat -

java.lang.NullPointerException
com.cybs.impactanalysis.dao.impl.TopMerchantsDAOImpl.retrieveMerchantList(TopMerchantsDAOImpl.java:35)
com.cybs.impactanalysis.controller.MerchantMonitoring.MerchantMonitoring(MerchantMonitoring.java:22)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:498)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

When I run the stored proc , I get 2 rows as output. The datatypes are configured properly (MerchantName column is VARCHAR(20))

How do I fix this issue? I tried to put it in debug mode but I cannot see the values in ResultSet

Prateek Narendra
  • 1,837
  • 5
  • 38
  • 67

1 Answers1

-1

Creating String with new won't solve the problem. Because, it wasn't causing NullPointerException instead below line of code

List<String> merchantList = null;

You have nowhere created ArrayList and directly started using them.

   if (rs.next()) {
        merchantList.add(new String(rs.getString("MerchantName")));
    }

At above line of code merchantList is still pointing null reference. So, you need to create an instance of ArrayList before using them

 List<String> merchantList = new ArrayList<>();

Also, you should revert the below code

 new String(rs.getString("MerchantName"))

to

 rs.getString("MerchantName")

As there is no need of creating new String object.

Ravi
  • 30,829
  • 42
  • 119
  • 173
  • @Prateek Narendra You need to change `if` to `while`. Because `if` will be executed only once and for the first time when it get `true`. – Ravi Sep 16 '17 at 13:55