This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JPA giving me Nullpointers on the Facade classes  RSS feed

 
michael smithson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Guru's,

I'm afraid i need some help with JPA.
I have made a simple testproject that should store a user in a database.
However, JPA does create the tables but gives me the famous Nullpointer when i try to persist a 'BlogUser' object.

I have controller class that uses a serviceclass.
In this serviceclass the facade classes are injected as @EJB's.

To post alle the code is a but much, but i'm willing to if it's needed ofcourse.

The relevant part i think is this :

Controller:


Service class :



and the BlogUser class :



The generated BlogUserFacade class :



The persistence.xml file :


If i run my project i get the following error :



Debugging showed that it crashes in the InitExamples procedure on this line : blogUserFacade.create(u1);
I looked everywhere but i don't get it. I have also tried working with different primary key generation strategies, but no success.
If any more information is needed, tell me, i will add it.

So my question is actually : Why do i receive a nullpointer on my Facade class while it should never be instantiated anyway?
What am i missing here?

Thank you in advance.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36396
453
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to move this to our EJB forum since the problem is actually about EJBs and not JPA. The problem is that this code is never injected so when the method InitExamples is run, a NullPointer is thrown.

 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36396
453
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code looks right. Are there any messages in the log before this one? Maybe when starting up the EJB itself, there is an error?

You might also check the load order in the log as you start up. The EJB should be starting up before the Web project.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is in the way you initialize the BlogService. Because you use a constructor, nothing is injected. All those @EJB annotations mean nothing if you do all the initializing yourself. So instead of creating the BlogService manually, just inject it into the servlet. Assuming BlogService is a bean itself, just annotate it with @EJB:
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36396
453
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:The problem is in the way you initialize the BlogService. Because you use a constructor, nothing is injected.

Good catch Rob! I didn't notice that. Have a cow for being thorough in reading the code.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
michael smithson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:The problem is in the way you initialize the BlogService. Because you use a constructor, nothing is injected. All those @EJB annotations mean nothing if you do all the initializing yourself. So instead of creating the BlogService manually, just inject it into the servlet. Assuming BlogService is a bean itself, just annotate it with @EJB:


Thank you very much for looking into it.

You are correct Rob. I changed the structure of the program so it would enable the container to instantiate the BlogService, but unfortunately i still get errors.
I think i made the BlogController class a managed bean by adding the annotation @Named and i made it @RequestScoped. This should give the container access to the beans.
In the meantime i added a webpage with a button. When clicked it supposed to call BlogController, in which BlogService is injected (in which the BlogUserFacade is injected), to insert a few dbase records.
Somehow GlassFish reports it's unable to generate a stateless bean.

The BlogController should be a managed bean to give the container access to it.
The BlogService is injected as a @Stateless bean into the controller, should be good.
The facade classes are @Stateless and are injected into the BlogService.

Right now the project looks like this:

The new BlogController: (i think i made it a managed bean because of @Named, please correct if wrong)



The BlogService:
(Not sure if the empty constructor is still needed actually)



I made a (very simple) webpage with a button that should insert some records into the JavaDB:



When i press the button, i get an exception in the browser, giving the following stacktrace:



Edit: my web.xml file. It doesn't include any EJB references. Is this possibly the problem?



Somehow i thin my question now is : Why doesn't the container see the BlogController bean?
Did it indeed become a managed bean by adding @Named?

At least i think the problem is that the container doesn't see the BlogController class.
If anyone can give me a push in the right direction, it'd be most appreciated.

Thanks again for helping a beginner :-).

 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems that the problem is in BlogMessageFacade. Can you show us the class with its annotations?
 
michael smithson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, here is the local interface and the class BlogMessageFacade :

BlogMessageFacadeLocal:



and here is the BlogMessageFacade:



I had these classes generated by Netbeans (7.3.1).
When it didn't work i tried using local interfaces but i may have taken a wrong turn there.
They don't seem to do any harm though.

Thanks for looking into it, it's breaking my brain.


EDIT: My apologies. I just realised i committed a capital sin by not posting the full source.
I didn't post the message class because i'm not doing anything with it at the moment. It's commented out everywhere, but nonetheless could be part of the cause.
The only thing my code does is try to store a user in the database. I can't say for sure if it will make a difference if i remove the BlogMessage class and all of its support classes.

Tonight i will try out what the project does if i completely remove the message classes and facades, so only the user classes remain.
I'll post the result, again my apologies for any inconvenience it may have caused.


 
michael smithson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok i removed all references to BlogMessage from the project, just to be sure.
I removed the BlogMessage class, the BlogMessageFacade and the BlogMessageFacadeLocal interface.
Cleaned, restarted glassfish, and deployed.

I now have:
a BlogController, (supposed to be a managed bean)
BlogService, (injected into controller)
BlogUser, (model)
BlogUserFacade, (injected into BlogService)
BlogUserFacadeLocal (local interface)

The above classes haven't changed, except for the removal of the BlogMessage imports etc.
The index.html page is still the same.

Unfortunately the removal of the BlogMessage class and it's 'bean-jpa-accesoires' didn't bring me closer to a solution.
The problem still seems to be that the container can't find my managed bean, the BlogController.
Here is the current stacktrace Glassfish throws back at me :


An object that can't be found, it must be some stupid mistake that i'm overlooking but i'm just not seeing it.
Please, if this is easy for anyone, give me a hint so i can sleep in peace again ;-).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!