Hi,
I get the following exception when I try to marshall a
java.util.Vector that contains
java objects
MyVector.main
Caught exception java.lang.NullPointerException
java.lang.NullPointerException
at org.apache.xml.serialize.Printer.printText(Unknown Source)
at org.apache.xml.serialize.XMLSerializer.startElement(Unknown
Source)
at
org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:1261)
at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:774)
at org.exolab.castor.xml.Marshaller.marshal(Marshaller.java:663)
at MyVector.main(MyVector.java:37)
I have verified that the Person object can be marshalled and
unmarshalled correctly.
The source code for this is attached with this message.
thanks,
~umesh
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import java.io.*;
import java.util.Vector;
public class MyVector implements java.io.Serializable {
public MyVector() {
super();
}
public static void main(String[] args) {
try {
System.out.println("MyVector.main");
Vector vector = new Vector();
Person person1 = new Person("Ryan 'Mad Dog' Madden");
person1.setAge(40);
vector.addElement(person1);
Person person2 = new Person("Sachin Tendulkar");
person2.setAge(30);
vector.addElement(person2);
// Create a File to marshal to
FileWriter writer = new FileWriter("myvector.xml");
if (vector == null) {
System.out.println("vector is null");
}
if (writer == null) {
System.out.println("writer is null");
}
// Marshal the person object
Marshaller.marshal(vector, writer);
System.out.println("MyVector.main marshalled object");
// Create a Reader to the file to unmarshal from
FileReader reader = new FileReader("myvector.xml");
// Marshal the person object
vector = (Vector)Unmarshaller.unmarshal(Vector.class, reader);
System.out.println("MyVector.main unmarshalled object");
System.out.println("MyVector.main myVector contains " + vector.size() + "
elements");
for (int ii = 0; ii < vector.size(); ii++) {
Person person = (Person) vector.elementAt(ii);
System.out.println("Person.main name is " + person.getName());
System.out.println("Person.main age is " + person.getAge());
}
}
catch (Throwable t) {
System.out.println("Caught exception " + t);
t.printStackTrace();
}
}
}
import java.util.Date;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import java.io.*;
/**
* A simple person class
*/
public class Person implements java.io.Serializable {
/**
* The name of the person
*/
private String name = null;
/**
* The Date of birth
*/
private int age = 0;
/**
* Creates a Person with no name
*/
public Person() {
super();
}
/**
* Creates a Person with the given name
*/
public Person(String name) {
this.name = name;
}
/**
* @return age of the person
*/
public int getAge() {
return age;
}
/**
* @return name of the person
*/
public String getName() {
return name;
}
/**
* Sets the age of the person
* @param age of the person
*/
public void setAge(int age) {
this.age = age;
}
/**
* Sets the name of the person
* @param name the name of the person
*/
public void setName(String name) {
this.name = name;
}
public static void main(String[] args) {
try {
System.out.println("Person.main");
Person person = new Person("Ryan 'Mad Dog' Madden");
person.setAge(40);
// Create a File to marshal to
FileWriter writer = new FileWriter("person.xml");
// Marshal the person object
Marshaller.marshal(person, writer);
System.out.println("Person.main marshalled object");
// Create a Reader to the file to unmarshal from
FileReader reader = new FileReader("person.xml");
// Marshal the person object
person = (Person)Unmarshaller.unmarshal(Person.class, reader);
System.out.println("Person.main unmarshalled object");
System.out.println("Person.main name is " + person.getName());
System.out.println("Person.main age is " + person.getAge());
}
catch (Throwable t) {
System.out.println("Caught exception " + t);
t.printStackTrace();
}
}
}