Thursday, January 07, 2010

Solving Errors with Netbeans Faces 2.0 CRUD Tutorial

Finally had some time to sit and go through what's new in Java EE 6. I opted for the latest Netbeans version 6.8 with the bundled Glassfish v3 app server. The quickest way to get started.

There is a lot of good documentation available on-line as well as several tutorials exploring various feature of the new platform. The first that caught my eye was Generating a JavaServer Faces 2.0 CRUD Application from a Database

The tutorial runs you through how to generate a lot of boilerplate code for a standard crud app in Netbeans that I was really excited to see, having been spoiled from what's available in VS in the .Net world. I came across a few minor issues preventing the sample app from fully working. If these errors look familiar, here's how to fix them.

The first results from trying to create or edit a Client:

An Error Occurred:

clientPK.clientName

- Stack Trace

java.lang.IllegalArgumentException: clientPK.clientName
...
This results from the string "clientPK.clientName being used in the id fields if h:inputText

h:inputtext id="clientPK.clientName" value="#{clientController.selected.clientPK.clientName}" title="#{bundle.EditClientTitle_clientPK_clientName}" required="true" requiredMessage="#{bundle.EditClientRequiredMessage_clientPK_clientName}"/>


Change this id to clientName. Also change it in the line above in the h:outputLabel. Make similar changes for ClientDepartmentNumber in the same file. There is a similiar change required in project/Edit.xhtml and project/Create.xhtml for projectName

This got me to the client create screen, but when trying to save, I was greeted with

An Error Occurred:

/client/Create.xhtml @19,256 value="#{clientController.selected.clientPK.clientName}": Target Unreachable, 'null' returned null
The 'null' here is clientPK within the selected client entity in the client controller. The prepareCreate in the clientController creates a new Client but the contained clientPK remains null. I would typically check for this condition in the accessor and create the object if null, and actually still do. Looking at the generated code I found a constructor that takes a clientPK as a parameter, so simply changing the instantiation in clientController.prepareCreate to:
current = new Client(new ClientPK());
took care of it.

Here is an excellent 3 part overview of what's new in Java EE 6:


5 comments:

Robert said...

Thanks Rob. I ran into the same problem and your fix solved it. I just hadn't noticed the id error. I hope whoever writes the "Entities to JSF" wizard picks up on this. Until it's fixed, the tutorial loses credibility for both JSF and Netbeans 6.8.
BTW, is a compound name for "id=" illegal or just a rendering peculiarity?

Rob Kraft said...

From the brief research I've done it appears the dot is legal markup, but I could be wrong. There is most likely some ambiguity in what is legal so different browsers will act differently. The tutorial probably works fine for the original author.

I think a lot of the samples are taken directly from the community so the quality does vary.

Rob

chittibabu said...

java.lang.IllegalArgumentException: clientPK.clientName

any one help to solve the above problem

cesar.cecilia said...

You help me to solve my problem. In addition to your solution i have to add one more line in method create:

current.getProductosProvPK().setCif(current.getProveedores().getCif());

Because i have a foreing key and this attribute is null by default and not get value automaticaly.

Bijan said...

Excellent Rob, you took the sweetness of a great tutorial being destroyed by its errors back to us all ;)