当我想将数据保存到数据库时,它会抛出异常。
我的Jsp如下:
的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>
当我运行应用程序时,我得到了像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";
}
}
}
这个例外我得到了如何克服我不知道
答案 0 :(得分:0)
您有逻辑错误。我注意到你有一个无意义的if语句似乎总是会返回true。
if ((session.get("name") != null) || ((session.get("name") == null)))
您正在检查或声明此处(|| = OR)如果该声明为空,则该声明将为真。
你可能想要更多的内容:
if (session.get("name") != null)
{
do the is NOT null actions
}else if(session.get("name") == null)
{
do the IS null actions
(probably put a warning message here to help you better diagnose null problems with your session)
}
这可能是您当前nullPointerException的原因,或者它可能不是,但无论哪种方式,这将解决将来涉及该语句的可能问题。