Hi,
I have a FieldHandler for storing values of an HashMap to a file. I worked without any problems in Castor-0.9.3.19 which was the latest version when I wrote the FieldHandler.
I have upgraded castor to 0.9.4 and FieldHandler throws a NPE.I debugged the code and looks like Unmarshal code calls Validate, which in turn call FieldHandlerImpl.getValue().
I never expected FieldHandler's getValue() to be called while Unmarshalling XML to Object.
Here is the trace for the Exception
java.lang.NullPointerException
at com.dorado.is.persist.castor.TxnNamedObjectsHandler.getValue(TxnNamed
ObjectsHandler.java:71)
at org.exolab.castor.mapping.loader.FieldHandlerImpl.getValue(Unknown So
urce)
at org.exolab.castor.xml.FieldValidator.validate(Unknown Source)
at org.exolab.castor.xml.util.XMLClassDescriptorImpl.validate(Unknown So
urce)
at org.exolab.castor.xml.Validator.validate(Unknown Source)
at org.exolab.castor.xml.UnmarshalHandler.endElement(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.endElement(AbstractSAXPar
ser.java:552)
at org.apache.xerces.impl.XMLNamespaceBinder.endElement(XMLNamespaceBind
er.java:646)
at org.apache.xerces.impl.dtd.XMLDTDValidator.handleEndElement(XMLDTDVal
idator.java:2978)
at org.apache.xerces.impl.dtd.XMLDTDValidator.endElement(XMLDTDValidator
.java:918)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.handleEndElemen
t(XMLDocumentFragmentScannerImpl.java:1145)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(
XMLDocumentFragmentScannerImpl.java:988)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContent
Dispatcher.dispatch(XMLDocumentFragmentScannerImpl.java:1446)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(XM
LDocumentFragmentScannerImpl.java:333)
at org.apache.xerces.parsers.StandardParserConfiguration.parse(StandardP
arserConfiguration.java:529)
at org.apache.xerces.parsers.StandardParserConfiguration.parse(StandardP
arserConfiguration.java:585)
at org.apache.xerces.parsers.XMLParser.parse(XMLParser.java:147)
at org.apache.xerces.parsers.AbstractSAXParser.parse(AbstractSAXParser.j
ava:1148)
at org.exolab.castor.xml.Unmarshaller.unmarshal(Unknown Source)
at org.exolab.castor.xml.Unmarshaller.unmarshal(Unknown Source)
I get NPE as I access the Objects's member in getValue() which is not yet set during Unmarshalling.
Here is a small use case scenario that I am running into.
public class SomClass
implements FieldHandler
{
public Object getValue( Object object )
throws IllegalStateException
{
SampleObject sb = (SampleObject)object;
String valueToStore = sb.getValueToStore();
String fileToStoreIn = sb.getFileName(); // This is set after the object is created.
storeInFile(fileToStoreIn, valueToStore); // NOT IMPLEMENTED IN THIS CLASS
return fileToStoreIn;
}
public void setValue( Object object, Object value )
throws IllegalStateException, IllegalArgumentException
{
String st = readFromFile((String)value); // NOT IMPLEMENTED IN THIS CLASS
(SampleObject)object.setValueToStore(st);
}
public void resetValue( Object object )
throws IllegalStateException, IllegalArgumentException
{
//-- add reset code here
}
/**
* @deprecated No longer supported
*/
public void checkValidity( Object object )
throws ValidityException, IllegalStateException
{
//-- add validation check if desired
}
public Object newInstance( Object parent )
throws IllegalStateException
{
return new String();
}
}
I get NPE in the above case because validate calls SomeClass.getValue() where sb.getFileName() throw exception as it not yet set. This happens during Unmarshalling.
What is the RIGHT way to solve this problem?
Why has the behaviour changed in Castor 0.9.4
I appreciate any response or help.
Thanks
Suresh
