2012年4月5日 星期四

[Java] Null pointer handlings

In my previous project, I used to do this when handling the null pointers -
if (obj != null)
{
doSomething();
}


But I was told to do something like this to have a better result -

try
{
doSomething();
}
catch(NullPointerException npe)
{
App.reportException(npe);
}

Unless, the object is allow to have null as a legitimate return value, we can do this -

Bitmap bitmap = ResourceLoader.get(file);
if (bitmap == null)
{
loadBitmapInAnotherWay(file);
}

Well, all these code are conceptually working. You will have to work it out in your own way, according to your architecture. Copying and Pasting these code into yours will just bring you compiling errors. :)


沒有留言:

張貼留言