Привет, команда недавно я начал с пакета ввода-вывода, и я столкнулся с одной проблемой. Как показано в приведенном ниже коде, у меня есть идентификатор класса, имеющий ссылку на несериализацию адрес класса. Я хочу сохранить объект адреса вместе с классом идентификации, но при чтении объекта я не могу вернуть объект адреса и он выбрасывает исключение нулевого указателя в моем основном классе. Ниже я показал весь код, связанный с моей проблемой, пожалуйста, сделайте необходимо.не удалось сохранить ссылку на несериализуемый объект
Address class--
package com.io.practice;
public class Address {
String state;
String country;
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
Identity Class--
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import com.io.practice.Address;
public class Identity implements Serializable {
private static final long serialVersionUID = 1L;
transient Address address;
int panCardId;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public int getPanCardId() {
return panCardId;
}
public void setPanCardId(int panCardId) {
this.panCardId = panCardId;
}
public void writeObject(ObjectOutputStream os) throws IOException {
os.defaultWriteObject();
os.writeObject(address);
}
public void readObject(ObjectInputStream is) throws IOException, ClassNotFoundException {
is.defaultReadObject();
this.address=(Address) is.readObject();
}
}
GraphTest main class--
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import com.io.practice.Address;
public class GraphTest {
public static void main(String[] args) throws IOException, ClassNotFoundException {
Identity id = new Identity();
Address add = new Address();
add.setCountry("Austrialia");
add.setState("Sidney");
id.setPanCardId(560100);
id.setAddress(add);
FileOutputStream fileOut = new FileOutputStream("D:/cgi.ser");
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
objectOut.writeObject(id);
objectOut.flush();
objectOut.close();
FileInputStream fileIn = new FileInputStream("D:/cgi.ser");
ObjectInputStream objectIn = new ObjectInputStream(fileIn);
Identity identity = (Identity) objectIn.readObject();
System.out.println(identity.getPanCardId());
System.out.println(identity.getAddress().getCountry());
}
}
System.out.println (identity.getAddress().getCountry());
по этой линии я получаю нулевой pointer.please дый правильный код, если это возможно.
Заранее спасибо.
переходный Адресный адрес; переходные атрибуты не сохраняются. – wxyz