当前位置:编程学习 > JAVA >>

【用action过滤相同用户名】下面的代码为什么不行?

package Action;

import jdbc.dao.impl.UserDaoImpl;
import jdbc.user.User;
import jdbc.dao.UserDao;
import com.opensymphony.xwork2.ActionSupport;

public class RegistAction extends ActionSupport {

private User user=new User();
UserDao udi=new UserDaoImpl();

public User getUser() {
return user;
}

public void setUser(User user) {
this.user = user;
}

public String execute()
{
//先查询user的name值是否存在
User user1=udi.findUser(user.getUsername(), null);
boolean flag=user1.getUsername().equals(user.getUsername());
 System.out.println("成功");
        if(flag==false)
        {
          System.out.println("成功");
          udi.addUser(user);
          System.out.println("成功");
          User user2=udi.findUser(user.getUsername(), user.getUserpass());
          if(user2!=null)
     {
              return "success";
     }
             else
         {return "error";}
       }
    else
     return "error";
}   

 
}




报错是:013-11-4 10:27:19 com.opensymphony.xwork2.util.logging.jdk.JdkLogger error
严重: Exception occurred during processing request: null
java.lang.NullPointerException
at Action.RegistAction.execute(RegistAction.java:25) --------------------编程问答-------------------- 异常信息不是说得很清楚了么
//先查询user的name值是否存在
如果没有匹配的 user1就为null了 --------------------编程问答--------------------

User user1=udi.findUser(user.getUsername(), null);    // 这里没有查询到user1是null,所以抛异常了。你至少对这个user1进行非空判断
--------------------编程问答-------------------- user1为null导致的nullpointerException,你检查一下后台是不是取到了前台传过来的user的参数?你的findUser方法贴出来看一下呗?
还有就是建议你24行后面加一下判空的逻辑,比如if(user1 == null || user1.equals("")){
    System.out.println("user is empty!");
} --------------------编程问答--------------------
引用 2 楼 huxiweng 的回复:

User user1=udi.findUser(user.getUsername(), null);    // 这里没有查询到user1是null,所以抛异常了。你至少对这个user1进行非空判断

额 不大明白啊,哎 ,现在比较晕 不知道怎么改? --------------------编程问答--------------------

import jdbc.dao.impl.UserDaoImpl;
import jdbc.user.User;
import jdbc.dao.UserDao;
import com.opensymphony.xwork2.ActionSupport;
 
public class RegistAction extends ActionSupport {
 
    private User user=new User();
    UserDao udi=new UserDaoImpl();
     
    public User getUser() {
        return user;
    }
 
    public void setUser(User user) {
        this.user = user;
    }
     
    public String execute()
    {
        //先查询user的name值是否存在
        User user1=udi.findUser(user.getUsername(), null);
        if(user1 == null){
System.out.println("user1 不存在");
return "error";
}   
        boolean flag=user1.getUsername().equals(user.getUsername());
         System.out.println("成功");
            if(flag==false)
            {
                 System.out.println("成功");
                 udi.addUser(user);
                 System.out.println("成功");
                 User user2=udi.findUser(user.getUsername(), user.getUserpass());
                 if(user2!=null)
                {
                     return "success";
                }
                 else
                 {return "error";}
           }
            else
                return "error";
        }   
         
                      
    }
    
--------------------编程问答--------------------
引用 5 楼 huxiweng 的回复:

import jdbc.dao.impl.UserDaoImpl;
import jdbc.user.User;
import jdbc.dao.UserDao;
import com.opensymphony.xwork2.ActionSupport;
 
public class RegistAction extends ActionSupport {
 
    private User user=new User();
    UserDao udi=new UserDaoImpl();
     
    public User getUser() {
        return user;
    }
 
    public void setUser(User user) {
        this.user = user;
    }
     
    public String execute()
    {
        //先查询user的name值是否存在
        User user1=udi.findUser(user.getUsername(), null);
        if(user1 == null){
System.out.println("user1 不存在");
return "error";
}   
        boolean flag=user1.getUsername().equals(user.getUsername());
         System.out.println("成功");
            if(flag==false)
            {
                 System.out.println("成功");
                 udi.addUser(user);
                 System.out.println("成功");
                 User user2=udi.findUser(user.getUsername(), user.getUserpass());
                 if(user2!=null)
                {
                     return "success";
                }
                 else
                 {return "error";}
           }
            else
                return "error";
        }   
         
                      
    }
    

但是这样的话,插入添加用户时,都进行判空操作进入了error界面,要肿么办~我好晕呀~ --------------------编程问答-------------------- user1的username不就是根据user的username查出来的么,判断user1是否为空就行了啊,还搞个flag干什么。
而且if(flag==false)这样的代码,最好写成if(!flag)
--------------------编程问答--------------------
引用 6 楼 sdauwcy 的回复:
Quote: 引用 5 楼 huxiweng 的回复:


import jdbc.dao.impl.UserDaoImpl;
import jdbc.user.User;
import jdbc.dao.UserDao;
import com.opensymphony.xwork2.ActionSupport;
 
public class RegistAction extends ActionSupport {
 
    private User user=new User();
    UserDao udi=new UserDaoImpl();
     
    public User getUser() {
        return user;
    }
 
    public void setUser(User user) {
        this.user = user;
    }
     
    public String execute()
    {
        //先查询user的name值是否存在
        User user1=udi.findUser(user.getUsername(), null);
        if(user1 == null){
System.out.println("user1 不存在");
return "error";
}   
        boolean flag=user1.getUsername().equals(user.getUsername());
         System.out.println("成功");
            if(flag==false)
            {
                 System.out.println("成功");
                 udi.addUser(user);
                 System.out.println("成功");
                 User user2=udi.findUser(user.getUsername(), user.getUserpass());
                 if(user2!=null)
                {
                     return "success";
                }
                 else
                 {return "error";}
           }
            else
                return "error";
        }   
         
                      
    }
    

但是这样的话,插入添加用户时,都进行判空操作进入了error界面,要肿么办~我好晕呀~


这是你逻辑上问题,想清楚如果找不到用户是进入error还是干嘛 --------------------编程问答--------------------
引用 6 楼 sdauwcy 的回复:
Quote: 引用 5 楼 huxiweng 的回复:


import jdbc.dao.impl.UserDaoImpl;
import jdbc.user.User;
import jdbc.dao.UserDao;
import com.opensymphony.xwork2.ActionSupport;
 
public class RegistAction extends ActionSupport {
 
    private User user=new User();
    UserDao udi=new UserDaoImpl();
     
    public User getUser() {
        return user;
    }
 
    public void setUser(User user) {
        this.user = user;
    }
     
    public String execute()
    {
        //先查询user的name值是否存在
        User user1=udi.findUser(user.getUsername(), null);
        if(user1 == null){
System.out.println("user1 不存在");
return "error";
}   
        boolean flag=user1.getUsername().equals(user.getUsername());
         System.out.println("成功");
            if(flag==false)
            {
                 System.out.println("成功");
                 udi.addUser(user);
                 System.out.println("成功");
                 User user2=udi.findUser(user.getUsername(), user.getUserpass());
                 if(user2!=null)
                {
                     return "success";
                }
                 else
                 {return "error";}
           }
            else
                return "error";
        }   
         
                      
    }
    

但是这样的话,插入添加用户时,都进行判空操作进入了error界面,要肿么办~我好晕呀~


25 26去掉

 27行换成这个吧  if(user1 == null) 判断用户名不存在就执行插入 如果已经存在了 就跳转到错误页面 --------------------编程问答--------------------
引用
boolean flag=user1.getUsername().equals(user.getUsername());
         System.out.println("成功");
            if(flag==false)

把这3句改成如下代码:
if(null==user1||!user1.getUsername().equals(user.getUsername())
--------------------编程问答--------------------
引用 8 楼 huxiweng 的回复:
Quote: 引用 6 楼 sdauwcy 的回复:

Quote: 引用 5 楼 huxiweng 的回复:


import jdbc.dao.impl.UserDaoImpl;
import jdbc.user.User;
import jdbc.dao.UserDao;
import com.opensymphony.xwork2.ActionSupport;
 
public class RegistAction extends ActionSupport {
 
    private User user=new User();
    UserDao udi=new UserDaoImpl();
     
    public User getUser() {
        return user;
    }
 
    public void setUser(User user) {
        this.user = user;
    }
     
    public String execute()
    {
        //先查询user的name值是否存在
        User user1=udi.findUser(user.getUsername(), null);
        if(user1 == null){
System.out.println("user1 不存在");
return "error";
}   
        boolean flag=user1.getUsername().equals(user.getUsername());
         System.out.println("成功");
            if(flag==false)
            {
                 System.out.println("成功");
                 udi.addUser(user);
                 System.out.println("成功");
                 User user2=udi.findUser(user.getUsername(), user.getUserpass());
                 if(user2!=null)
                {
                     return "success";
                }
                 else
                 {return "error";}
           }
            else
                return "error";
        }   
         
                      
    }
    

但是这样的话,插入添加用户时,都进行判空操作进入了error界面,要肿么办~我好晕呀~


这是你逻辑上问题,想清楚如果找不到用户是进入error还是干嘛



额。我只是想在添加用户之前,判断用户名是否存在,如果存在就不进行插入,如果不存在再插入的这样一个判断 --------------------编程问答-------------------- 看样子是逻辑不太清晰啊,
你使用user.getUserName()去查询user1, user1没有查到,报了个空指针,就证明已经起到过滤作用了,楼上的代码参考已下 --------------------编程问答--------------------
引用 10 楼 l568646976 的回复:
引用
boolean flag=user1.getUsername().equals(user.getUsername());
         System.out.println("成功");
            if(flag==false)

把这3句改成如下代码:
if(null==user1||!user1.getUsername().equals(user.getUsername())

还是不行,不能达到不插入相同用户名的数据的目的啊~~~ --------------------编程问答--------------------


import jdbc.dao.impl.UserDaoImpl;
import jdbc.user.User;
import jdbc.dao.UserDao;
import com.opensymphony.xwork2.ActionSupport;
 
public class RegistAction extends ActionSupport {
 
    private User user=new User();
    UserDao udi=new UserDaoImpl();
     
    public User getUser() {
        return user;
    }
 
    public void setUser(User user) {
        this.user = user;
    }
     
    public String execute()
    {
        //先查询user的name值是否存在
        User user1=udi.findUser(user.getUsername(), null);
        if(user1 == null){
System.out.println("user1 不存在");
User user = new User();
...
//做你的插入操作
System.out.println("插入成功");
return "success";
}   
        boolean flag=user1.getUsername().equals(user.getUsername());
         System.out.println("成功");
            if(flag==false)
            {
                 System.out.println("成功");
                 udi.addUser(user);
                 System.out.println("成功");
                 User user2=udi.findUser(user.getUsername(), user.getUserpass());
                 if(user2!=null)
                {
                     return "success";
                }
                 else
                 {return "error";}
           }
            else
                return "error";
        }   
         
                      
    }
    

--------------------编程问答--------------------
引用 14 楼 huxiweng 的回复:


import jdbc.dao.impl.UserDaoImpl;
import jdbc.user.User;
import jdbc.dao.UserDao;
import com.opensymphony.xwork2.ActionSupport;
 
public class RegistAction extends ActionSupport {
 
    private User user=new User();
    UserDao udi=new UserDaoImpl();
     
    public User getUser() {
        return user;
    }
 
    public void setUser(User user) {
        this.user = user;
    }
     
    public String execute()
    {
        //先查询user的name值是否存在
        User user1=udi.findUser(user.getUsername(), null);
        if(user1 == null){
System.out.println("user1 不存在");
User user = new User();
...
//做你的插入操作
System.out.println("插入成功");
return "success";
}   
        boolean flag=user1.getUsername().equals(user.getUsername());
         System.out.println("成功");
            if(flag==false)
            {
                 System.out.println("成功");
                 udi.addUser(user);
                 System.out.println("成功");
                 User user2=udi.findUser(user.getUsername(), user.getUserpass());
                 if(user2!=null)
                {
                     return "success";
                }
                 else
                 {return "error";}
           }
            else
                return "error";
        }   
         
                      
    }
    


额 判断的操作在execute后面吗? --------------------编程问答--------------------
引用 14 楼 huxiweng 的回复:


import jdbc.dao.impl.UserDaoImpl;
import jdbc.user.User;
import jdbc.dao.UserDao;
import com.opensymphony.xwork2.ActionSupport;
 
public class RegistAction extends ActionSupport {
 
    private User user=new User();
    UserDao udi=new UserDaoImpl();
     
    public User getUser() {
        return user;
    }
 
    public void setUser(User user) {
        this.user = user;
    }
     
    public String execute()
    {
        //先查询user的name值是否存在
        User user1=udi.findUser(user.getUsername(), null);
        if(user1 == null){
System.out.println("user1 不存在");
User user = new User();
...
//做你的插入操作
System.out.println("插入成功");
return "success";
}   
        boolean flag=user1.getUsername().equals(user.getUsername());
         System.out.println("成功");
            if(flag==false)
            {
                 System.out.println("成功");
                 udi.addUser(user);
                 System.out.println("成功");
                 User user2=udi.findUser(user.getUsername(), user.getUserpass());
                 if(user2!=null)
                {
                     return "success";
                }
                 else
                 {return "error";}
           }
            else
                return "error";
        }   
         
                      
    }
    

是应该先进行判断再插入值得吧?好晕、、、 --------------------编程问答--------------------
引用 10 楼 l568646976 的回复:
引用
boolean flag=user1.getUsername().equals(user.getUsername());
         System.out.println("成功");
            if(flag==false)

把这3句改成如下代码:
if(null==user1||!user1.getUsername().equals(user.getUsername())
如果这样还抛空指针异常,除非在User user1=udi.findUser(user.getUsername(), null);中的user还是空的。建议你在使用对象前,先对对象进行非空判断。 --------------------编程问答-------------------- 空指针异常。建议
1、判断user和user1是否空
2、判断user.getUsername()和user1.getUsername()是否为空
3、比较
补充:Java ,  Java EE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,