Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing int arrays to files

 
Toh Chun Zheng
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have a program that needs to save variables and objects to files
int noOfQuestions
int[] questionRange
Object survey

I did a search and found to write arrays you use the object reader:

FileOutputStream aka = new FileOutputStream(saveData);
ObjectOutputStream out = new ObjectOutputStream(aka);
out.writeObject(questionRange);
out.flush();
out.close();

To read the array:

ObjectInputStream inData = new ObjectInputStream(new FileInputStrea(saveData));

questionRange = (int[])inData.readObject();
inData.close();

However when I try to load the array from the file I get an IO error, and therefore my program ends encountering a NullPointerException in the array
Is there an error in saving or loading?
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch.
You must have missed our naming policy on the way in. In short, your displayed name should be a real (or at least real sounding) first and last name, separated by a space. You can change your name here.
As for your question, It is difficult to say because you gave us code we can't run, you didn't show us the stack trace of the exception or tell us which line the exception occurred on. Read through our FAQ, How to Ask Questions on JavaRanch, fill in some of the blanks and we'll see if we can help you.
 
Toh Chun Zheng
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi very sorry, I didn't know you didn't allow Asian names to show a surname and initials

The problem occurs in this line

questionRange = (int[])inData.readObject();

When I attempt to print the outputs of this array, it gives me a NullPointerException, its either I didn't write it to file properly, or I am missing something to load it from the file
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Toh Chun Zheng:
Hi very sorry, I didn't know you didn't allow Asian names to show a surname and initials

The problem occurs in this line

questionRange = (int[])inData.readObject();

When I attempt to print the outputs of this array, it gives me a NullPointerException, its either I didn't write it to file properly, or I am missing something to load it from the file


Take a look at the file and confirm that it has been written. Just make sure it exists. Your code for serializing an int array and deserializing it back looks fine.

Henry
 
Toh Chun Zheng
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again, I have checked the output file and confirmed it writes extra data as the program contiunes
My objects are unable to load properly, giving me NullPointerException when I try to print outputs

Most notably I catch an IOException when trying to read the array, and a ClassNotFoundException when I try to read the survey objects
[ February 24, 2008: Message edited by: Toh Chun Zheng ]
 
Henry Wong
author
Sheriff
Posts: 22542
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most notably I catch an IOException when trying to read the array, and a ClassNotFoundException when I try to read the survey objects


Please print the stacktrace of the IOException, and post it here.

Henry
[ February 24, 2008: Message edited by: Henry Wong ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!