Jump to content
Cyber_Star

Null Pointer Explanation

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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 by Jack McKalling

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...