Get NullPointerException in java EE application on Intellij Idea IDE

风格不统一 提交于 2019-12-13 11:23:52

问题


I'm trying to run this simple and small application but I don't understand which is the problem and consequently how to fix it.

Here it is the code. It is very simple. One bean invoked by a client application in order to print out in the client terminal the well-known message Hello World. I have created the application by using Intellij Idea 13.0.3 IDE with Server GlassFish 4.0.0, Java JDK 8.0 and Java EE 7.

EDIT

I think I got those errors because I didn't deployed correctly my application. I have found a guide that says that after having created the client application in netbeans environment you have to execute Insert Code -> Call Enterprise Bean. Now how can I do the same thing on Intellij Idea?


package myejb;

import javax.ejb.Remote;

@Remote
public interface HelloBeanRemote
{
    public String sayHello(String name);
}

package myejb;

import javax.ejb.Stateless;

@Stateless
public class HelloBean implements HelloBeanRemote
{
    @Override
    public String sayHello(String name)
    {
        return "Hello, " + name + "!";
    }
}

and the Client application

package myejbclient;

import javax.ejb.EJB;
import myejb.HelloBeanRemote;

public class HelloClient
{
    @EJB
    private static HelloBeanRemote helloBean;

    public static void main(String[] args)
    {
        HelloClient client = new HelloClient();
        client.doConversation();
    }

    public void doConversation()
    {
        System.out.println(helloBean.sayHello("World"));
    }
}

I execute glassfish to deploy the application. it is deployed correctly but when I run the client application I get this error message:

Exception in thread "main" java.lang.NullPointerException
    at myejbclient.HelloClient.doConversation(HelloClient.java:27)
    at myejbclient.HelloClient.main(HelloClient.java:22)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

EDIT2

The same code works like a charm in Netbeans IDE having GlassFish installed.

Netbeans IDE


回答1:


You haven't assigned hello to anything that's been instantiated.




回答2:


In your client's code, at any time you assign the reference to your hello service.

When you use it in an application server as glassfish, jboss, etc. The container reads the annotations and insert the available EJB.

For your client you need an Application Client Container or search the reference by your self

From the Glassfish FAQ How do I access a Remote EJB ... you need InitialContext, if is necessary you need to configure the server and port

With an ACC, you need to create an enterprise project and deploy in your server, an easy example from Netbeans can help you: https://netbeans.org/kb/docs/javaee/entappclient.html



来源:https://stackoverflow.com/questions/22969602/get-nullpointerexception-in-java-ee-application-on-intellij-idea-ide

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!