• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException

 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to debug an error "java.lang.NullPointerException" - this is happening on the statement "List results = em.createNativeQuery(myQuery).getResultList();". I might have to do with "em" as I don't understand what that is doing. I have tried a couple ways of defining em.

jboss console.JPG
[Thumbnail for jboss console.JPG]
jboss console showing error
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At line 2 you assign null to the variable em and at line 7 you try to invoke a method via that variable, which dereferneces null and leads to the NullPointerException.
You need to assign a reference to an EntityManager instance to variable em somewhere in between line 2 and line 7.
 
Knute Snortum
Bartender
Pie
Posts: 2908
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless my eye are deceiving me, you set em to null and then try to execute a method from it.
 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this particular example that is true. I only did this cause the compiler forced me to. I have also tried eliminating that line and putting "EntityManager em" (and "em2") in the public class. The major thing I'm trying to do is to execute a query and grab the results. I don't know how or where to define EntityManager em. I am also trying to copy off other Java programs but no luck yet.

public class WEMCostBean implements Serializable {
EntityManager em2;
private String client;
private String phone_number
 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying to avoid it, but here is the entire code.

 
Stevens Miller
Bartender
Posts: 1377
28
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like you are using the Enterprise Edition, which is kind of outside my realm, but you are going to have to assign em a value that refers to an instance of an EntityManager object before you can do anything like this:

Based on what I found on the web, you do that with a factory method, like this:

Maybe the tutorial at that Web page will be of more help to you.
 
Junilu Lacar
Bartender
Pie
Posts: 8893
81
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You usually get an EntityManager from an EntityManagerFactory - try to find other classes in your application that reference EntityManagerFactory.
 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I try to add that code, I get an error that EntityManagerFactory is not defined. See attached.
EntityManager.JPG
[Thumbnail for EntityManager.JPG]
EntityManagerFactory is not resolved.
 
Knute Snortum
Bartender
Pie
Posts: 2908
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need a persistence context, which is too complex to describe here. See Stevens's second link.
 
Junilu Lacar
Bartender
Pie
Posts: 8893
81
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Posting the entire code is unnecessary and not helpful.

This is the only relevant code you needed to post, with a comment like "These are the only lines that reference em in this class:"
 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I appreciate you giving me thoughts and ideas. When I put the following in, everything compiles fine but when I run it I get the null pointer error

 
Knute Snortum
Bartender
Pie
Posts: 2908
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really should read this (as Stevens pointed out) before you post another question.
 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did read it and I tried various things based on it but I'm still not golden. I'm sure I am doing something stupid but I don't know what it is. This stuff is pretty confusing.
 
Stevens Miller
Bartender
Posts: 1377
28
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael, your problem is the same as in this little program:



You simply can't use methods defined in a class until you have created an instance of that class and assigned a reference to that instance to a variable of that class's type. In my little program, I get a NullPointerException at this line:
because s constains null. I never created an instance of a String and assigned a reference to that instance to s. So, I can't use any String methods on s, because s doesn't refer to a String.

In your code, em is null when you try to use EntityManager methods. You can't use those methods until after you assign em a reference to an instance of an EntityManager object.
 
Junilu Lacar
Bartender
Pie
Posts: 8893
81
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing you can do, as I suggested before, is to look at other classes in your codebase, which I gather is quite large. There are probably other classes that use an EntityManager. See how those classes are getting a reference to an instance of one and try to do the same thing here. In Eclipse, you would highlight EntityManager, then right click, References... then select the scope (one of Workspace, Project, etc.) Once you find a few references, look for a reference to EntityManagerFactory nearby. Or you can do a Java Search. From the Eclipse menu, select Search -- Java, then type in EntityManagerFactory.
 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is what I've been trying to do (look at other code and duplicate it). Unfortunately, the code set is very large and confusing and so far I haven't been able to figure out why similar code works elsewhere but not when I add it to my program. Still searching and begging for how to do this. Part of the problem is that I do not understand everything I'm seeing. I'm googling and reading too but so far no joy.
 
Junilu Lacar
Bartender
Pie
Posts: 8893
81
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if there's much else to offer then, aside from reiterating: large code bases are not really a problem especially if you're using an IDE like Eclipse. You just have to know what you're looking for and how to use your tool to search for it. Did you not find any references to EntityManagerFactory in your code base then? Again, let Eclipse do the heavy lifting for you; all you have to do is hit CTRL+H, go to the Java Search tab, and type in "EntityManagerFactory". If there really is no reference to EntityManagerFactory in your code base, then look at what other code is doing to run a native SQL query.

BTW, are there no other programmers on your team whom you can ask? They might be able to offer some insight that we can't.
 
Stevens Miller
Bartender
Posts: 1377
28
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And let me add to what Junilu has suggested (all of which makes sense to me): you really need to know, before you go looking at larger bodies of code, exactly why a call to em.createNativeQuery generates a NullPointerException when you have not assigned anything to em.

Really, let me emphasize this, because it is basic to everything you'll do with Java: uninitialized object reference variables (that is, any variable that holds anything other than a native data type, like an int or float) contain the value "null." You cannot run methods or query the value of member variables with an object reference variable until it contains a reference to an instance of an object. Until it does, you are trying to run a method without referring to any object to run it on. It's slightly akin to dividing by zero.

Now, it seems to me that you may not have had to cope with a NullPointerException before. I say this because it seems as though you're looking for a solution in the form of knowing more about how the EntityManager class works, while I can absolutely assure you that no amount of additional knowledge about that class will help you solve your problem. Back in your original post, your problem was set in stone by these two lines of code:



Line 1 (which was Line 2, in your original post) assigned null to em. That's not needed, because uninitialized object reference variables are automatically initialized to null. But, you also declared em to be final, which means that, once initialized, it can never be changed. Thus, in Line 2, above (Line 7, in your original post), em is still null when you try to run the createNativeQuery method. At that point, your code generates a NullPointerException because em does indeed contain null, and you tried to run a method by reference to null, which is what generates that exception.

So, at this point, do you understand why assigning null to em and then trying to call a method with em produces a NullPointerException? If so, great! If not, do not go looking at anything in your code base for help, because your problem isn't with your understanding of your code base; it's with your understanding of how a method operates on an object reference. When you truly understand why the two lines above will always produce a NullPointerException, you'll be ready to move on to bigger things.
 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand that the problem is that em is null. The challenge I'm having is figuring out how to write the code so that it isn't the case. I have tried so many things that my head is spinning. When I figure out how to do this, I'll post the answer.

All I'm trying to do is to execute a query and get the result of the query. This should be easy but it isn't. I don't know if it is Java or Hibernate that is making this so confusing.

I am looking at other code in the project (only 9000 files) but when I do what they do, I just get different errors. It all boils down to not knowing how to set em and what needs to be imported at the top of the program.

I appreciate everyone's thoughts and ideas.
 
Junilu Lacar
Bartender
Pie
Posts: 8893
81
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Piaser wrote:but when I do what they do, I just get different errors. It all boils down to not knowing how to set em and what needs to be imported at the top of the program.

It might help us help you if you showed us exactly what code you found, tried, and failed to make work.
 
Paul Clapham
Sheriff
Posts: 21583
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't know what to import? In my version of Eclipse, if you press Ctrl-Shift-o it will figure out what packages your unknown classes belong to, and ask you to choose from a list if there's any ambiguity (e.g. java.awt.List versus java.util.List). If you don't know what to choose from that list of packages you should ask the person who is supervising your training.
 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code works:



What I am trying is



In the jboss log, I see the query but not "after list". Immediately after
 
Stevens Miller
Bartender
Posts: 1377
28
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Piaser wrote:The following code works:


That seems unlikely. Where does em get set to a reference to an instance of an EntityManager object?
 
Knute Snortum
Bartender
Pie
Posts: 2908
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's set by injection. Read this.

I bet the persistence.xml file is the problem.
 
Junilu Lacar
Bartender
Pie
Posts: 8893
81
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "magic" is done with the @PersistenceContext annotation. Track down the persistence.xml file and see how that's configured to work with the query. In Eclipse, try CTRL-R (goto Resource) and type in "persistence.xml" to find it in your project.
 
Stevens Miller
Bartender
Posts: 1377
28
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:It's set by injection. Read this.

Hey, that's some hot stuff, Knute! Thanks.

I read the link and confess I'm baffled at this point. I know what injection is (or thought I did), but how that annotation accomplishes it is not immediately obvious to me.

I'll pursue it, though...
 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sounds encouraging. Now I have to figure out what to do. I have 8 persistence.xml files. None of them reference IdentityWS.java (which is the code that works). Not sure what I should be looking for.
 
Michael Piaser
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I removed all except for some sample classes from one of my persistence.xml files. When I move (via Eclipse) the webReportDetail entry from the nonJTA to the JTA section, it gets moved it back to the nonJTA section when I compile. Obviously the change needs to be made another way. I don't know what JTA and nonJTA mean, but I figured I'd try.

 
Knute Snortum
Bartender
Pie
Posts: 2908
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've moved this thread to Object Relational Mapping where I think it may get some better responses. I know something of JPA, but I usually have a book open while I code.
 
Knute Snortum
Bartender
Pie
Posts: 2908
62
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:
Knute Snortum wrote:It's set by injection. Read this.

Hey, that's some hot stuff, Knute! Thanks.

I read the link and confess I'm baffled at this point. I know what injection is (or thought I did), but how that annotation accomplishes it is not immediately obvious to me.

I'll pursue it, though...


The annotation serves as a marker for whatever container server it is running in. In the linked tutorial is was TomEE but I believe others, like JBoss, use that annotation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic