Results 1 to 2 of 2
  1. #1
    lsbill is offline Member
    Join Date
    Apr 2012
    Location
    Austin TX
    Posts
    2
    Rep Power
    0

    Default XMLEncoder: discarding statement ArrayList.add

    I am trying to serialize an object that contains an ArrayList as part of the object. However, I keep getting an error:

    Java Code:
    Writing Race
    java.lang.InstantiationException: com.longswordllc.testEncoder.structs$Car
    Continuing ...
    java.lang.Exception: XMLEncoder: discarding statement ArrayList.add(structs$Car);
    Continuing ...
    Here is my code:

    structs.java
    Java Code:
    package com.longswordllc.testEncoder;
    
    public class structs {
    	
    	public structs() {
    		
    	}
    	
    	public class Car {
    		private String color;
    		
    		public Car() {
    			color = null;
    		}
    		
    		public String getColor() {
    			return color;
    		}
    		
    		public void setColor(String c) {
    			color = c;
    		}
    	}
    }
    Race.java
    Java Code:
    package com.longswordllc.testEncoder;
    
    import java.util.ArrayList;
    import com.longswordllc.testEncoder.structs.Car;
    
    public class Race {
    	private ArrayList<Car> texasRace;
    	
    	public Race() {
    		
    	}
    	
    	public ArrayList<Car> getRace() {
    		return texasRace;
    	}
    	
    	public void setRace(ArrayList<Car> a) {
    		texasRace = a;
    	}
    }
    mainClass.java
    Java Code:
    package com.longswordllc.testEncoder;
    
    import java.beans.XMLEncoder;
    import java.io.BufferedOutputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    
    import com.longswordllc.testEncoder.structs.*;
    
    public class mainClass {
    
    	public static void main(String[] args) {
    		structs myStructs = new structs();
    		Race myRace = new Race();
    		Car myCar = myStructs.new Car();
    		myCar.setColor("Blue");
    		
    		ArrayList<Car> someCars = new ArrayList<Car>();
    		someCars.add(myCar);
    		myRace.setRace(someCars);
    		
    		System.out.println("Writing Race");
    		
    		// XML Encode the race object
    		try {		
    			XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream( new FileOutputStream("race.xml")));
    			encoder.writeObject(myRace);
    			encoder.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    And here is the resulting xml code:
    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    -<java class="java.beans.XMLDecoder" version="1.6.0_31"> -<object class="com.longswordllc.testEncoder.Race"> -<void property="race"> <object class="java.util.ArrayList"/> </void> </object> </java>
    I have tried to ensure i use getter/setters and no argument constructors. not sure what i am missing.

    I am still kind of new to serializing. Thanks in advance for any help. I have been stumped for days.

  2. #2
    lsbill is offline Member
    Join Date
    Apr 2012
    Location
    Austin TX
    Posts
    2
    Rep Power
    0

    Default Re: XMLEncoder: discarding statement ArrayList.add

    I fixed it! I had to get rid of the structs class and make it the race class. Then pull the Car class into it's own class file. a few changes to the mainClass and it work. Here is updated code that shows how to make it work. Not sure why it works, though. Im still a new guy to java.

    Car Class
    Java Code:
    package com.longswordllc.testEncoder;
    
    public class Car{
    	private String color;
    	
    	public String getColor() {
    		return color;
    	}
    	
    	public void setColor(String c) {
    		color = c;
    	}
    }
    Race Class
    Java Code:
    package com.longswordllc.testEncoder;
    
    import java.util.List;
    
    public class Race {
    	private List<Car> myCars;
    
    	public List<Car> getCars() {
    		return myCars;
    	}
    	
    	public void setCars(List<Car> data) {
    		myCars = data;
    	}
    }

    mainClass class

    Java Code:
    package com.longswordllc.testEncoder;
    
    import java.beans.XMLEncoder;
    import java.io.BufferedOutputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    public class mainClass {
    
    	public static void main(String[] args) {
    		Car myCar = new Car();
    		myCar.setColor("Blue");
    		Car aCar = new Car();
    		aCar.setColor("Red");
    		
    		List<Car> texasRace = new ArrayList<Car>();
    		texasRace.add(myCar);
    		texasRace.add(aCar);
    		
    		Race myRace = new Race();
    		myRace.setCars(texasRace);
    		
    		System.out.println("Writing Race");
    		
    		// XML Encode the race object
    		try {		
    			XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream( new FileOutputStream("race.xml")));
    			encoder.writeObject(texasRace);
    			encoder.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2012, 05:27 PM
  2. XMLEncoder e XMLDecoder
    By pamm in forum New To Java
    Replies: 6
    Last Post: 09-28-2011, 02:33 PM
  3. Replies: 12
    Last Post: 07-07-2011, 09:49 PM
  4. Replies: 7
    Last Post: 11-22-2010, 02:26 PM
  5. Replies: 1
    Last Post: 01-10-2009, 03:43 AM

Tags for this Thread

Posting Permissions

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