使用serializable的Android套接字流,错误:java.lang.NullPointerException

时间:2012-11-10 02:56:31

标签: android sockets nullpointerexception serializable

我使用Serializable进行数据传输。以下是来自Serializable的课堂内容。

public class UserAction implements Serializable{

/**
 *
 */
private static final long serialVersionUID = 1L;

public int userID;
public transient Path path;
public transient Paint paint;

public UserAction(){
    path = new Path();
    paint = new Paint();
}

public UserAction(int ID, Path path, Paint paint){
    setUserID(ID);
    setPath(path);
    setPaint(paint);
}

public void setUserID(int ID){
    this.userID = ID;
}

public void setPath(Path path){
    this.path.set(path);
}

public void setPaint(Paint paint){
    this.paint.set(paint);
}

}

OutputStream就像这样:

 client = new Socket("localhost", 54321);
 ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream());
 out.writeObject(action);
 out.close();
 client.close();

InputStream是:

 ObjectInputStream in = new ObjectInputStream(client.getInputStream());
 rec_action = (UserAction) in.readObject();
 in.close();

但是当我使用rec_action从输入流中获取时。 rec_action不是Null,但是它的成员路径和paint都是NULL!获取 java.lang.NullPointerException

1 个答案:

答案 0 :(得分:0)

您将这些成员标记为transient,因此序列化会跳过它们。这就是该关键字的定义。