如何克服java.lang.NullPointerException? [英] How can I overcome java.lang.NullPointerException?
问题描述
当我想将数据保存到数据库中时,会引发异常.
When I want to save data into database it's throws exception.
我的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
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异常的异常
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:
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
推荐答案
您遇到逻辑错误.我注意到您的废话if语句似乎总是返回true.
You have a logic error. I have noticed that you have a nonsense if statement that seems that it will always return true.
if ((session.get("name") != null) || ((session.get("name") == null)))
您正在此处使用or语句进行检查(|| = OR),无论该语句是否为null,在两种情况下该语句都是正确的.
Your are checking with an or statement here ( || = OR ) the statement will be true in both cases if it is null or not.
您可能想要更多类似的东西:
you probably want something more along the lines of:
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的原因,也可能不是,但是这两种方式都可以解决将来涉及该语句的问题.
this could be the cause of your current nullPointerException, or it might not, but either way this will fix possible problems in the future involving that statement.
这篇关于如何克服java.lang.NullPointerException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!