dcsimg
Invalid Class Exception
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Venkatesh_Sangam
Posted On:   Friday, May 24, 2002 12:12 AM

Hi, I am getting an Invalid class exception for some reason. I have an array that is sent over the socket. I will be sending this serializable object in a loop, each time sending a different array. The program runs fine for some time and suddenly crashes saying Invalid class Exception. I dont understand why it executes for some part of the loop and why it crashes in between. I have the following program. import java.io.*; public class GridServ implements java.io.Serializable{ private String serverArray[][]; private int noOfRows; private int noOfCols; public GridServ(){ serverArray = new String[50][50]; noOfRows = 0;   More>>

Hi,

I am getting an Invalid class exception for some reason.
I have an array that is sent over the socket.

I will be sending this serializable object in a loop, each time sending a different array.

The program runs fine for some time and suddenly crashes saying Invalid class Exception.
I dont understand why it executes for some part of the loop and why it crashes in between.

I have the following program.

import java.io.*;

public class GridServ implements java.io.Serializable{

private String serverArray[][];
private int noOfRows;
private int noOfCols;

public GridServ(){
serverArray = new String[50][50];
noOfRows = 0;
noOfCols = 0;
}

public void setGrid(String array[][]){
for (int i=0;i
for (int j=0;j
serverArray[i][j]=array[i][j];
}

public void setSize(int rows, int cols){
noOfRows = rows;
noOfCols = cols;
}

public void writeToFile(){
String outGrid = "/home/vsangam/grid/Grid.txt";
try{
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new
FileOutputStream(outGrid, false)));
for (int i=0;i
for (int j=0;j
out.write(serverArray[i][j]+" ");
out.newLine();
}
out.close();
}catch(Exception e){System.out.println("Exception while writing to file:GridServ.writeToFile() " +e);}



}

I my main program, I create a GridServ object and call the setGrid(array [][]) function.
In my receiving program, I just call the writeToFile() function that writes the grid array to a file.

It runs fine for some part of the loop, but the program crashes after sometime.
can anybody please explain why ??


Thanks,
Venkatesh.

   <<Less

Re: Invalid Class Exception

Posted By:   Les_Jones  
Posted On:   Wednesday, June 5, 2002 08:54 AM

Further to my other reply, re-reading I see you say you're getting an InvalidClassException. That's likely to come out during object serialization, which is not what you're doing. Although your GridServ class implements Serializable, the example you've given doesn't take advantage of that, and just writes out the data in your own specific format.



In other words, to be able to help, I need more information.

Re: Invalid Class Exception

Posted By:   Les_Jones  
Posted On:   Wednesday, June 5, 2002 08:22 AM

Firstly, in future can you include code include the tags
 .... code here ...  
so that it is readable.



Ok, what specifically is the error? ArrayIndexOutOfBoundsException per chance?



Also, you say you create the GridServ object and then call setGrid. Surely, with the code you have, you need to call setSize first, otherwise your setGrid will do nothing!



As an aside, why are you creating an array of size [50][50]? Why are you not allocating it to the right size when you know what the size should be?



Finally, why use the code :
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outGrid, false)));

when this does the same job?
BufferedWriter out = new BufferedWriter(new FileWriter(outGrid, false));
About | Sitemap | Contact