Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Reading an Array of Objects from a .bin file!

827803Jan 12 2011 — edited Jan 13 2011
Hey, at the moment I'm creating a program whereby I write a set of objects onto a bin file, in this case an array of objects with a String name/county, and int population/area.... I'm not trying to read it back from the bin file and encountering trouble! I've managed to get it working using plain objects, however I want to be able to read in arrays of objects. The file itself compiles, however during running I get this error

java.lang.NullPointerException
at RecordFiles.CoastalRead.main(CoastalRead.java:33)
at RecordFiles.__SHELL16.run(__SHELL16.java:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at bluej.runtime.ExecServer$3.run(ExecServer.java:774)


Here is the program itself,
 import java.io.*;

public class CoastalRead {

    public static void main(String[] args) {
        
        CoastalTowns towns[] = new CoastalTowns[50];
		towns[0] = new CoastalTowns();
		towns[1] = new CoastalTowns();
		towns[2] = new CoastalTowns();
		towns[3] = new CoastalTowns();
		towns[4] = new CoastalTowns();

		String name, county;
		int population, area; 
		
        
        try {
            
            FileInputStream fileIn =
                    new FileInputStream("CoastalTownsBin.bin");
                    
            ObjectInputStream in = new ObjectInputStream(fileIn);
            
            for (int i = 0; i < towns.length; i++){
            
             towns[i] =(CoastalTowns) in.readObject();

        	  System.out.println("Deserialized File...");
       		  System.out.println("Name: " + towns.name);
System.out.println("County: " + towns[i].county);
System.out.println("Population: " + towns[i].population);
System.out.println("Area: " + towns[i].area);

}

in.close();

fileIn.close();

} catch (IOException i) {}
catch (ClassNotFoundException c) {}


}

}
+I'd appreciate a fast reply!+ And if someone needs the writing class I've used, I can post it asap.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 10 2011
Added on Jan 12 2011
4 comments
319 views