• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Returning an object or null

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm taking a class in J2EE programming, and we are writing a servlet which reads in login information from a user (from an HTML form), and then is supposed to authenticate the user. The authentication is accomplished via a method in a separate "AccountDelegate" class. There is also a UserAccount class which creates user account objects. We are not connecting to a database, but just have several hardcoded user accounts stored in the AccountDelegate class (UserAccount objects). If the login information matches one of the hard coded user accounts we are supposed to return a UserAccount object. If it doesn't match we are supposed to return null (which indicates login failure). This is the part that is giving me problems. In the servlet, I call the authentication method as follows:
m_user = new UserAccount(AccountDelegate.authenticateUser(strUsername, hashPassword));
This works fine if the method authenticateUser returns a UserAccount object. The UserAccount class has a constructor which takes another UserAccount object. However, if authenticateUser returns null (i.e. because I have passed it an invalid username and/or password), it fails.
How should I call this method from the servlet such that I can deal with it returning either an object or null?
Thanks,
Mike
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why can't you do the following

UserAccount ua = AccountDelegate.authenticateUser
(strUsername,hashPassword));
if(ua != null)
m_user = new UserAccount(ua);
else
m_user = null;
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An alternative would be to have the UserAccount constructor throw an exception if it gets a null value or otherwise unsuitable input. I like to use IllegalArguementException for this purpose - naturally you should provide for catching the exception and returning null.
Bill
 
Michael Cleary
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! I will play around w/ these ideas for a bit - in between shoveling snow/ice! I need to get this part done so I can move on to the rest of the project (behind schedule as usual....)
Thanks again,
Mike
 
Michael Cleary
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In reference to the first response, how should "ua" be initialized?
Using this method, the result is always an invalid user. It appears that even if a valid UserAccount object is returned, ua still turns out to be null.
For example, as a check I inserted the line: String name = ua.getID() after the call to the authentication method. Now, even if I input a valid username and password, this line always generates a NullPointerException. So apparently, ua is not getting assigned the attributes of the returned object.
My UserAccount constructors are as follows:
public UserAccount()
{
}

public UserAccount(String userID, String userPassword, String userRole)
{
setID(userID);
setPassword(userPassword);
setRole(userRole);
}

public UserAccount(UserAccount account)
{
this(account.m_strID, account.m_strPassword, account.m_strRole);
}
Thanks,
Mike
 
Phani Paladugu
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then I guess you must be doing something incorrectly in the
AccountDelegate.authenticateUser(strUsername,hashPassword));
If you can send the code for AccountDelegate.authenticateUser method
this can be clarified.
Also the solution proposed by bill is a good a way resolving this.
 
Michael Cleary
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here it is:
public static UserAccount authenticateUser(String id, String password) throws BusinessException
{
int count = 0;
for(int i = 0; i < 5; i++)
{
if(m_users[i].getID().equals(id))
{
m_currentUser = new UserAccount(m_users[i]);
break;
}
count++;
}//for
if(count < 5)
{
if(m_currentUser.getPassword().equals(password))
{
return m_currentUser;
}
else
{
return null;
}

else
{
return null;
}

}//end authenticateUser
Actually, I got it to work by adding another catch block in the servlet code:
try
{
m_user = new UserAccount(AccountDelegate.authenticateUser(strUsername, hashPassword));
}
catch(BusinessException badUser)
{
}
catch(NullPointerException badValue)
{
m_user = null;
}
The "BusinessException" is our own exception class which, quite frankly doesn't appear to do anything....
Thanks,
Mike
 
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But, I think the BusinessException is along the lines of what Bill was referring to, For example in your

whenever you hava an invalid user i.e. returning a null value, you can throw a BussinessException("Invalid User") or something along those lines.
That way you want have to define another catch block such as NullPointerException. The BusinessException fits along the lines of what you are trying to accomplish. It is an exception created just for the application(authenticateUser) use, it is an anticipated error and a recoverable error.
I hope this makes sense.
Craig.
 
Michael Cleary
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So if I get an invalid password (or ID) in authenticateUser, I might do something like:
if(m_currentUser.getPassword().equals(password))
{
return m_currentUser;
}
else
{
throw new BusinessException("my message");
return null;
}
Or did I misunderstand?
Thanks,
Mike
 
Michael Cleary
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks CJ! I did the following and it works like a charm!
In the method authenticateUser:
if(m_currentUser.getPassword().equals(password))
{
return m_currentUser;
}
else
{
throw new BusinessException("Invalid User Password");
}
I did the same for an invalid user ID.
Thanks again!
Mike
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic