0

I am trying to deserialize a single variable in java however seem to be bumping into a problem when casting the value:

public void loadGrid(Sudoku testGrid) {
    try {
        FileInputStream fileIn = new FileInputStream(System.getProperty("user.home") + "\\tmp\\grid.grd");
        ObjectInputStream in = new ObjectInputStream(fileIn);
        testGrid.sudokuGrid = (Sudoku.sudokuGrid) in.readObject();
        in.close();
        fileIn.close();
    } catch(IOException i) {
        i.printStackTrace();
    } catch(ClassNotFoundException c) {
        c.printStackTrace();
    }
}

Sudoku is a class containing an ArrayList of ArrayList variable. I run into:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Sudoku.sudokuGrid cannot be resolved to a type
4
  • So, shouldn't the cast be to something like (List<ArrayList<?>>) in.readObject(); ? Commented Jan 4, 2015 at 15:19
  • Do you have any class within Sudoku named sudokuGrid? Commented Jan 4, 2015 at 15:20
  • Yes, what is sudokuGrid? a field in class Sudoku? casts are types, not variables. What are you trying to do? Commented Jan 4, 2015 at 15:21
  • SudokuGrid is the variable Commented Jan 4, 2015 at 15:22

1 Answer 1

3

You need to cast to the type of Sudoku.sudokuGrid, not the sudokuGrid field itself:

testGrid.sudokuGrid = (ArrayList<ArrayList<SomeType>>) in.readObject();
Sign up to request clarification or add additional context in comments.

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.