1

I have the following class, which performs some calculations to fill its static arrays.

public class Amount implements Serializable{
      private static final long serialVersionUID = 8141477444408242243L;
      public static Amount values1[][] = new Amount[10][30];
      public static Amount values2[][] = new Amount[10][30];
      public static Amount values3[][] = new Amount[10][30];

      double highestValue;
      double highestAmount;
      double lowestAmount;
      double lowestValue;

      ...
}

As the calculations take 20 minutes or so, I am looking to store the arrays on file and load the values when the program starts. I am attempting to use the java serialization method and have the following functions

public static void loadFile(Amount[][] arr, String filename){
    try {
        FileInputStream fis = new FileInputStream(filename);
        ObjectInputStream in = new ObjectInputStream(fis);
        arr = (Amount[][])in.readObject();
        in.close();
      }
      catch (Exception e) {
          System.out.println(e);
      }
}

public static void saveFile(Amount[][] arr, String filename){
     try {
         FileOutputStream fos = new FileOutputStream(filename);
         ObjectOutputStream out = new ObjectOutputStream(fos);
         out.writeObject(arr);
         out.flush();
         out.close();
      }
      catch (IOException e) {
          System.out.println(e); 
      }
}

which I call like this saveFile(values1, "valueOneSaveFile"); and loadFile(values1, "valueOneSaveFile");

I have run the program once, saving all the arrays to various files. The files have been created and look to be around the correct size. When I change my program to call the loadFile functions, the arrays do not appear to initialize correctly. I am getting null pointer exceptions when trying to read a value from the array (which appears to be empty after the load)

1
  • Have you considered using an embedded database instead of going out to file? :-) Commented Oct 22, 2011 at 16:11

2 Answers 2

3

The problem is in your LoadFile method. Java passes parameters by value. In the case of objects a copy of the "pointer" is passed. When you update the array:

arr = (Amount[][])in.readObject();

You are not updating Amount.values1 array, instead the local arr variable points to a new array.

You should change the method signature to:

public static Amount[][] loadFile(String filename)

And use it accordingly.

Sign up to request clarification or add additional context in comments.

2 Comments

I am slightly confused though. So in the original code, the first argument of the loadFile function will be passed in as a pointer. The function then creates a copy of this pointer, and updates that copy to point to a different area of memory where the result of the file load (i.e. in.readObject()) is stored. The pointer sent to the function remains unchanged and still points to an empty array. Is this correct
There aren't really pointers in Java. I guess that it is more correct to say references. What you wrote above is correct. The original array variable is never initialized and thus you get a NullPointerException when you access it.
0

It might be an issue with the readObject and writeObject methods that you are implementing in your Amount class. A comprehensive example can be found here.

You might also consider using XStream to save/load your data. It is very easy to use, as shown here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.