Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Writing to aserializable file

  1. #1
    Member
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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:
    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:
    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:
    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:
    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.
    Last edited by ranjithfs1; March 18th, 2012 at 07:14 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Writing to aserializable file

    Do you have a class for testing the posted code? I don't see a main() method here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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:


    import java.io.*;
    public class delsergame implements Serializable{
    	TicTacToeAlgorithm a;
    	delsergame()
    	{
    		a=new TicTacToeAlgorithm(1);
    	}
    }
    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");
    		}
    	}
    }

    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 :

    import java.io.*;
    public class TicTacToeFinalTest {
    	public static void main(String args[])throws IOException
    	{
    		TicTacToeFinal a=new TicTacToeFinal();
    		a.mainScreen();
    	}
    }


    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)
    Last edited by ranjithfs1; March 18th, 2012 at 07:58 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Writing to aserializable file

    Can you make one simple class for testing. You have posted 5 classes.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Writing to aserializable file

    Can you make it one simple class instead of 5 classes?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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)

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Password writing file
    By SilentNite17 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 5th, 2012, 10:07 PM
  2. new line in file writing
    By deependeroracle in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: January 30th, 2012, 10:06 AM
  3. file reading & writing
    By macko in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 12th, 2011, 08:54 AM
  4. [SOLVED] Writing " to a File
    By Sai in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 29th, 2010, 05:21 AM
  5. [SOLVED] Writing integers to a file
    By dubois.ford in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 06:18 PM