Results 1 to 9 of 9
  1. #1
    JavaNoobb is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Having trouble using XMLDecoder with Java linkedlist<parameter>

    Hi All,

    I am having trouble with a linkedlist when using XMLDecoder.

    I think I have confused myself completely with what I am doing.
    I get the following error.
    java.lang.ClassCastException: java.util.LinkedList cannot be cast to testarea.Tests
    Even though testarea.Tests has the following declaration in it.

    Java Code:
    public class Tests {
    	public LinkedList<Test> tests= new LinkedList<Test>();
            public void add(Test test){
    	tests.add(test);
           }
    The code that has an issue is in another class
    Java Code:
    tests=new Tests();
    readOK=true;
    System.out.println("Opening");
    readOK = true;
    
    try{
     XMLDecoder decoder = new XMLDecoder(new FileInputStream(TESTSFILE));
     tests =(Tests) decoder.readObject();
     decoder.close();		
    }catch (Exception e){
     System.out.println(e.toString());
     System.out.println("FileNotFound");
     readOK=false;
    }
    Hope this is enough info for someone to point me in the right direction as to what I am doing wrong.

    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Having trouble using XMLDecoder with Java linkedlist<parameter>

    I think I see how you're decoding your data, but how are you encoding your data? I wonder if you're encoding the LinkedList and not the Tests object?

  3. #3
    JavaNoobb is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: Having trouble using XMLDecoder with Java linkedlist<parameter>

    Thanks for the quick reply.

    Java Code:
    public void save(){
    		try{
      			XMLEncoder encoder = new XMLEncoder(new FileOutputStream(TESTSFILE));
      			encoder.writeObject(tests);
      			encoder.close();
      		}catch (Exception e){
      			
      		}
    	}

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Having trouble using XMLDecoder with Java linkedlist<parameter>

    It looks like you're writing out a LinkedList, not a Tests object, so it should be no surprise that you're seeing the error that you're seeing. Also, your code surely doesn't ignore exceptions like that does it?

  5. #5
    JavaNoobb is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: Having trouble using XMLDecoder with Java linkedlist<parameter>

    I am just trying to get my head around all this, so I haven't done any exception handling yet. How would I make it write out a Tests object instead then? And would that file contain all the objects in the list? Thank you for your time.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Having trouble using XMLDecoder with Java linkedlist<parameter>

    Quote Originally Posted by JavaNoobb View Post
    I am just trying to get my head around all this, so I haven't done any exception handling yet.
    Your catch block in the last code example was empty. You'll want to log errors or at least print out a stack trace.

    How would I make it write out a Tests object instead then? And would that file contain all the objects in the list?
    We'll probably need to see more of your code, and you'll want to use clearer variable name as your Tests class has a tests variable that's a linked list, and there's also another tests variable used elsewhere... a bit confusing to me.

  7. #7
    JavaNoobb is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: Having trouble using XMLDecoder with Java linkedlist<parameter>

    This code is for the linkedlist

    Java Code:
    import java.util.LinkedList;
    
    import testarea.Test;
    
    public class Tests {
    
    	public LinkedList<Test> tests= new LinkedList<Test>();
    	
    	public Tests(){
    		
    	}
    
    	
    	public void add(Test test){
    		Tests.add(test);
    	}
    	public void delete(Test test){
    		Tests.remove(test);
    	}
    	public Test[] getAllTests() {
    		int size = tests.size();
    		Test[] tests = new Test[size];
    		if(size>0){
    			for(int i = 0; i< size;i++){
    				tests[i]=this.tests.get(i);
    			}
    		}
    		return Tests;
    	}
    	
    }
    This is the code that should utilise the linkedlist and save/load it

    Java Code:
    
    import java.beans.XMLDecoder;
    import java.beans.XMLEncoder;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    public class DataBase {
    	/**
    	 * The linked list of Tests
    	 */
    	private Tests tests;
    
    	/*
    	 * The name of the XML file that contains the tests data used to read the saved data
    	 */
    	private final String TESTSFILE="Tests.xml";
    	
    
    	public DataBase(){
    		boolean readOK=true;
    		tests=new Tests();
    		readOK=true;
    		System.out.println("Opening");
    		readOK = true;
    
    		try{
    			XMLDecoder decoder = new XMLDecoder(new FileInputStream(TESTSFILE));
    			 tests =(Tests) decoder.readObject();
    			decoder.close();		
    		}catch (Exception e){
    			System.out.println(e.toString());
    			System.out.println("FileNotFound");
    			readOK=false;
    		}
    		if(!readOK){
    			
    		}else{
    		System.out.println("Tests Read In");
    		}
    	
    	}
    
    	public boolean saveSystem(){
    		boolean savedOK=true;
    		try{
      			XMLEncoder encoder = new XMLEncoder(new FileOutputStream(TESTSFILE));
      			encoder.writeObject(tests);
      			encoder.close();
      		}catch (Exception e){
      			
      		}
    		
      		return savedOK;
    	}
    	
    	public boolean addTest(Test test){
    		tests.add(test);
    		return true;
    	}
    	
    	public Test[] getAllTests(){
    		Test[] list = tests.getAllTests();
    		return list;
    	}
    
    }
    Thank you again for your time.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Having trouble using XMLDecoder with Java linkedlist<parameter>

    I can't compile nor run your code. Are you sure it's correct and is the code you're actually using? And what about your Test class?

  9. #9
    JavaNoobb is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: Having trouble using XMLDecoder with Java linkedlist<parameter>

    Sorry I will try copying again.

    Database Class
    Java Code:
    package Testing;
    
    import java.beans.XMLDecoder;
    import java.beans.XMLEncoder;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    public class DataBase {
    	/**
    	 * The linked list of Tests
    	 */
    	private Tests tests;
    
    	/*
    	 * The name of the XML file that contains the tests data used to read the saved data
    	 */
    	private final String TESTSFILE="Tests.xml";
    	
    
    	public DataBase(){
    		boolean readOK=true;
    		tests=new Tests();
    		readOK=true;
    		System.out.println("Opening");
    		readOK = true;
    
    		try{
    			XMLDecoder decoder = new XMLDecoder(new FileInputStream(TESTSFILE));
    			 tests =(Tests) decoder.readObject();
    			decoder.close();		
    		}catch (Exception e){
    			System.out.println(e.toString());
    			System.out.println("FileNotFound");
    			readOK=false;
    		}
    		if(!readOK){
    			
    		}else{
    		System.out.println("Tests Read In");
    		}
    	
    	}
    
    	public boolean saveSystem(){
    		boolean savedOK=true;
    		try{
      			XMLEncoder encoder = new XMLEncoder(new FileOutputStream(TESTSFILE));
      			encoder.writeObject(tests);
      			encoder.close();
      		}catch (Exception e){
      			
      		}
    		
      		return savedOK;
    	}
    	
    	public boolean addTest(Test test){
    		tests.add(test);
    		return true;
    	}
    	
    	public Test[] getAllTests(){
    		Test[] list = tests.getAllTests();
    		return list;
    	}
    
    }
    Test Class
    Java Code:
    package Testing;
    
    public class Test {
    
    	private String firstPara;
    
    	private String lastPara;
    
    	public void setFirstPara(String firstPara) {
    		this.firstPara = firstPara;
    	}
    
    	public String getFirstPara() {
    		return this.firstPara;
    	}
    
    	public void setLastPara(String lastPara) {
    		this.lastPara = lastPara;
    	}
    
    	public String getLastPara() {
    		return this.lastPara;
    	}
    	
    	public String toString(){
    		String result = "";
    		result = result + firstPara + ", ";
    		result = result + lastPara + ", ";
    		
    		return result;
    	}
    	
    }
    Tests Class
    Java Code:
    import java.util.LinkedList;
    
    public class Tests {
    
    	public LinkedList<Test> tests= new LinkedList<Test>();
    	
    	public Tests(){
    		
    	}
    
    	
    	public void add(Test test){
    		tests.add(test);
    	}
    	public void delete(Test test){
    		tests.remove(test);
    	}
    	public Test[] getAllTests() {
    		int size = tests.size();
    		Test[] tests = new Test[size];
    		if(size>0){
    			for(int i = 0; i< size;i++){
    				tests[i]=this.tests.get(i);
    			}
    		}
    		return tests;
    	}
    	
    }
    Start Class - Used to run the system
    Java Code:
    package Testing;
    
    public class Start {
    
    	public static void main(String[] args){
    		Test test = new Test();
    		DataBase mySystem = new DataBase();
    		Test[] tests; 
    		
    		test.setFirstPara("Test1");
    		test.setLastPara("Test2");
    		
    		mySystem.addTest(test);
    		
    		tests = mySystem.getAllTests();
    		for(int i=0; i < tests.length-1;i++){
    			System.out.println(tests[i].toString());
    		}
    		
    		if(!mySystem.saveSystem()){
    			System.out.println("Failed to save");
    		}else{
    			System.out.println("Saved");
    		}	
    		
    	}
    }
    Everything seems to work except for the saving/loading.

    Thanks for your time.

Similar Threads

  1. XMLEncoder e XMLDecoder
    By pamm in forum New To Java
    Replies: 6
    Last Post: 09-28-2011, 02:33 PM
  2. Replies: 18
    Last Post: 08-09-2011, 09:08 AM
  3. LinkedList-java
    By kamali in forum New To Java
    Replies: 2
    Last Post: 09-26-2008, 04:29 PM
  4. Problem with LinkedList Java
    By paul in forum Advanced Java
    Replies: 2
    Last Post: 08-07-2007, 05:57 AM
  5. Please help me with LinkedList in Java
    By lenny in forum New To Java
    Replies: 2
    Last Post: 07-31-2007, 03:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •