Cyber_Star 0 Posted August 4, 2009 Report 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. Quote Link to post Share on other sites
cshao 0 Posted August 13, 2009 Report 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. Quote Link to post Share on other sites
Jack McKalling 2 Posted September 1, 2009 Report Share Posted September 1, 2009 (edited) 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} Edited September 1, 2009 by Jack McKalling Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.