File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB and other Java EE Technologies and the fly likes Problems with EJB Local and using servlets - null pointers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Problems with EJB Local and using servlets - null pointers" Watch "Problems with EJB Local and using servlets - null pointers" New topic
Author

Problems with EJB Local and using servlets - null pointers

John Ament
Greenhorn

Joined: Jul 07, 2007
Posts: 22
Hi, all.

I'm not new to Java but I am new to EJBs. I am starting with EJB3 spec, running on weblogic 10 (it's what we've decided to go with at my job). I am attempting to create a simple bean, based on this interface:



I have the following set up in my web.xml:



With the expected result, if I type in:

http://127.0.0.1/HelloUserWeb/HelloUser?name=Dave
The output should be:
"Welcome to the world of Enterprise JavaBeans, Dave!"

I figure once I get the basic concept down, the rest will come to me very quickly

Alas, it is not working. Whenever I try to run the servlet, I'm getting a null pointer exception:



The EJB is annotated like so in my servlet:


I hope I have given everyone enough information. Other than the web.xml, I am not using any deployment descriptors. My package is laid like this:

EAR:
- EJB-JAR
- WAR
-- Servlet packed in JAR + web.xml in correct location.

Thanks,

- John
Amit M Tank
Ranch Hand

Joined: Mar 28, 2004
Posts: 257
Could you please paste the code of



I guess the NullPointerException is in the servlet. Please check it.


Amit Tank
Linked In
John Ament
Greenhorn

Joined: Jul 07, 2007
Posts: 22
This is the source to the entire servlet.



As you can see, i'm ensuring there's a value in the input.... the null pointer seems to be because the bean itself is null. but my understanding is that the @EJB annotation should populate the bean automatically...

Thank you so much for your response.
Amit M Tank
Ranch Hand

Joined: Mar 28, 2004
Posts: 257
Please read this
John Ament
Greenhorn

Joined: Jul 07, 2007
Posts: 22
So I tried a different approach, based on what I read on that page (since POJO based apps are really what we're after, not directly in the servlets).

I get a new error (yay, well not really).


Exception preparing module: EJBModule(HelloWorldAppEJB.jar) [EJB:011023]An error occurred while reading the deployment descriptor. The error was: : java.lang.ClassNotFoundException: Class bytes found but defineClass()failed for: 'ejb.learning.HelloWorldBean'.


This is a different app now (I decided to leave the old code intact to be updated later).

This is my EJB POJO


This is my locator



And my servlet's processRequest:



As you can see, it's trying to find the class, but can't, but it can find the bytes. I don't see how it's possible.

This is my application.xml

John Ament
Greenhorn

Joined: Jul 07, 2007
Posts: 22
Ok, so that issue was a pure mismatch of java versions. I am able to get it to build and deploy.

But now I'm back to square one.
Amit M Tank
Ranch Hand

Joined: Mar 28, 2004
Posts: 257
So when you call Locator.getBean() are you getting some exception? You are getting NPE at what line now?
John Ament
Greenhorn

Joined: Jul 07, 2007
Posts: 22
I'm getting NameNotFoundException from the locator.

John Ament
Greenhorn

Joined: Jul 07, 2007
Posts: 22
BTW.

This is the information about the bean from the context.


Application Name: HelloWorldApp
EJB Module Name: HelloWorldAppEJB.jar
Bean Name: HelloWorldBean


So I would assume the JNDI name would be "HelloWorldApp/HelloWorldBean/local" no?
Amit M Tank
Ranch Hand

Joined: Mar 28, 2004
Posts: 257
On the weblogic console, do a right click and view the JNDI tree and check the JNDI name of the bean. This is the JNDI name which needs to be used in the call.
John Ament
Greenhorn

Joined: Jul 07, 2007
Posts: 22
I'm not sure what you mean. I'm using web console, can't really right click on anything.

Edit: Nevermind, I found it.

But I don't see the EJB in the tree.
[ July 10, 2007: Message edited by: John Ament ]
John Ament
Greenhorn

Joined: Jul 07, 2007
Posts: 22
don't know if it's of any help, but when I look at the deployment configuration, I see the following:

HelloWorldApp Enterprise Application
EJBs
-None to display
Modules
-HelloWorld Web Application
-HelloWorldAppEJB.jar EJB Module
Web Services
-None to display


Should my EJB be under EJBs?
Amit M Tank
Ranch Hand

Joined: Mar 28, 2004
Posts: 257
There would be something called as MyDomain-->Servers --> myserver

Right click on it. Click on View JNDI Tree. This will give you the JNDI names of all the deployed objects.
John Ament
Greenhorn

Joined: Jul 07, 2007
Posts: 22
Yes yes I got to that point. But I don't see my deployment in there.
Amit M Tank
Ranch Hand

Joined: Mar 28, 2004
Posts: 257
That coud mean that your EJB is not deployed in the server. Could you check the weblogic console and see if the status of your EJB is deployed? Weblogic also provides you a mechanism to test your deployed EJBs through the console UI.
Amit M Tank
Ranch Hand

Joined: Mar 28, 2004
Posts: 257
You might want to look at this post


He mentions that you could use the same name as that provided in the ref-name in web.xml.
John Ament
Greenhorn

Joined: Jul 07, 2007
Posts: 22
EJB is definitely deployed, I am able to see it.

Can you please look at my earlier post, where I show the contents of the deployment configuration. To me it looks odd, but maybe it's wrong?
Amit M Tank
Ranch Hand

Joined: Mar 28, 2004
Posts: 257
these are showing inside the Deployments-->Application -->

You should see your web and EJB modules here. And should be able to test your EJB from this UI.
John Ament
Greenhorn

Joined: Jul 07, 2007
Posts: 22
Hi. This is local only, so web service based testing isn't available. I do see it listed, as I mentioned before.

Name Test Point Comments
HelloWorldBean There are no test points for this EJB module.

So it sees it, but I still don't see it in the JNDI tree.
Amit M Tank
Ranch Hand

Joined: Mar 28, 2004
Posts: 257
Did you try the stuff mentioned in the post



You might want to look at this post

He mentions that you could use the same name as that provided in the ref-name in web.xml.

John Ament
Greenhorn

Joined: Jul 07, 2007
Posts: 22
The items mentioned in that post don't make much sense, as I'm using JNDI look up, not web reference.
John Ament
Greenhorn

Joined: Jul 07, 2007
Posts: 22
I'm making more progress.

Looks like the reason that it didn't show up in the JNDI tree was that it didn't implement a remote interface.

I got this about it now though:

Binding Name: HelloWorldAppHelloWorldAppEJB_jarHelloWorldBean_HelloWorldRemote

Class: ejb.learning.HelloWorldBean_kap09s_HelloWorldRemoteImpl_1000_WLStub

Hash Code: 337

toString Results: weblogic.rmi.internal.CollocatedRemoteRef - hostID: '7836518163334780112S::wl_server:examplesServer', oid: '337', channel: 'null'



Though I am a bit confused as to why it's only remote in the tree.

Edit: Ok, so I'm able to get the remote out of the tree, but the object it's giving me back is of type:

ejb.learning.HelloWorldBean_kap09s_HelloWorldRemoteImpl

When I'm expecting 1 of:
HelloBean
IHelloBean
HelloBeanLocal
HelloBeanRemote

Any idea?

[ July 10, 2007: Message edited by: John Ament ]

Edit #2: (haha edit superior, reply inferior)

I finally made a lot of progress.

I added the following annotation to the bean: @Stateless(name="HelloWorldBean",mappedName="HelloWorldBean") (instead of just @Stateless)
I now can pull it out of the context with the following JNDI name: HelloWorldBean#ejb.learning.HelloWorldRemote

I am still very confused as to why it's remote only.
[ July 10, 2007: Message edited by: John Ament ]
Amit M Tank
Ranch Hand

Joined: Mar 28, 2004
Posts: 257
Now your bean has become a Remote Bean. The Object which it gives you is a Stub, which you can cst to your RemoteHome after you do a PortableRemoteObject.narrow().
John Ament
Greenhorn

Joined: Jul 07, 2007
Posts: 22
I understand that...and that's what I'm trying to avoid.

I want to be able to use the Local interface to the bean.

But I read somewhere that you can only access Local from Servlet/JSP directly, and need to use JNDI from helper classes. Is this correct?
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Any client - JSP, servlet or EJB - can use the local interface. What is important is that the client and the EJB are part of the same application. In other words, you must build an EAR file containing the whole application code and then deploy that EAR file.

The client still has to go through JNDI and do lookup (as is the case with remote interfaces), but will now get a reference to a real Java object on the heap, instead of a stub to a remote object.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
John Ament
Greenhorn

Joined: Jul 07, 2007
Posts: 22
Roger,

That's what I thought too. That's how my app's setup as well. But it got the remote interface. I was hoping, and expecting, it to get the Local interface. Is there a different way I'm supposed to write my code? Can I use the EJB annotation somehow? That's what I wanted originally.
John Ament
Greenhorn

Joined: Jul 07, 2007
Posts: 22
I"m going to start a new topic - I have a new problem now, not really relevant.

Thanks for all of your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems with EJB Local and using servlets - null pointers