When I want to save data into database it's throws exception.
My Jsp as follows:
list.jsp
<body>
<s:form action="userActionForm">
<s:submit value="Add"/>
</s:form>
<div class="content">
<table class="userTable" cellpadding="5px">
<tr class="even">
<th>First Name</th>
<th>Last Name</th>
<th>Contact</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<s:iterator value="userList" status="userStatus">
<tr
class="<s:if test="#userStatus.odd == true ">odd</s:if> <s:else>even</s:else>">
<td><s:property value="fName" /></td>
<td><s:property value="lName" /></td>
<td><s:property value="contact" /></td>
<td><s:url id="editURL" action="editUser">
<s:param name="id" value="%{id}"></s:param>
</s:url> <s:a href="%{editURL}">Edit</s:a></td>
<td><s:url id="deleteURL" action="deleteUser">
<s:param name="id" value="%{id}"></s:param>
</s:url> <s:a href="%{deleteURL}">Delete</s:a></td>
</tr>
</s:iterator>
</table>
</div>
<s:a href="logOut">LogOut</s:a>
</body>
register.jsp
<body>
<s:form action="saveOrUpdateUser" method="post">
<s:push value="userdata">
<s:hidden name="id" />
<s:textfield name="First name" label="Enter Name"/>
<s:textfield name="Lst Name" label="Enter Lst Name"/>
<s:textfield name="Contact" label="Enter Contact"/>
<s:submit />
</s:push>
</s:form>
</body>
UserAction.java
public class UserAction extends ActionSupport implements ModelDriven<UserData> {
private UserData userdata = new UserData();
private UserDAO userDAO = new UserDAOImpl();
setters & getters
@Override
public UserData getModel() {
System.out.println("userdata = ==" + userdata.getName());
return userdata;
}
public String saveOrUpdate() {
System.out.println("user data" + userdata);
userDAO.saveOrUpdateUser(userdata);
return SUCCESS;
}
}
UserDAO
public interface UserDAO
{
public void saveOrUpdateUser(UserData userData);
}
UserDAOImpl
public class UserDAOImpl implements UserDAO
{
@SessionTarget
private Session session;
@TransactionTarget
Transaction transaction;
public void saveOrUpdateUser(UserData userdata) {
try {
session.saveOrUpdate(userdata);
} catch (Exception e) {
transaction.rollback();
e.printStackTrace();
}
}
Struts.xml
<struts>
<constant name="struts.devMode" value="true" />
<package name="helloworld" extends="hibernate-default">
<interceptors>
<interceptor name="mylogging" class="Demo.AuthenticationInterceptor"> </interceptor>
<interceptor-stack name="loggingStack">
<interceptor-ref name="mylogging" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="loggingStack"></default-interceptor-ref>
<action name="saveOrUpdateUser" method="saveOrUpdate" class="Demo.UserAction">
<result name="success" type="redirect" >list.jsp</result>
<result name="login">/login.jsp</result>
</action>
</package>
</struts>
When I run the application I got exception like NullPointer Exception
java.lang.NullPointerException
at Demo.UserDAOImpl.saveOrUpdateUser(UserDAOImpl.java:36)
at Demo.UserAction.saveOrUpdate(UserAction.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:440)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:279)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:242)
AuthenticationInterceptor:
public class AuthenticationInterceptor implements Interceptor
{
public String intercept(ActionInvocation ai) throws Exception
{
System.out.println("inside the interceptor()......new");
Map session = ai.getInvocationContext().getSession();
String name = (String) session.get("name");
System.out.println("inside the session or loginaction=" + name);
if ((session.get("name") != null) || ((session.get("name") == null))) {
System.out.println("inside the session or loginaction ");
return ai.invoke();
} else {
return "login";
}
}
}
This exception I got how to overcome I don't know
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…