samedi 3 février 2018

NullPointerException occurred while invoking getter method of a property

I'm trying to get properties of DataSource and Connection objects which are managed by JavaEE server(in this case I'm running WebSphere Application Server and Db2 as RDBMS).

I wrote following code.

public class ClassInfo {
    protected static final String NOT_APPLICABLE = "N/A";
    protected static final String UNKNOWN = "unkown";

    public static LinkedHashMap<String, String[]> getLinkedHashMap(Object obj) {
        LinkedHashMap<String, String[]> map = new LinkedHashMap<String, String[]>();
        BeanInfo bi = null;
        PropertyDescriptor[] pd = null;
        String name, type, value, getter, setter;
        Method m;
        try {
            bi = Introspector.getBeanInfo(obj.getClass());
        } catch (IntrospectionException e) {
            e.printStackTrace();
        }
        pd = bi.getPropertyDescriptors();
        for (int i = 0; i < pd.length; i++) {
            name = pd[i].getName();
            type = pd[i].getPropertyType().getName();
            m = pd[i].getReadMethod();
            if (m != null) {
                getter = m.getName();
                try {
                    value= m.invoke(obj).toString();
                } catch (Exception e) {
                    System.err.println("Exception occured while procession " + m.getName() + " --- " + e.getMessage());
                    e.printStackTrace();
                    value = UNKNOWN + " (getter is " + e.getMessage() + " )";
                }
            } else {
                getter = NOT_APPLICABLE;
                value = UNKNOWN + " (getter method is not available)";
            }
            m = pd[i].getWriteMethod();
            if (m != null) {
                setter = m.getName();
            } else {
                setter = NOT_APPLICABLE;
            }
            map.put(name, new String[] { type, getter, setter, value });
        }
    return map;
}

And here is my Servlet code.

Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("jdbc/db2access");
LinkedHasMap<String, String> map = ClassInfo. getLinkedHashMap(ds);
... and more

I run this Servlet and got interesting output from my code. There are property fields named "XADataSource", "active", "connection" (and more) and their getter methods named "isXADataSource()", "isActive()", "getConnection()" respectively.

"isXADataSource()" returned not null object and its value could be retrieved by "toString()".

NullPointerException occurred while this "isActive()" was invoked although I have checked that the Method object is not null. Here is the code block where this Exception actually caught.

m = pd[i].getReadMethod();
if(m != null) {
    getter = m.getName();              //not null
    value = m.invoke(obj).toString();  //throws NullPointerException
}

Here is stack trace log.

[2/4/18 8:33:59:281 JST] 000000aa SystemErr     R Exception occured while procession isActive --- null
[2/4/18 8:33:59:282 JST] 000000aa SystemErr     R java.lang.reflect.InvocationTargetException
[2/4/18 8:33:59:283 JST] 000000aa SystemErr     R   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[2/4/18 8:33:59:283 JST] 000000aa SystemErr     R   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:90)
[2/4/18 8:33:59:284 JST] 000000aa SystemErr     R   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
[2/4/18 8:33:59:284 JST] 000000aa SystemErr     R   at java.lang.reflect.Method.invoke(Method.java:508)
[2/4/18 8:33:59:284 JST] 000000aa SystemErr     R   at net.mognet.servlet.ClassInfo.getLinkedHashMap(ClassInfo.java:35)
[2/4/18 8:33:59:285 JST] 000000aa SystemErr     R   at net.mognet.servlet.ClassInfo.exposeAsMarkdown(ClassInfo.java:61)
[2/4/18 8:33:59:286 JST] 000000aa SystemErr     R   at net.mognet.servlet.JDBCProperty.doGet(JDBCProperty.java:58)
[2/4/18 8:33:59:286 JST] 000000aa SystemErr     R   at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
[2/4/18 8:33:59:287 JST] 000000aa SystemErr     R   at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
[2/4/18 8:33:59:287 JST] 000000aa SystemErr     R   at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1235)
[2/4/18 8:33:59:288 JST] 000000aa SystemErr     R   at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:779)
[2/4/18 8:33:59:288 JST] 000000aa SystemErr     R   at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:478)
[2/4/18 8:33:59:288 JST] 000000aa SystemErr     R   at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
[2/4/18 8:33:59:289 JST] 000000aa SystemErr     R   at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1124)
[2/4/18 8:33:59:289 JST] 000000aa SystemErr     R   at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:82)
[2/4/18 8:33:59:289 JST] 000000aa SystemErr     R   at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:963)
[2/4/18 8:33:59:290 JST] 000000aa SystemErr     R   at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1817)
[2/4/18 8:33:59:290 JST] 000000aa SystemErr     R   at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:382)
[2/4/18 8:33:59:291 JST] 000000aa SystemErr     R   at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:465)
[2/4/18 8:33:59:291 JST] 000000aa SystemErr     R   at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:532)
[2/4/18 8:33:59:291 JST] 000000aa SystemErr     R   at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:318)
[2/4/18 8:33:59:292 JST] 000000aa SystemErr     R   at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:289)
[2/4/18 8:33:59:292 JST] 000000aa SystemErr     R   at com.ibm.ws.ssl.channel.impl.SSLConnectionLink.determineNextChannel(SSLConnectionLink.java:1187)
[2/4/18 8:33:59:292 JST] 000000aa SystemErr     R   at com.ibm.ws.ssl.channel.impl.SSLConnectionLink$MyReadCompletedCallback.complete(SSLConnectionLink.java:694)
[2/4/18 8:33:59:293 JST] 000000aa SystemErr     R   at com.ibm.ws.ssl.channel.impl.SSLReadServiceContext$SSLReadCompletedCallback.complete(SSLReadServiceContext.java:1833)
[2/4/18 8:33:59:293 JST] 000000aa SystemErr     R   at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175)
[2/4/18 8:33:59:294 JST] 000000aa SystemErr     R   at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
[2/4/18 8:33:59:294 JST] 000000aa SystemErr     R   at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
[2/4/18 8:33:59:294 JST] 000000aa SystemErr     R   at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
[2/4/18 8:33:59:295 JST] 000000aa SystemErr     R   at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
[2/4/18 8:33:59:295 JST] 000000aa SystemErr     R   at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
[2/4/18 8:33:59:296 JST] 000000aa SystemErr     R   at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
[2/4/18 8:33:59:296 JST] 000000aa SystemErr     R   at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1909)
[2/4/18 8:33:59:296 JST] 000000aa SystemErr     R Caused by: java.lang.NullPointerException
[2/4/18 8:33:59:297 JST] 000000aa SystemErr     R   at com.ibm.ws.rsadapter.jdbc.WSJdbcDataSource.isActive(WSJdbcDataSource.java:1203)
[2/4/18 8:33:59:298 JST] 000000aa SystemErr     R   ... 33 more

I'm sure that the Method object(m) returned by "getReadMethod()" was not null but the Object returned by "m.invoke(obj)" was null. I assume that the property named "active" was not initialized(has null value) by JavaEE container and that made this happen. Is my assumption correct? If not please tell me why this happened and how to resolve it.

I'm new to Java , so any suggestions or advice would be appreciated. Regards,





Aucun commentaire:

Enregistrer un commentaire