Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

issue with a static method definition

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Add below code at beginning of your method.


As a side note: static methods are called Utility methods
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
vicky chauhan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes... That was the issue..

thanks!!

- Vicky
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 76070
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 22662
127
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 76070
362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic