Cyber_Star Posted August 4, 2009 Share Posted August 4, 2009 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 More sharing options...
cshao Posted August 13, 2009 Share Posted August 13, 2009 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 More sharing options...
Jack McKalling Posted September 1, 2009 Share Posted September 1, 2009 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.