Java 使JTextArea可序列化
当我试图通过套接字发送这个类时,我得到的只是NullPointException。如何使其不出现NullPoint异常Java 使JTextArea可序列化,java,sockets,nullpointerexception,jtextarea,serializable,Java,Sockets,Nullpointerexception,Jtextarea,Serializable,当我试图通过套接字发送这个类时,我得到的只是NullPointException。如何使其不出现NullPoint异常 public class Hick implements Serializable{ public JTextArea jta; public Hick(){ jta = new JTextArea(); } } 我用下面的代码测试了它,它似乎工作得很好 我会确保您可以首先在本地序列化对象,以排除任何潜在的问题。如果您仍然无法通过套接字加载它,那么您的套接字代码是错误的,而不
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,并且(如果我的内存正常的话)两端都有兼容版本的序列化对象。发布代码,说明您如何尝试发送它。我使用ObjectOutputStream发送它。我想做的是使jta可序列化好吧,我不能仅仅猜测问题所在,发布代码,这可能会有所帮助。顺便说一句,所有swing组件都是可序列化的。提供堆栈跟踪也可能有所帮助。