0

I have a EE app and now Im trying to use jax rs project that use a ejb method from the EE app.

When I access the url http://localhost:8080/rest/projectws/getplayers I get:

Servlet Path:
/rest

Path Info:
/projectws/getplayers

Query String:
null

Stack Trace
org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException

I will put below the code that I think that should be relevant for this issue.

I have a interface "PlayerLocal.java" to return players from a team:

@Local
public interface PlayerLocal{
    public ArrayList<String> getFromTeam(String teamName);
}

Than in "Player.java" I have:

   @Stateless
    public class Player implements PlayerLocal{
    @Override
        public ArrayList<String> getFromTeam(String teamName) {
            ArrayList<String> result;
            List<Player> players;

            Query q=em.createNamedQuery("Team.findByName",Team.class);
            q.setParameter("c", teamName);

            Team t =null;
            try{
                t=(Team)q.getSingleResult();
            }
            catch(NoResultException e){
                return null;
            }
            players=t.getPlayers();

            result=new ArrayList<String>();

            for(Player p:players){
                result.add(p.getName());
            }   

            return result;
        }
       }

I have also the entity Player in jpa but i think its not important in this issue.

Now in the jax-rs "WS.java":

import java.util.ArrayList;
import javax.ejb.EJB;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import ejb.Player;

@Path("/projectws")
public class WS{

    @EJB
    Player ejbP;

    public WS() {
        System.out.println("WS created");
    }

    @GET
    @Path("getplayers")
    @Produces({MediaType.APPLICATION_JSON})
    public ArrayList<String> getPlayers() {


        ArrayList<String> ls;
        ls = ejbP.getFromTeam("teamA");

        System.out.println(ls.size());


        return ls;
    }
}

And I also have

package rest;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("rest")
public class Test extends Application {
}

I already try a lot of things and nothing is working, do you see what can be wrong?

Whole stack trace:

Error processing request
Context Path:
/Project

Servlet Path:
/rest

Path Info:
/projectws/getplayers

Query String:
null

Stack Trace
org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException
org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:77)
org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:220)
org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:175)
org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:418)
org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:209)
org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:221)
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:805)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:745)

I also have a warning:

20:51:22,035 WARNING [javax.enterprise.resource.webcontainer.jsf.config] (ServerService Thread Pool -- 80) javax.naming.NamingException: WFLYNAM0027: Failed instantiate InitialContextFactory org.jboss.naming.remote.client.InitialContextFactory from classloader ModuleClassLoader for Module "deployment.proj-EAR.ear.proj-WEB.war:main" from Service Module Loader [Root exception is java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory from [Module "deployment.proj-EAR.ear.proj-WEB.war:main" from Service Module Loader]]: javax.naming.NamingException: WFLYNAM0027: Failed instantiate InitialContextFactory org.jboss.naming.remote.client.InitialContextFactory from classloader ModuleClassLoader for Module "deployment.proj-EAR.ear.proj-WEB.war:main" from Service Module Loader [Root exception is java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory from [Module "deployment.proj-EAR.ear.proj-WEB.war:main" from Service Module Loader]]
        at org.jboss.as.naming.InitialContext.getDefaultInitCtx(InitialContext.java:118)
        at org.jboss.as.naming.InitialContext.init(InitialContext.java:99)
        at javax.naming.ldap.InitialLdapContext.<init>(InitialLdapContext.java:154)
        at org.jboss.as.naming.InitialContext.<init>(InitialContext.java:89)
        at org.jboss.as.naming.InitialContextFactory.getInitialContext(InitialContextFactory.java:43)
        at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:684)
        at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
        at javax.naming.InitialContext.init(InitialContext.java:244)
        at javax.naming.InitialContext.<init>(InitialContext.java:192)
        at com.sun.faces.config.WebConfiguration.processJndiEntries(WebConfiguration.java:835)
        at com.sun.faces.config.WebConfiguration.<init>(WebConfiguration.java:155)
        at com.sun.faces.config.WebConfiguration.getInstance(WebConfiguration.java:216)
        at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:166)
        at io.undertow.servlet.core.ApplicationListeners.contextInitialized(ApplicationListeners.java:187)
        at io.undertow.servlet.core.DeploymentManagerImpl$1.call(DeploymentManagerImpl.java:200)
        at io.undertow.servlet.core.DeploymentManagerImpl$1.call(DeploymentManagerImpl.java:171)
        at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:42)
        at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
        at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
        at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
        at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
        at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
        at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
        at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
        at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:234)
        at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:100)
        at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:82)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
        at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Caused by: java.lang.ClassNotFoundException: org.jboss.naming.remote.client.InitialContextFactory from [Module "deployment.proj-EAR.ear.proj-WEB.war:main" from Service Module Loader]
        at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:198)
        at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:363)
        at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:351)
        at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:93)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:348)
        at org.jboss.as.naming.InitialContext.getDefaultInitCtx(InitialContext.java:113)
        ... 32 more

In the wildfy log I can see that the error is in the line 30 that is "ls = ejbP.getFromTeam("teamA");" I just dont understand why is giving npe here

I try this and I get the same issue class not found exception. But like below I cant access "http://localhost:8080/rest/projectws/getplayers".

InitialContext ic=null;

CRUDStudentRemote bean=null; 


@GET
    @Path("getplayers")
    @Produces({MediaType.APPLICATION_JSON})
    public ArrayList<String> getPlayers() {

        try{
            ic=new InitialContext();

            bean = (PlayerRemote) 
                    ic.lookup("Projects/Player!ejb.PlayerRemote");

            ArrayList<String> ls;
            ls = bean.getFromTeam("teamA");

            System.out.println(ls.size());
            return ls;

        }
        catch(NamingException e){
            System.out.println("error.");
            e.printStackTrace();
            System.exit(1);
        }


        return null;

    }
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
kalk
  • 1
  • 2
  • Is that your whole stack trace? – shmosel Dec 12 '16 at 20:59
  • Thanks for your answer. I update it now. – kalk Dec 12 '16 at 21:02
  • 1
    You are going to need to learn to use a debugger on your server. You cannot debug a program by asking stackoverflow questions. – Mike Nakis Dec 12 '16 at 21:03
  • Thanks for your answer. Im getting this errors above, I just dont understand why is happening because everything seems ok so I ask to see if someone can give a help. And i know what nep is. – kalk Dec 12 '16 at 21:07
  • I'd pay close attention to that `Caused by` section in the warning you're getting. It sounds like you're missing a required class at runtime. Not being able to instantiate something might be a cause for an NPE later. – Ian McLaird Dec 12 '16 at 22:46
  • You'll need to look in the server logs to see the cause of your NPE. The stack trace you posted is RESTEasy just catching the error and rethrowing it. You'll need the under underneath it. – James R. Perkins Dec 13 '16 at 00:08
  • Thanks for your answer. But in the wildfy log I can see that the error is in the line 30 that is "ls = ejbP.getFromTeam("teamA");" I just dont understand why is giving npe here. – kalk Dec 13 '16 at 00:38
  • `ejbP` is null. Invoking a method on a null instance causes `NullPointerException`. It is null because container could not inject `@EJB Player ejbP;` And this is probably caused by `java.lang.ClassNotFoundException org.jboss.naming.remote.client.InitialContextFactory`. The ContextFactory is responsible of looking for instances of EJBs – pedrofb Dec 13 '16 at 08:45

1 Answers1

0

WS class is not managed by the EJB container so it can not inject Player ejbP. You could either make the service an EJB ot you could aquire the Player instance thru the InitialContext.

user3719857
  • 1,083
  • 4
  • 16
  • 45