0
當我嘗試通過套接字發送這個類時,我得到的所有回覆是NullPointException。我如何製作它,以便我不會得到NullPoint異常?製作一個JTextArea序列化
public class Hick implements Serializable{
public JTextArea jta;
public Hick(){
jta = new JTextArea();
}
}
當我嘗試通過套接字發送這個類時,我得到的所有回覆是NullPointException。我如何製作它,以便我不會得到NullPoint異常?製作一個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的兼容版本和(如果我沒有記錯正確)在兩端都有兼容的序列化對象版本。
發佈代碼,您如何嘗試發送它。 – Mordechai
我使用ObjectOutputStream發送它。我想要做的就是讓jta可序列化 – Barakados
好吧,我不能猜測問題,發佈代碼,這可能會有所幫助。順便說一句,所有的擺動組件都是可串行化的。 – Mordechai