How to handle null string in java -
i .net programmer , new in java. facing problem in handling null string in java. assigning value string array string variable completeddate. tried didn't work.
string completedate; completedate = country[23]; if(country[23] == null && country[23].length() == 0) { // ... } if (completedate.equals("null")) { // ... } if(completedate== null) { // ... } if(completedate == null || completedate.equals("null")) { // ... }
for starters...the safest way compare string against potentially null value put guaranteed not-null string first, , call .equals on that:
if("constantstring".equals(completeddate)) { // logic } but in general, approach isn't correct.
the first one, commented, generate nullpointerexception it's evaluated past country[23] == null. if it's null, doesn't have .length property. meant call country[23] != null instead.
the second approach compares against literal string "null", may or may not true given scope of program. also, if completeddate null, fail - in case, rectify described above.
your third approach correct in sense it's thing checking against null. typically though, want logic if object wanted wasn't null.
your fourth approach correct accident; if completeddate null, or short-circuit. true if completeddate equal literal "null".
Comments
Post a Comment