Hello!

I am about to load details from a file and save it to a node. I am having an error regarding to a certain code. Here's the code::

boolean loadFromFile(String filename) throws Exception{
		boolean b = false;
		try{
			FileInputStream myfile = new FileInputStream(filename);
			ObjectInputStream in = new ObjectInputStream(myfile);
			do{
				start = (Node)(in.readObject()); /*This part is the error. Cannot cast a String into a Node */
				System.out.println(start.toString());
				b = true;
			}while(b == true);
			in.close();
		}catch (EOFException e){
			System.out.println("EOF");
			b = false;
		}catch(Exception e){
			System.out.println(e.getMessage());
			System.out.println("Cannot load file");
		}
		
		return b;
	}

You might also need the code for saveToFile. (since this might cause the problem)
boolean saveToFile(String filename) throws Exception{

boolean b = false;
		try{
			FileOutputStream myfile = new FileOutputStream(filename);
			ObjectOutputStream out = new ObjectOutputStream(myfile);
			System.out.println("Writing files...");
			index = start;
			do{
				out.writeObject(index); //Or out.writeObject(index.toString()) though I do not think this will work.
				index = index.next;
			}while(index!=null);
			out.close();
		
		}catch(Exception e){
			System.out.println(e.getMessage());
			System.out.println("File not existing.");
			b = false;
		}
		return b;
	}

Any suggestions regarding to this matter? Thank you very much.

Recommended Answers

All 3 Replies

Line #7 - in.readObject() method reads something which is not belonging to Node object. Can you maybe provide some more information about class Node.

Line #7 - in.readObject() method reads something which is not belonging to Node object. Can you maybe provide some more information about class Node.

class Node{
        private String elname; //surname
	private String efname; //first name
	private String work; //position
	private int floorno; //floor number
	Node next;
}
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.