[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using getText() with a JTextField component?



Also remember that (sName.equals("Mike")) will throw a
NullPointerException if sName is null. Always check for null values or,
even better, if you have a constant value, use "Mike".equals(sName).
Looks strange but works and you needn't worry about that pesky
exception.
Ed.

Michal Wallace wrote:
> 
> On Wed, 1 Aug 2001, D. Michael Nelson wrote:
> 
> > //The following line works - prints "Mike
> > System.out.println(sName);
> >
> > //This condition fails
> > if (sName == "Mike")
> >   System.out.println("yes");
> >
> > Any suggestions?
> 
> Sure. This isn't a problem with getText, but a natural behavior of all
> java Strings. Try this instead:
> 
> if (sName.equals("Mike")) ...
> 
> The "==" operator compares identity, not value..
> 
> Cheers,
> 
> - Michal
> ----------------------------------------------------------------------
> let me host you! http://www.sabren.com       me: http://www.sabren.net
> ----------------------------------------------------------------------