-3

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

enter image description here

Mazzy
  • 13,354
  • 43
  • 126
  • 207
  • You need to explain why the other answers aren't good enough if you're going to downvote them. @arTsmarT is correct, you are not assigning anything to hello and you don't even have 27 lines of code (regarding error back trace) here so I'm guessing you're not showing your whole code. – Bjorn Apr 12 '14 at 23:16
  • What you see here is all the code trust me. For the second time I repeat when you use Java EE you don't have instantiated anything. here it is a guide to prove that https://netbeans.org/kb/docs/javaee/entappclient.html – Mazzy Apr 13 '14 at 07:32
  • If I run and deploy this code in Netbeans following that guide everything works but when I execute on IntelliJ Idea it doesn't work. It follows that it is a problem of IDE – Mazzy Apr 13 '14 at 07:34
  • The above screenshot proves that the code works on Netbeans. I would like to run the same code in Intellij Idea. How can I do? If you don't have any experience in working on Intellij Idea I don't think you can help me – Mazzy Apr 13 '14 at 07:48
  • Accessing EJBs from a client application running out of the container is not that easy. You need an [ACC](http://blogs.steeplesoft.com/posts/2011/02/22/java-ees-buried-treasure-the-application-client-container/). See this answer for further info: http://stackoverflow.com/a/15488335/870122 . Futhermore you need to acquire familiarity about how to run a jar out of an IDE. – perissf Apr 13 '14 at 10:08
  • @perissf your answer is useless at the moment. I said previously I'm able to run that application in Netbeans IDE. The code above compile correctly and it gives me out the message. What I'm asking is how to compile, deploy and run the same code on Intellij IDEA 13. – Mazzy Apr 13 '14 at 10:21
  • To solve this question, one should know how to create and manage a java ee application on Intellij Idea. Only in this way the problem could be solved – Mazzy Apr 13 '14 at 10:22
  • It was a comment, not an answer, and as such was only suggesting a couple of ideas. Since you know perfectly how to solve your issue, I am wondering why you have asked a question. Anyway, I wish you good luck – perissf Apr 13 '14 at 10:47
  • Knowing that the error happened at `client.doConversation();` is a huge piece of information you left unclear. More than one person has assumed it was `helloBean` but you just downvoted them and didn't correct them. Have you tried running the debugger and put a break point there to see just what was null? How could client be null after a new instance was create the line before? – Bjorn Apr 13 '14 at 17:45
  • This is right what I'm asking to myself from one week. How could I get NullPointerException when client is istantiated correctly? I have not actually run the debugger but I suppose I wouldn't get useful infos. From my perspective the point is that the container doesn't inject correctly the dependance into HelloClient. I mean there is not communication between the client and the beans and all depend to the deploying of application in Intellij Idea environment. – Mazzy Apr 13 '14 at 17:54
  • Maybe this is a dumb question, but... Is your IntelliJ a Comunity Edition version? – Pablo Lozano Apr 16 '14 at 15:31
  • No it is ultimate edition – Mazzy Apr 17 '14 at 06:53

2 Answers2

1

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

arTsmarT
  • 456
  • 3
  • 11
  • This answer seems right, or at least addresses an important point, and it was downvoted. – Bjorn Apr 12 '14 at 23:17
  • If you say that then you don't know how java ee works. Here it is a guide https://netbeans.org/kb/docs/javaee/entappclient.html you don't have instantiated anything so this answer is wrong – Mazzy Apr 13 '14 at 07:27
  • As you can see in the guide MySessionRemote is an interface and the object mySession is not instantiated. – Mazzy Apr 13 '14 at 07:29
  • It could have been the answer, it's not just a critique. The question didn't have enough information. – Bjorn Apr 13 '14 at 17:49
  • If you tell me what information you need to help me better, I'll be very glad to give you. I repeat that it is not a problem of code because the same code run correctly in Netbeans. The problem is the way as Intellij Idea works is totally different to Netbeans. – Mazzy Apr 13 '14 at 17:59
  • @BjornTipling I found out you have a blog where you talk about Intellij Idea so I suppose you know pretty well Intellij Idea and you use it for your project. Have you tried to execute this code in your machine? – Mazzy Apr 13 '14 at 18:04
  • I have no idea. I"d have to google it. It's seems like you are trying to see if a Netbeans feature is in Intellij. I'd try searching or asking on the jetbrains forums. – Bjorn Apr 14 '14 at 12:01
  • I have just done it but I have not received yet any answer about that answer – Mazzy Apr 14 '14 at 21:00
1

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

David SK
  • 814
  • 7
  • 21