Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

issue with a static method definition

 
vicky chauhan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • 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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add below code at beginning of your method.


As a side note: static methods are called Utility methods
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Yes... That was the issue..

thanks!!

- Vicky
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • 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 Java Windows XP
  • Mark post as helpful
  • send pies
  • 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
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic