aspose file tools*
The moose likes Beginning Java and the fly likes issue with a static method definition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "issue with a static method definition" Watch "issue with a static method definition" New topic
Author

issue with a static method definition

vicky chauhan
Ranch Hand

Joined: Nov 27, 2008
Posts: 41
I have to write a static method which does a simple check to the passed string:

a) whether the string length is of seven characters
b) whether the 4th character is a dash '-'

if both are true then return true, else return false

The method is:


public static boolean isLicenseNumber(String s)
{
if(s.length()==7 && s.charAt(3)=='-')
return true;

return false;
}


but the code gives null pointer exception. I don't know why. I observed that the code atleast compiles if i am removing "&& s.charAt(3)=='-'". So some issue with comparison of characters. But I don't know what is wrong in my comparison.

Please help!!

Thanks,
Nikunj
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Add below code at beginning of your method.


As a side note: static methods are called Utility methods
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Please post the StackTrace. The possible reason would be that the String being passed is null.
You can make the change as suggested by Seetharaman.


Mohamed Sanaulla | My Blog
vicky chauhan
Ranch Hand

Joined: Nov 27, 2008
Posts: 41
Yes... That was the issue..

thanks!!

- Vicky
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Seetharaman Venkatasamy wrote:... As a side note: static methods are called Utility methods

I wouldn't say that. Some static methods may be utility methods but not all.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Wouter Oet wrote:I wouldn't say that. Some static methods may be utility methods but not all.

I was wrong. you are right!. I would have written that *Some static methods may be utility methods*.

Thank you for your kind correction
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Seetharaman Venkatasamy wrote:Add below code at beginning of your method.



Of course we don't really know what semantics the OP wants but it would seem to me more likely that an IllegalArgumentException should be thrown if 's' is null.

Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Agree about throwing an Exception, but I would have thought an NPE would be customary in this instance.
Also never write if ... return true; else return false; You simply put the return keyword earlier. I don't agree with everything in this style guide, but it's §10.5.2.You can create a regular expression which will match your requirements.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Campbell Ritchie wrote:Agree about throwing an Exception, but I would have thought an NPE would be customary in this instance.


My general philosophy is to use an IllegalArgumentException if the values passed to a method do not meet the contract for the method argument. I can't remember ever explicitly creating a NPE (though I've experienced my fair share by accident :-) ) . I'm not religious about this but to my mind any NPE represent an internal programming error in the code throwing the NPE.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

James Sabre wrote:
Campbell Ritchie wrote:Agree about throwing an Exception, but I would have thought an NPE would be customary in this instance.


... I can't remember ever explicitly creating a NPE (though I've experienced my fair share by accident :-) ) . ...


I dont think Campbell meant to throw NPE because the code automatically would throw a NPE. Agree with throwing IllegalArgumentException. The important thing is that the End user/developer should be indicated of what went wrong.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

I disagree. NPE and IAE are both caused by programming errors, but I often throw NPEs when input to my constructor or setter is null while it shouldn't be. True, if possible I let the NPE being thrown by simply using the null reference, but sometimes (especially in constructors and setters) you don't use the argument immediately, you just store it for future use. And for me, NPE is then more logical since null is used where it shouldn't be. But I must admit that an IAE is also possible, since the argument is indeed invalid.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Agree with Rob. You could throw an IAE or an NPE in that situation, but NPE is probably more specific to passing null by mistake. I also think Jmes Sabre and I are in agreement, excpet maybe about the kind of Exception.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: issue with a static method definition