0

I have a UserModel class which has user data like firstName id etc.

Database class:

public class Database {
    private static Map<Long, UserModel> userDatabase = new HashMap<>();
    private static UserModel temp = new UserModel((long)1, "vamsi", "sree", "daf", "ad@afa", "psadw");

    public static void addUserData(UserModel user) {
        Long id = (long) (userDatabase.size() + 1);
        userDatabase.put(id, user);
}

    public static Map<Long, UserModel> getUserData() {
        userDatabase.put((long) 1, temp);
        return userDatabase;
    }
}

A UserDAO class:

public class UserDAO {

    private Map<Long, UserModel> users = Database.getUserData();

    public void addUserDao(UserModel user) {
        Database.addUserData(user);
    }

    public List<UserModel> getUserDao() {
        return new ArrayList<UserModel>(users.values());
    }
}

And finally a UserResource class:

@Path("user")
public class UserResource {

    private UserDAO userDao;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<UserModel> getUser() {
         return userDao.getUserDao();
         //return new UserModel((long)1, "vamsi", "sree", "daf", "ad@afa", "psadw");
    }
}

When I run the code with the second return statement (the commented one) enabled, it works without problem. But running the first return statement gives the following error:

Type Exception Report

Message java.lang.NullPointerException

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

javax.servlet.ServletException: java.lang.NullPointerException
    org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:489)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause

java.lang.NullPointerException
    org.vamsi.prototype.resource.UserResource.getUser(UserResource.java:21)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    java.lang.reflect.Method.invoke(Unknown Source)
    org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
    org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
    org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
    org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:205)
    org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
    org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
    org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
    org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
    org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
    org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
    org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    org.glassfish.jersey.internal.Errors.process(Errors.java:267)
    org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
    org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)
    org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)
    org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

I tried using GenericEntity<List>, JsonArray, but they are not working.

nSv23
  • 429
  • 6
  • 19
  • 1
    you are not creating instance of userDao that is why it is null. Create an instance of userDao – Vikas Sachdeva May 26 '17 at 13:52
  • @VikasSachdeva: I have created an instance of `userDAO` in `UserResource`: `private UserDAO userDao;`, otherwise I wont be able to access the `getUserDao()` in `UserResource`. – nSv23 May 26 '17 at 14:46
  • 2
    Writing `private UserDAO userDao;` is variable declaration just. `private UserDAO userDao = new UserDao();` is instance creation – Vikas Sachdeva May 26 '17 at 14:51
  • @VikasSachdeva: OMG thanks a lot, you are a saviour... I am used to c++ where I just do `UserDAO userDao`, and call the method I want`userDao.getUserDao()`. Please write an answer so that I can upvote and declare it an answer. – nSv23 May 26 '17 at 15:24

0 Answers0