Good day to all;
I'm developing a program using JSF2 and JPA2 (OpenJPA). Im also using
IBM Rapid App Dev't v8 with WebSphere App Server v8 test server.
I have two simple entities, Employee and Department. Each Department has many Employees and each
Employee belongs to a Department (using deptno and workdept). My problem occurs when i tried to add a
new employee and selecting a department from a combo box (using selectOneMenu - populated from Department table):
when i run the program, the following error messages appeared:
An Error Occurred:
java.lang.NullPointerException
Caused by:
java.lang.NullPointerException - java.lang.NullPointerException
I also tried to make another program using Deptno and Workdept as String instead of integer, still doesn't work.
Pls help. Im also a newbie. Tnx and God bless.
Below are my codes, configurations and setup. Just tell me if there are some codes that I forgot to include.
Im also using Derby v10.5 as my database:
CREATE SCHEMA RTS;
CREATE TABLE RTS.DEPARTMENT
(DEPTNO INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
DEPTNAME VARCHAR(30));
ALTER TABLE RTS.DEPARTMENT
ADD CONSTRAINT PK_DEPARTMNET PRIMARY KEY (DEPTNO);
CREATE TABLE RTS.EMPLOYEE
(EMPNO INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
NAME VARCHAR(50),
WORKDEPT INTEGER);
ALTER TABLE RTS.EMPLOYEE
ADD CONSTRAINT PK_EMPLOYEE PRIMARY KEY (EMPNO);
Employee and Department Entities
package rts.entities;
import java.io.Serializable;
import javax.persistence.*;
@Entity
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int empno;
private String name;
//bi-directional many-to-one association to Department
@ManyToOne
@JoinColumn(name="WORKDEPT")
private Department department;
.......
getter and setter methods
package rts.entities;
import java.io.Serializable;
import javax.persistence.*;
import java.util.List;
@Entity
public class Department implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int deptno;
private String deptname;
//bi-directional many-to-one association to Employee
@OneToMany(mappedBy="department")
private List<Employee> employees;
.......
getter and setter methods
JSF 2 snipet using combo box (populated from Department table)
<tr>
<td align="left">Department</td>
<td style="width: 5px"> </td>
<td><h:selectOneMenu styleClass="selectOneMenu"
id="department1" value="#{pc_EmployeeAdd.employee.department}">
<f:selectItems value="#{DepartmentManager.departmentSelectList}"
id="selectItems1"></f:selectItems>
</h:selectOneMenu></td>
</tr>
package rts.entities.controller;
import com.ibm.jpa.web.JPAManager;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import com.ibm.jpa.web.NamedQueryTarget;
import com.ibm.jpa.web.Action;
import javax.persistence.PersistenceUnit;
import javax.annotation.Resource;
import javax.transaction.UserTransaction;
import rts.entities.Department;
import java.util.List;
import javax.persistence.Query;
import java.util.ArrayList;
import java.text.MessageFormat;
import javax.faces.model.SelectItem;
@SuppressWarnings("unchecked")
@JPAManager(targetEntity = rts.entities.Department.class)
public class DepartmentManager {
.......
public List<SelectItem> getDepartmentSelectList() {
List<Department> departmentList = getDepartment();
List<SelectItem> selectList = new ArrayList<SelectItem>();
MessageFormat mf = new MessageFormat("{0}");
for (Department department : departmentList) {
selectList.add(new SelectItem(department, mf.format(
new Object[] { department.getDeptname() },
new StringBuffer(), null).toString()));
}
return selectList;
}
Converter:
package rts.entities.converter;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import rts.entities.Department;
import rts.entities.controller.DepartmentManager;
import com.ibm.jpa.web.TypeCoercionUtility;
public class DepartmentConverter implements Converter {
public Object getAsObject(FacesContext facesContext, UIComponent arg1,
String entityId) {
DepartmentManager departmentManager = (DepartmentManager) facesContext
.getApplication().createValueBinding("#{DepartmentManager}")
.getValue(facesContext);
int deptno = (Integer) TypeCoercionUtility.coerceType("int", entityId);
Department result = departmentManager.findDepartmentByDeptno(deptno);
return result;
}
public String getAsString(FacesContext arg0, UIComponent arg1, Object object) {
if (object instanceof Department) {
return "" + ((Department) object).getDeptno();
} else {
throw new IllegalArgumentException("Invalid object type:"
+ object.getClass().getName());
}
}
}
Method for Add button:
public String createEmployeeAction() {
EmployeeManager employeeManager = (EmployeeManager) getManagedBean("EmployeeManager");
try {
employeeManager.createEmployee(employee);
} catch (Exception e) {
logException(e);
}
return "";
}
faces-conf.xml
<converter>
<converter-for-class>rts.entities.Department</converter-for-class>
<converter-class>rts.entities.converter.DepartmentConverter</converter-class>
</converter>
Stack trace
javax.faces.FacesException: java.lang.NullPointerException
at org.apache.myfaces.shared_impl.context.ExceptionHandlerImpl.wrap(ExceptionHandlerImpl.java:241)
at org.apache.myfaces.shared_impl.context.ExceptionHandlerImpl.handle(ExceptionHandlerImpl.java:156)
at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:258)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:191)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1147)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:722)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:449)
at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:178)
at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1020)
at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:87)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:886)
at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1655)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:195)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:276)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1650)
Caused by: java.lang.NullPointerException
at rts.entities.converter.DepartmentConverter.getAsString(DepartmentConverter.java:29)
at org.apache.myfaces.shared_impl.renderkit.RendererUtils.getConvertedStringValue(RendererUtils.java:656)
at org.apache.myfaces.shared_impl.renderkit.html.HtmlRendererUtils.getSubmittedOrSelectedValuesAsSet(HtmlRendererUtils.java:444)
at org.apache.myfaces.shared_impl.renderkit.html.HtmlRendererUtils.internalRenderSelect(HtmlRendererUtils.java:421)
at org.apache.myfaces.shared_impl.renderkit.html.HtmlRendererUtils.renderMenu(HtmlRendererUtils.java:359)
at org.apache.myfaces.shared_impl.renderkit.html.HtmlMenuRendererBase.encodeEnd(HtmlMenuRendererBase.java:76)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:519)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:626)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:622)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:622)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:622)
at org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage.renderView(FaceletViewDeclarationLanguage.java:1320)
at org.apache.myfaces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:263)
at org.apache.myfaces.lifecycle.RenderResponseExecutor.execute(RenderResponseExecutor.java:85)
at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:239)
... 24 more