Writing to aserializable file
I am getting an error ( IOException ) when I try to write to a serializable file. The object to be saved in turn contains an object both of which are serializable. The following stack trace error is being printed:
Code :
java.io.NotSerializableException: GameObject
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
at SerializeGame.saveGame(SerializeGame.java:26)
at TicTacToeFinal.saveGame(TicTacToeFinal.java:379)
at TicTacToeFinal.playGame(TicTacToeFinal.java:174)
at TicTacToeFinal.newGame(TicTacToeFinal.java:88)
at TicTacToeFinal.menu(TicTacToeFinal.java:34)
at TicTacToeFinal.loadGame(TicTacToeFinal.java:343)
at TicTacToeFinal.menu(TicTacToeFinal.java:35)
at TicTacToeFinal.mainScreen(TicTacToeFinal.java:19)
at TicTacToeFinalTest.main(TicTacToeFinalTest.java:14)
Here is the GameObject class:
Code :
import java.io.*;
public class GameObject implements Serializable{
TicTacToeAlgorithm game;
int startWith;
int cChances;
int uChances;
GameObject(TicTacToeAlgorithm game, int startWith, int cChances, int uChances)
{
this.game=game;
this.startWith=startWith;
this.cChances=cChances;
this.uChances=uChances;
}
}
And following is the TicTacToeAlgorithm class:
Code :
import java.util.Random;
import java.util.Arrays;
import java.io.*;
public class TicTacToeAlgorithm implements Serializable{
int[][] grid;
int difficultyLevel;
public TicTacToeAlgorithm(int difficultyLevel)
{
grid=new int[9][9];
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
grid[i][j]=0;
}
this.difficultyLevel=difficultyLevel; // 1 easy 2 difficult
//................................... contains any methods
}
Following is the code that I have used to serialise the file:
Code :
import java.io.*;
public class SerializeGame {
ObjectOutputStream output;
public void openFile(){
try{
output = new ObjectOutputStream(new FileOutputStream( "savedGame.ser" ) );
}
catch(Exception e)
{
System.out.println("Error creating game file");
}
}
public void saveGame(GameObject gameObject)
{
openFile();
try{
output.writeObject(gameObject);
System.out.println("\nGame saved");
}
catch(IOException e)
{
System.out.println("Error saving game file: IOException");
e.printStackTrace();
}
closeFile();
}
public void closeFile()
{
try{
if(output!=null)
output.close();
}
catch(Exception e)
{
System.out.println("Error closing game file");
}
}
}
I am in very much need of it. Any help would be appreciated.
Re: Writing to aserializable file
Do you have a class for testing the posted code? I don't see a main() method here.
Re: Writing to aserializable file
I have tested them in another test (different from where I wanted them to be used) and everything worked fine:
Here is that test class:
Code :
import java.io.*;
public class delsergame implements Serializable{
TicTacToeAlgorithm a;
delsergame()
{
a=new TicTacToeAlgorithm(1);
}
}
Code :
import java.io.*;
public class delsergame2 {
ObjectOutputStream output;
public void openFile(){
try{
output = new ObjectOutputStream(new FileOutputStream( "delsergame2.ser" ) );
}
catch(Exception e)
{
System.out.println("Error creating game file");
}
}
public void saveGame(delsergame delser)
{
openFile();
try{
output.writeObject(delser);
System.out.println("\nGame saved");
}
catch(IOException e)
{
System.out.println("Error saving game file: IOException");
e.printStackTrace();
}
closeFile();
}
public void closeFile()
{
try{
if(output!=null)
output.close();
}
catch(Exception e)
{
System.out.println("Error closing game file");
}
}
}
Code :
import java.io.*;
public class delsergame3 {
public static void main(String args[])throws IOException
{
delsergame2 DSG=new delsergame2();
delsergame DG=new delsergame();
DSG.saveGame(DG);
}
}
Everything is fine in this test class. But when I use it in the class where I need it, it isnt working fine and here are those classes, partial code :
Code :
import java.io.*;
public class TicTacToeFinalTest {
public static void main(String args[])throws IOException
{
TicTacToeFinal a=new TicTacToeFinal();
a.mainScreen();
}
}
Code :
import java.io.*;
import java.util.Scanner;
public class TicTacToeFinal {
//---------------- code deleted
public void mainScreen()throws IOException
{
System.out.println("\n");
System.out.print("Welcome to Tic Tac Toe\nPress Enter to continue");
br.readLine();
menu();
}
//----------------------- code deleted
public void saveGame(TicTacToeAlgorithm game, int startWith, int cChances, int uChances)
{
GameObject gameObject=new GameObject(game, startWith, cChances, uChances);
SerializeGame sG=new SerializeGame();
sG.saveGame(gameObject);
}
public void playGame(TicTacToeAlgorithm game, int startWith, int cChances, int uChances)throws IOException
{
System.out.println("\nThe game starts\n");
if(startWith==1)
{
printGrid(game.grid);
while(((cChances>=1)||(uChances>=1)))
{
if(cChances>=1)
{
if(game.noMore(cChances,1,uChances,2))
{
System.out.println("\nThere exists no more possibility of gaining points");
break;
}
DualNum result=game.chooseNext();
cChances--;
System.out.println("\nComputer has choosen grid ["+result.x+"]["+result.y+"]\n");
printGrid(game.grid);
}
if(uChances>=1)
{
if(game.noMore(uChances,2,cChances,1))
{
System.out.println("\nThere exists no more possibility of gaining points");
break;
}
boolean correctChoice=false;
while(!correctChoice)
{
int x,y;
try{
System.out.print("\nEnter row number of the grid you wish to choose: ");
x=Integer.parseInt(br.readLine());
System.out.print("Enter column number of the grid you wish to choose: ");
y=Integer.parseInt(br.readLine());
}
catch(Exception e)
{
System.out.println("\nIncorrect choice entered");
continue;
}
if((x==-1)||(y==-1))
{
boolean menuOptionChoosen=false;int menuChoice=-1;
while(!menuOptionChoosen)
{
System.out.print("\n1. Exit game without saving\n2. Save game and exit\n\nEnter your choice: ");
try{
menuChoice=Integer.parseInt(br.readLine());
}
catch(Exception e)
{
System.out.println("\nIncorrect choice entered");
}
if(menuChoice==1)
{
menuOptionChoosen=true;
menu();
return;
}
if(menuChoice==2)
{
menuOptionChoosen=true;
saveGame(game,2,cChances,uChances);
System.out.println("\nPress Enter to return to main menu");
br.readLine();
menu();
return;
}
}
}
if((x>=0)&&(x<=8)&&(y>=0)&&(y<=8))
{
if((game.grid[x][y]==0))
{
game.setUser(x,y);
correctChoice=true;
uChances--;
}
}
if(correctChoice==false)
{
System.out.println("\nIncorrect choice entered");
}
}
System.out.println();
printGrid(game.grid);
}
}
}
// -------------- similarly for startsWith=2;
//-------------- code for remaining part of method
//-------------------------- other methods
And the TicTacAlgorithm class can be found in the main thread
I think that this would be sufficient for someone to help me. I will repluy with the netire code ( It is fairly long coming to around 2000. lines)
Re: Writing to aserializable file
Can you make one simple class for testing. You have posted 5 classes.
Re: Writing to aserializable file
The first three classes are the classes which together form the test class: one of them creates a new class which has a data member of type 'TicTacToeAlgorithm' and the second class is the class that contains methods for serialising and the third tests them. I have created the first class which in turn contains an object of another class because that is how my actual program works. Now these test classes are working fine. What I need is the same response when used in my actual classes. I feel that some problem exists in those actual classes which I am unable to figure out. That is why I have posted all the necessary code from those classes.
Re: Writing to aserializable file
Can you make it one simple class instead of 5 classes?
Re: Writing to aserializable file
That isn't possible. Each of the classes that I have created serve a different purpose. One of them contains the algorithm, one of them contains the non graphic user interface and so on. If you were asking me to make just one class for testing, then my answer is that it is working fine in that way. I have a problem in the format which I have posted ( 5 and even more classes)
Re: Writing to aserializable file
If you can get it to work by changing the way the classes are configured, that says there is a problem with the way your classes are defined. Now you should try different combinations of classes to find out which of your classes are the problem and then fix the class that is the problem.
It is a try this and try that process that you will have to do to find the problem. I would have to do the same thing. I'll leave it to you now that you know the process that needs to be done to find the problem.