Re: Passing NULL Objects




"fb" <fb@xxxxxxxxx> wrote in message
news:wsAaf.402999$tl2.182196@xxxxxxxxxxx
> Hello everyone. I have the following:
>
> //Perform a search for the account number
> public BankAccount searchAccounts (Vector<BankAccount> bankAccounts ,
> long accountNumber){
> int accNum=0;
> for(int x=0;x<bankAccounts.size();x++){
> if(bankAccounts.elementAt(x) == accountNumber){
> return ?? //I want to return the object that matches from the
> Vector to the calling function
> else
> return ?? //How do I return a NULL object (The reason is that I
> can test for it in the returning function)
> }
>
>
> So...returning null objects. Any thoughts? :-)

Usually, returning a null is easy:
---
return null;
---

Or, more typically, you declare the object that is being returned and
initialize it to null at the beginning of the method. Then, the code in the
rest of the method tries to assign a value or set of values to the object
being returned but, if that fails, hands back the null object. For example:

---
public Integer searchAccounts(Vector bankAccounts) {

Integer accountNumber = null; //initialize object to be returned to null

//Determine int version of account number based on data in incoming Vector
int acctno = ....

return new Integer(acctno); [if no account number could be determined,
return a null]
}
---

Your situation is a bit more difficult since you can't assign a null to an
int value, which is a primitive, only to an object. So, you can either
change your logic to return an object - Integer would be a logical choice,
in which your code would look a lot like the example I just gave - or, you
can do what I often do in methods: return an Object array. The first thing
in the Object array is always a boolean which is set to true if the method
returned what it was supposed to find or set to false if the method
encounters a problem. Also, if the method found what it was supposed to
find, return the thing you were trying to find along with the boolean true;
if the method failed, return a boolean false and the error message that
explains what went wrong. For example, if the code is looking up the
customer's account number and gets a bad return code from the database
lookup, I'd pass back a false in the boolean, followed by the error message
describing the problem. That would look something like this:

---
public Object[] findCustomerName (int accountNumber) {

String customerName = null;

//Look up customer info based on account number
try {
customerName = ....
} catch (SQLException excp) {
Object[] Result = {new Boolean(false), excp.getMessage()};
return Result;
}

//If we get this far, the customer name was found.
Object[] Result = {new Boolean(true), customerName};
return Result;
}

---
Then, to invoke the method and check the result:
---
String custname = null;
Object[] Result = findCustomerName(12345);
boolean nameFound = ((Boolean) Result[0]).booleanValue(); //find out if
method worked
if (nameFound) {
custname = (String) Result[1];
} else {
String msg = (String) Result[1];
//do appropriate error handling - display error message? Write it to a
log?
}
---

That probably looks like extra work but it makes it possible for your method
to tell the invoker what specifically went wrong if there is a problem.
Although you COULD just pass back a null or some other value and possibly
infer the same thing, this approach makes it possible for you to tell the
invoker WHY something failed, which can improve your program's error
handling.

Rhino


.



Relevant Pages

  • Re: How can I use one email account across multiple users with WinXP
    ... then make it identical to the Admin user. ... Whenever I run OE from an administrator privilige account, I have no problems at all. ... I get the following error message: ... "Outlook Express could not be started. ...
    (microsoft.public.windows.inetexplorer.ie6_outlookexpress)
  • Re: Cant send emails HELP!!!
    ... > Secondly, if AVG is installed using the default install, its mail checking ... > restart Windows Mail and recreate the account. ... > error message, right-click on it, copy, then paste it into a reply here. ... have entered the server name correctly. ...
    (microsoft.public.windows.vista.mail)
  • Re: Error Message on startup
    ... I do, however, remember "insulting" the .NET installation on ... not been very successful in finding even the error message mentioned. ... OE did, however, work under a new TEST account I created. ... >>> Express, Knutt? ...
    (microsoft.public.windows.inetexplorer.ie6_outlookexpress)
  • RE: upload manager
    ... You need to run this Service under Local account or under this specific ... The Upload Manager service failed to start due to the following error: ... Exmaple for an error message please read it may apply to you: ... View and Manage Event Logs in Event Viewer in Windows XP ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: 0x800ccc0e & 0x800ccc0d
    ... The error message below occurs because there is not a valid ... mail server name specified. ... Go to Tools, Accounts, inspect that "POP3" account. ...
    (microsoft.public.windows.vista.mail)