2013-12-22 2 views
1

안녕하세요 저는 최근에 I/O 패키지를 시작했고 한 가지 문제에 직면했습니다. 아래 코드에서 볼 수 있듯이 클래스 ID가 비 직렬화 클래스 주소. ID 클래스와 함께 주소 개체를 저장하려고하지만 개체를 ​​읽는 동안 주소 개체를 가져올 수 없으며 주 클래스에서 null 포인터 예외가 발생했습니다. 아래에서 제 문제와 관련된 모든 코드를 표시했습니다. 필요하다. 이 라인을 통해 비 직렬화 가능 객체의 참조를 저장할 수 없습니다.

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());

코드 가능하면 rrect 널 pointer.please 공동을 얻고있다.

미리 감사드립니다.

+1

임시 주소 주소; 일시적인 속성은 유지되지 않습니다. – wxyz

답변

1

ID 개체를 저장하려면 transient을 제거하고 Serializable을 만들어 Identity 개체를 serialize 할 때 NotSerializableException이 표시되지 않도록해야합니다.

관련 문제