3

I created a simple XML web service using NetBeans 7's "RESTful Web Services from Database..." wizard. At this point, I want to publish a list of users from the associated mySQL database.

When I attempt to access the service via its URL (http://localhost:8080/database/resources/users), I get an error that reads "java.lang.NullPointerException". The stack trace:

service.AbstractFacade.findAll(AbstractFacade.java:41)
service.UserFacade.findAll(UserFacade.java:51)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:165)
com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:67)
com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:276)
com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:83)
com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:133)
com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:71
com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1171)  com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1103)  com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1053)
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1043)
com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:406)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:477)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:662)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

User entity:

package entities;
...
@Entity
@Table(name="users")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"), 
...

I've also changed the named query to User.findAll in case the names needs to align with the entity's name. This did not solve the problem.

I'm not certain if it is 'normal' or not, but the wizard created a fairly sparse UserFacade class; I added the missing methods after researching the topic. Furthermore, the javax.ejb.Stateless package seems to be missing (perhaps not on my workstation's CLASSPATH); this is the reason that the @Stateless annotation is disabled.

UserFacade class:

//@Stateless
@Path("users")
public class UserFacade extends AbstractFacade<User> {

    @PersistenceContext(unitName="databasePU") 
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UserFacade() {
        super(User.class);
    } 

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public User find(@PathParam("id") BigDecimal id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<User> findAll() {
        return super.findAll();
    } 

}

Exception is thrown at the first line in the AbstractFacade's findAll method:

public List<T> findAll() {  

  javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
  ...
}

Questions:

  • Is the @Stateless annotation required for this to function?
  • Does this pattern require J2EE 6 rather than J2SE 6 (which is what is installed on my OS X workstation)? The 'javax.ejb' namespace seems to suggest enterprise java beans.

** edit **

  • Java SE 6 (1.6.0_29-b11-402)
craig
  • 25,664
  • 27
  • 119
  • 205
  • `@NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u")`?? not `"SELECT u.* FROM Users u"`? – ori Jan 12 '12 at 15:49

2 Answers2

2

The auto-generated query "SELECT u FROM Users u" works without any problems. As per the comment suggesting that "u" might be wrong because it doesn't represent a column, that suggestion is not correct because here "u" is an alias for the table users.

I would debug further the findAll() to check if something is null, i.e. the EntityManager.

The @Stateless annotation in the UserFacade is necessary, and removing it would probably cause the EntityManager to be null (note that I wrote "removing" because NetBeans places if for you, if you use "RestFul Web Services from Database" wizard). See here a similar question.

Regarding your latest edit: yes, these features need to be built using the Java Platform, Enterprise Edition. In particular, RESTFul web services make use of the Java API for RESTful Web Services (JAX-RS) which is included in the Java EE 6 platform as explained here.

GlassFish Server Open Source Edition is the first compatible implementation of the Java EE 6 platform specification: I suggest using this Application Server and following the tutorials linked above.

Community
  • 1
  • 1
perissf
  • 15,979
  • 14
  • 80
  • 117
  • Netbeans didn't add the annotation. In fact, the UserFacade that the wizard created was quite sparse. I had to manually create the code based on examples that I found. I wish I knew why this happened. Do you know which .JAR file contains this annotation? Is there a way to determine the missing files in Netbeans 7 (I can't seem to find a way)? – craig Jan 15 '12 at 18:57
  • Can we chat on the room "Netbeans restful web services"? I am talking about the class UserFacadeREST created by the wizard "New Restful Web services from Database" – perissf Jan 15 '12 at 19:19
  • I installed the EE edition of Netbeans 7.1 (which includes Glassfish). Recreating the project using Glassfish instead of Tomcat generated a functional project. – craig Jan 17 '12 at 21:10
0

I think @ori is on the the answer. Your table Users probably don't have a column named u so you get an exception when it tries to match the column u to the database.

Change to u.* and it should work fine.

Andreas Wederbrand
  • 38,065
  • 11
  • 68
  • 78
  • 'u' is an alias, not a column. Your suggestion did not work. – craig Jan 17 '12 at 21:08
  • Yeah, that's exactly why you can't do `SELECT u FROM Users u`. You need to do `SELECT u.* FROM Users u` – Andreas Wederbrand Jan 18 '12 at 10:58
  • Though it looks like SQL (and your answer would make sense if it were), this is actually a [JPQL (Java Persistence Query Language) statement](http://en.wikipedia.org/wiki/Java_Persistence_Query_Language), and its syntax is OK : here, `u` is an alias for the entity `Users`. – Alain BECKER Aug 09 '12 at 23:06