n_g n_g - 3 months ago 20
Java Question

ognl.OgnlException: target is null for setProperty...?

I'm new to Struts 2. I'm creating a demo web application which allows a user to submit employee details on a jsp and show them on the next jsp. Following is the code:


<constant name="struts.custom.i18n.resources" value="ApplicationResources" />

<!-- Configuration for the default package. -->
<package name="default" extends="struts-default" namespace="/">

<action name="empDetails" class="com.webapp.test.action.MyAction"
<result name="success">jsp/employeeDetails.jsp</result>

<action name="addEmployee" class="com.webapp.test.action.MyAction"
<result name="success">jsp/addEmployee.jsp</result>

Action class

public class MyAction extends ActionSupport{

private static final long serialVersionUID = 1L;

private Employee emp = null;

public String addEmployee(){
System.out.println("In addEmployee");
return SUCCESS;

public String employeeDetails(){
System.out.println("In employeeDetails");
System.out.println("Employee ID: "+emp.getEmployeeID());
return SUCCESS;

In the above action class, Employee is a separate model class with the following attributes:

String employeeID;
String name;
String department;

Following is my JSP page to add an employee:

<%@taglib uri="/struts-tags" prefix="s"%>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Add Employee</title>
<s:form action="empDetails">
<s:textfield name="emp.employeeID" label="Employee ID"></s:textfield>
<s:textfield name="emp.name" label="Name"></s:textfield>
<s:textfield name="emp.department" label="Department"></s:textfield>

After filling in the employee details when I click on the Submit button, I get a nullpointer exception in the employeeDetails() method:


and the server console displays the following exceptions:

ognl.OgnlException: target is null for setProperty(null, "department", [Ljava.lang.String;@acd4c9)
ognl.OgnlException: target is null for setProperty(null, "employeeID", [Ljava.lang.String;@c5dd98)
ognl.OgnlException: target is null for setProperty(null, "name", [Ljava.lang.String;@5753b0)

Please explain what is the problem and how can I fill the employee model object from the JSP. I do not want to create getter setter methods in the Action class and also I do not want to use the ModelDriven interface.

n_g n_g

The getter setter methods for the Employee object reference emp were missing in the MyAction class.

Problem resolved after creating the methods.