原文地址:http://cncc.bingj.com/cache.aspx?q=java.lang.IllegalArgumentException%3a+clientPK.clientDepartmentNumber&d=4930948971429890&mkt=zh-CN&setlang=zh-CN&w=791b320f,3d55f170

作者:Rob Kraft

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: