Avoiding Null Pointer Exceptions.

Hi:

This might sound like a newbie question.

In a funtion that returns an object of another class, if the required parameters are all rights (i.e no 'problems' ) the funtion works well. But in case, I have to return something (but not 'throwable' an exception) I return a 'null'. And this obviously is not a very good thing.

Please let me know what I can return such that the accepting method doesnt throw NPE.

Cheers,

Mack.

public TorsionBeamStressDistributor GetTBSDfromID(String tbsdid){

try{

TorsionBeamStressDistributor tbsdid_focal =new TorsionBeamStressDistributor(tbsdid);

return BootStrap.getStressPadManager().getBeamStructureManager().find(tbsdid_focal);//long as all is well.

}

catch(Exception e){

System.out.println(e.getMessage());

}

returnnull;// I want to have a better alternative than this... returning null.

}

[1383 byte] By [MackTrucka] at [2007-10-2 13:23:24]
# 1

Given that method signature, your only choices are to throw an

exception or return a TorsionBeamStressDistributor or to return null.

You could create a Null Object and return it. In short, it's an object

which implements the same interface as a valid TBSD, and returns

acceptable values while not really being a TBSD. Googling for

Null Object should give you plenty to go on.

es5f2000a at 2007-7-13 11:01:32 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2
Fantastic, I think I have a handle on it now.Some steel bars want to thank you! :)Cheers.
MackTrucka at 2007-7-13 11:01:32 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...