[英]Making a JTextArea serializable
当我尝试通过套接字发送此类时,我得到的只是NullPointException。 我将如何做到这一点,以免出现NullPoint异常?
public class Hick implements Serializable{
public JTextArea jta;
public Hick(){
jta = new JTextArea();
}
}
我用以下代码对其进行了测试,看来效果很好...
我将确保您可以先在本地序列化对象,以排除任何潜在的问题。 如果仍然无法通过套接字加载它,则您的套接字代码有问题,而不是序列化
public class TestSerialisation {
public static void main(String[] args) {
new TestSerialisation();
}
public TestSerialisation() {
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
Wrapper out = new Wrapper();
System.out.println("Before = " + out.dump());
try {
try {
oos = new ObjectOutputStream(new FileOutputStream(new File("Test.out")));
oos.writeObject(out);
} finally {
try {
oos.close();
} catch (Exception e) {
}
}
Wrapper in = null;
try {
ois = new ObjectInputStream(new FileInputStream(new File("Test.out")));
in = (Wrapper) ois.readObject();
} finally {
try {
ois.close();
} catch (Exception e) {
}
}
System.out.println("After = " + (in == null ? "null" : in.dump()));
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
public static class Wrapper implements Serializable {
private JTextArea textArea;
public Wrapper() {
textArea = new JTextArea("I'm some text");
}
public String dump() {
return textArea.getText();
}
}
}
还要确保您正在运行Java的兼容版本,并且(如果我的记忆适当地为我服务)在两端都具有兼容版本的序列化对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.