• Post Reply Bookmark Topic Watch Topic
  • New Topic

Implement the static method [solved]  RSS feed

 
Mark Moge
Ranch Hand
Posts: 100
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have found this exercise

Implement the static method isLicenseNumber(String s) that returns true if the given string is a legal (Finnish) license plate number, otherwise false. To verify legality, it's enough to check that:
- the string's length is 7 AND
- a dash '-' is found at index 3 (123-ABC is considered legal)
My soultion

This code works fine at my computer but on the page with exercise (www.javala.cs.tut.fi) I get "Your code threw an exception: java.lang.NullPointerException".
Is this my fault or error in exercise?


Ok i didn't check if s != null
 
Bert Wilkinson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your method looks ok. Without seeing the code you are using to test it, you are probably getting into trouble by passing it a String that is not initialized.

If you pass this to your method it will fail:

String test = null;
isLicenseNumber (test);

Get used to looking at the javadoc for the objects you are using....if you open the javadoc for the String object, it clearly states:

Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!