hey friends :D!

alright i was tryin to work with serialization basics and now i am stuck with this problem of reading an object from a file named 0x.txt.

and i know ths program is really a hell lot full of bugs but i have just started sp plz dont mind.

the program works one way when i do this

Num Nx[]=new Num[20];			//working
			Num nn=new Num();

			while( (nn=(Num)In.readObject())!=null)
			{
				Nx[range]=nn;
				System.out.println(Nx[range++]);
			}

but i dont understand why it doesnt works with this when i guess it should

Num Nx[]=(Num[])In.readObject();	//non-working-->throws here an exception

okay here's the full code

import java.io.*;


class Num implements Serializable
{ 
	private int w;
	private int l;

	public Num()
	{
		w=l=0;
	}
	public Num(int we,int lo)
	{
		if(we>=0)
			w=we;
		if(lo>=0)
			l=lo;
	}
	public void Setw(int we)
	{
		if(we>=0)
			w=we;
	}
	public void Setl(int lo)
	{
		if(lo>=0)
			l=lo;
	}
	public int Getl()
	{
		return l;
	}
	public int Getw()
	{
		return w;
	}
	public String toString()
	{
		return w+"+"+l;
	}
}

class FOS
{
	public static void main(String args[]) throws IOException
	{
		Num N[]=new Num[20];
		int range=0;

		ObjectOutputStream Ou=new ObjectOutputStream(new FileOutputStream("D:\\0x.txt"));
 
		DataInputStream Di=new DataInputStream(System.in);

		System.out.println("\nInput The Number Of Nums");
		int n=Integer.parseInt(Di.readLine());

		for(int i=0;i<n;i++)
		{
			System.out.println("\nEnter W:");
			int a=Integer.parseInt(Di.readLine());

			System.out.println("\nEnter L:");
			int b=Integer.parseInt(Di.readLine());

			N[i]=new Num(a,b);

			Ou.writeObject(N[i]);
		}

		Ou.close();

		try
		{
			System.out.println("\nThe Number Of Nums Are As Follows:");

			ObjectInputStream In=new ObjectInputStream(new FileInputStream("D:\\0x.txt"));

			Num Nx[]=(Num[])In.readObject();	//non-working-->throws here an exception

			//-------------------------------------------------------------
			/*Num Nx[]=new Num[20];			//working
			Num nn=new Num();

			while( (nn=(Num)In.readObject())!=null)
			{
				Nx[range]=nn;
				System.out.println(Nx[range++]);
			}*/
			//------------------------------------------------------------

			for(;range<Nx.length;range++)
				System.out.println(Nx[range]);

			In.close();
		}
		catch(Exception e)
		{
			System.out.println("EX");
		}
	}
}

so yep thanks in advance :D

and again all i need is understanding of why the program aint working with this

Num Nx[]=(Num[])In.readObject();	//non-working-->throws here an exception

any suggestions will be welcomed :D

You write a series of Num objects to the file. They happen to be from an array, but that's irrelevant. The file contains just a series of Nums, one after the other.
This is absolutely not the same as writing a array object to the file, so there's no way you can try to read it as a single array object.
If you want to write a whole load of Num objects in a single go, put them in a Collection object such as an ArrayList

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.