RuntimeException could not be mapped to a response, re-throwing to the HTTP container java.lang.NullPointerException

user2639869 picture user2639869 · Aug 20, 2013 · Viewed 24.2k times · Source

I am trying to validate a user record from my database using Spring Framework, RESTful Web Services and Jersey Implementation.

I am using MySQL v5.6.0, Eclipse Galileo, Apache tomcat v6.0

UserAccessWS.java

@Path("/user")
@Service
public class UserAccessWS {
@Autowired
private IUserService userService;
private static Logger LOGGER = Logger.getLogger(UserAccessWS.class);

@POST
@Path("/validateUser")
@Produces({ MediaType.APPLICATION_JSON })
public String getValidateUser(@Context HttpServletRequest request,
        @FormParam("userName") String userName,
        @FormParam("password") String password) throws JSONException {
    LOGGER.info("getValidateUser method");
    Users users = new Users();
    users.setUserName(userName);
    users.setPassword(password);
    List<Users> userList = new ArrayList<Users>();
    userList = userService.validateUser(users);

    JSONObject userInfo = new JSONObject();
    userInfo.put("authorize", true);
    return userInfo.toString();
}

@POST
@Path("/login")
@Produces({ MediaType.APPLICATION_JSON })
public String userLogin(
        @FormParam("userName") String userName,
        @FormParam("password") String password) 
                throws Exception 
{
    LOGGER.info("in UserAccessWS::userLogin()");

    JSONObject json = new JSONObject();
    JSONArray jsonArr = new JSONArray();
    List<Users> userList = new ArrayList<Users>();
    userList = userService.userLogin(userName, password);

Null error is being pointed out in this line "userList = userService.userLogin(userName, password);"

    //System.out.println(userList);

    if (userList.size() == 0)
    {
        json.put("status", "fail");
    } 
    else 
    {
        json.put("status", "success");
        jsonArr.add(userList.get(0));
        json.put("data", jsonArr);
    }
     //System.out.println(jsonArr.get(0).userId);
    return json.toString();
}

IUserService.java

package com.cisco.service.view;
import java.util.List;
import org.json.JSONObject;
import com.cisco.connectivity.rs.mapper.ForgotPassword;
import com.cisco.connectivity.rs.mapper.PasswordChange;
import com.cisco.connectivity.rs.mapper.Registration;
import com.cisco.model.xml.domain.Email;
import com.cisco.model.xml.domain.Users;
public interface IUserService {

public List<Users> validateUser(Users users);

public List<Users> userLogin(String userName, String password);

public String userChangePassword(PasswordChange change);

public List<Email> userForgotPassword(ForgotPassword forgot);

public JSONObject saveData( Registration registration);

public JSONObject check(String userName);

public JSONObject editData( Registration registration);

public JSONObject deleteData( Registration registration);

public JSONObject getData( Registration registration);

public JSONObject getRegistrationFormData(String userName);
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/tx/spring-aop-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd"
default-autowire="byName">

<bean id="applicationProperties"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="ignoreResourceNotFound" value="true" />
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="searchSystemEnvironment" value="true" />
</bean>

<context:component-scan base-package="com.cisco" />

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/pooler_mgmt" />
    <property name="username" value="root" />
    <property name="password" value="" />
</bean>

<bean id="systemProperties" class="java.util.HashMap"></bean>
<bean id="systemEnvironment" class="java.util.HashMap"></bean>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

<display-name>CiscoPoolerMgmt</display-name>
<description>CiscoPoolerMgmt</description>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:/CiscoPoolerMgmt/config/applicationContext.xml</param-value>
</context-param>

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath*:/CiscoPoolerMgmt/config/log4j.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<!-- REST web service -->
<servlet>
    <servlet-name>REST_stat_web_service</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.resourceConfigClass</param-name>
        <param-value>com.sun.jersey.api.core.PackagesResourceConfig</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.cisco.ws</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>REST_stat_web_service</servlet-name>
    <url-pattern>/cisco_BI/rest/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

Error which I am get is:

SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
java.lang.NullPointerException
at com.cisco.ws.UserAccessWS.userLogin(UserAccessWS.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:149)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:67)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:259)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:133)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:83)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:133)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:71)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:990)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:941)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:932)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:384)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:451)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:632)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
Aug 21, 2013 12:54:21 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet REST_stat_web_service threw exception
java.lang.NullPointerException
at com.cisco.ws.UserAccessWS.userLogin(UserAccessWS.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:149)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:67)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:259)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:133)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:83)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:133)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:71)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:990)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:941)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:932)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:384)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:451)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:632)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)

The database from where it has to validate the details entered by user is NOT empty. It has the records in them. But via this query it always gives a NullPointerException.

Please help me out.

Answer

Prabhakar Manthena picture Prabhakar Manthena · Jul 12, 2016

May be this is a late reply. I am suspecting the userService it self is null. It is not autowired properly. Because here you mentioned default-autowire="byName" in your applicationContext.xml. I couldn't find the bean named userService in your applicationContext.xml. I think you need to add the below line in you applicationContext.xml

 <bean id="userService" class="yourIUserServiceImplClass"></bean>