Jump to content

Null Pointer Explanation


Cyber_Star

Recommended Posts

Does anyone know about null pointer in java. for example : why we use String id = se.request.getParameter("id");......................if (("").equals(id) || ("null").equals(id)) {//some codes here}instead of usingif(id.equals("") || id.equals("null")) {//some codes here}Anyway, i need some clear explanations here. Thanks for helping.

Link to comment
Share on other sites

  • 2 weeks later...
Does anyone know about null pointer in java. for example : why we use String id = se.request.getParameter("id");......................if (("").equals(id) || ("null").equals(id)) {//some codes here}instead of usingif(id.equals("") || id.equals("null")) {//some codes here}Anyway, i need some clear explanations here. Thanks for helping.
null does not have any method, including equals(), so if id is null, id.equals(...) will generate NullPointerException.
Link to comment
Share on other sites

  • 3 weeks later...

It is also possible to just check if something is null, or to use try..catch:

if (id == null || id.equals("")){ // if ID is null, the .equals() will not be compiled}try{ if (id.equals("")) { /* id is an empty string */ }}catch (Exception e){ // id might be null, or some other error occured}
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...