Tuesday, 2 June 2009

NullPointerException in static factory-method in Spring

For one project, I was trying to test db connection at startup using some method like
public static void testConnection(DataSource ds){...}

which I happily configured in Spring using the factory-method attribute. To my surprise I got NullPointerException in populateMethod of Spring class AbstractAutowireCapableBeanFactory.

The reason for this was, Spring was expecting a object to be returned as it's a factory-method and fatory method will always return some object. Since my use case was to only test the connection and leave it I didnt return anything.

Hope this helps.

Monday, 1 June 2009

Binary To Text using Base64 encoding

If you need to convert binary data to text one of the recommended approach is to use base64 encoding and not direct string conversion like
byte[] byteArray = //get byte array from any image file
String str = new String(byteArray);

This is not recommended as it will not necessarily represent a valid character string in any encoding. Base64 will make sure that the byte array represents a valid ASCII character string
Ex byte[] byteArray = //get byte array from any image file
String str = new Base64Encoder().encode(byteArray);

To support this point lets take an XML String with the str String padded between some xml element like
String xmlString = <imagedata>;

This xml when parsed for the image
InputSource inputSource = new InputSource(new StringReader(xmlString));
DocumentBuilder builder = DocumentBuilderFactory.newInstance() .newDocumentBuilder();
Document document = builder.parse(inputSource);

will throw an error in the first case ie using String saying
org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0x0) was found in the element content of the document.

So a cleaner approach is to use Base64 encoding.