这段代码为什么会报
空指针异常服务器端:public void registerNewUser() {
//将用户信息插入数据库中的sql语句
String sql = "INSERT INTO USER_INFO (accNUM,NAME,PASSWORD," +
"SIGN,PIC,SEX,EMAIL,BIRTHDAY) VALUES(?,?,?,?,?,?,?,?)";
//读取qq号码的sql语句
String sql2 = "SELECT accNUM FROM ACCNUM WHERE ID = 1";
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql2);
rs.next();
int acc = rs.getInt("accNUM");
PreparedStatement pre1 = conn.prepareCall(sql);
acc += 1; //将QQ号加1
//下面读取客户端发来的信息
String name = in.readUTF();
String password = in.readUTF();
//String ip = in.readUTF();
String sign = in.readUTF();
String pic = in.readUTF();
String sex = in.readUTF();
String email = in.readUTF();
String birthday = in.readUTF();
pre1.clearParameters();
pre1.setInt(1, acc);
pre1.setString(2, name);
pre1.setString(3, password);
//pre1.setString(4, ip);
pre1.setString(4, sign);
pre1.setString(5, pic);
pre1.setString(6, sex);
pre1.setString(7, email);
pre1.setString(8, birthday);
System.out.println(sql);
pre1.executeUpdate();
//更改QQ号的sql语句
String sql3 = "UPDATE ACCNUM SET accNUM = ? WHERE ID = 1";
PreparedStatement pre2 = conn.prepareCall(sql3);
pre2.clearParameters();
pre2.setInt(1, acc);
pre2.executeUpdate();
out.writeUTF("registerOver");
//向用户返回注册的QQ号
out.writeInt(acc);
rs.close();
stmt.close();
} catch (Exception ex) {
try {
out.writeUTF("registerFail");
} catch (
IOException ex1) {
ex1.printStackTrace();
}
ex.printStackTrace();
}
}
客户端: public long registerNewUser(String name, String password, String sign,String pic, String sex, String email,String birthday) {
long acc = 0;
String serverInfo = "";
try {
Socket socket = new Socket(logAddress, serverPort);
DataInputStream in = new DataInputStream(socket.getInputStream());
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.writeUTF("registerNewUser");
out.writeUTF(name);
out.writeUTF(password);
out.writeUTF(sign);
out.writeUTF(pic);
out.writeUTF(sex);
out.writeUTF(email);
out.writeUTF(birthday);
serverInfo = in.readUTF();
if (serverInfo.equals("registerFail")) {
return 0;
} else {
acc = in.readInt();
}
} catch (IOException ex) {
ex.printStackTrace();
}
return acc;
}
------解决方案--------------------哪個是null??
nullpointexception的處理方式是找出哪個是null,然後做null判斷,或是排出發生null的情況。
------解决方案--------------------表示没得异常提示
------解决方案--------------------地址要指向服务器地址
端口要与服务器绑定的地址一样
------解决方案--------------------