这是我写的java注册账号的demo,输入各项信息后出现报错java.lang.NullPointerException。
经过简单的测试发现代码运行到 Test中的Integer count= UserService.register(user);,后面就报错了 请问这是什么情况导致的呢,应该怎么解决
下面是封装user用户的代码
public class user {
/**
* 用户类
* @author chenzihan
*/
private int userID ; // id主键
private String userAccount; //用户账号
private String userName;
private String password;
private String sex; // 性别
private String team; // 阵营
private String teamTribe; // 部落
private int money; // 金币数
private int thumbsUp ; // 点赞数
public int getUserID() {
return userID;
}
public void setUserID(int userID) {
this.userID = userID;
}
public String getUserAccount() {
return userAccount;
}
public void setUserAccount(String userAccount) {
this.userAccount = userAccount;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getTeam() {
return team;
}
public void setTeam(String team) {
this.team = team;
}
public String getTeamTribe() {
return teamTribe;
}
public void setTeamTribe(String teamTribe) {
this.teamTribe = teamTribe;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public int getThumbsUp() {
return thumbsUp;
}
public void setThumbsUp(int thumbsUp) {
this.thumbsUp = thumbsUp;
}
}
下面是接口
public interface UserService {
//注册新用户
Integer register(user user) throws SQLException;
//登录已有账号
Integer Login(user user);
}
下面是实现接口的类
```java
public class UserServiceImpl implements UserService {
//获取Connection连接
private Connection conn=jdbcUtil.getConnection();
public UserServiceImpl() throws SQLException {
}
/*
注册新用户
*/
@Override
public Integer register(user user) throws SQLException {
String sql="insert into user" +
"(userAccount,userName,Pword,sex,team,teamTribe,money,thumbsUp)" +
"value (?,?,?,?,?,?,0,0)";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1,user.getUserAccount());
pstmt.setString(2,user.getUserName());
pstmt.setString(3,user.getPassword());
pstmt.setString(4,user.getSex());
pstmt.setString(5, user.getTeam());
pstmt.setString(6,"暂未加入部落");
int count=pstmt.executeUpdate();
jdbcUtil.closeConnection(conn);
jdbcUtil.closePreparedStatement(pstmt);
return count;
}
@Override
public Integer Login(user user) {
return null;
}
}
下面是测试类
System.out.println("请输入账号:");
String userAccount=in.next();
System.out.println("请输入密码:");
String password=in.next();
user.setPassword(password);
Integer count= UserService.register(user);
if(count==1){
System.out.println("注册成功");
}else{
System.out.println("注册失败");
}
下面是报错信息