Convert null object to String

71,137

Solution 1

Use this,

try {
    deleteuserDetails.setCreatedBy(result.getPropertyAsString(0).toString());
}
catch(Exception e) {
    deleteuserDetails.setCreatedBy("null");
}

Solution 2

Instead of catching the exception or putting conditions, use String.valueOf(result.getPropertyAsString(0));

It will call toString() method of the argument and will convert it to String and if result.getPropertyAsString(0) is null, then it will change it to "null".

Solution 3

Although it's not a good practice, you can concatenate the null value with "" to make it a String.

For example:

String str=null;
System.out.println((str+"").length()); /// prints 4

Solution 4

result.getPropertyAsString(0) alone will result in a NPE already when the property is internally null. Your stacktrace points to SoapObject.getPropertyAsString(SoapObject.java:165) which should be

public String getPropertyAsString(int index) {
    PropertyInfo propertyInfo = (PropertyInfo) properties.elementAt(index);
    return propertyInfo.getValue().toString();
}

source - it will crash when propertyInfo or propertyInfo.getValue() is null.

To prevent that from happening you need to get the property not via getPropertyAsString but via getProperty and convert it manually to a String.

You can encapsulate that into some utility method

public static String getPropertyAsString(SoapObject object, int index) {
    Object prop = object.getProperty(index);
    if(prop instanceof PropertyInfo) {
        prop = ((PropertyInfo)prop).getValue();
    }
    return String.valueOf(prop); // will make it "null" if it is null
}

and then do

deleteuserDetails.setUserId(getPropertyAsString(result, getPropertyAsString(4)));
deleteuserDetails.setUserName(getPropertyAsString(result, getPropertyAsString(2)));
deleteuserDetails.setUserRole(getPropertyAsString(result, getPropertyAsString(3)));

Solution 5

Try this:

deleteuserDetails.setCreatedBy(result.getPropertyAsString(0) == null ? "null": result.getPropertyAsString(0));
Share:
71,137
Vivek Shankar
Author by

Vivek Shankar

Iam just a infant developer,Please bear with my questions.Kindly dont ban me from asking questions pls

Updated on July 29, 2022

Comments

  • Vivek Shankar
    Vivek Shankar almost 2 years

    I have written an android program to load values to table-row from web service. But value comes null so I need to convert it into a string. Can someone tell me the method to do it?

    try{                    
    
        SoapObject request = service.getRequest();
        SoapSerializationEnvelope envelope = service.getEnvelope(request);
        SoapObject response = service.getResponse(envelope);
        Log.i("Service Master", response.toString());
        int count = response.getPropertyCount();
        for (int i = 0; i < count; i++) {
            SoapObject result = (SoapObject) response.getProperty(i);
            DeleteuserDetails deleteuserDetails=new DeleteuserDetails();
            deleteuserDetails.setUserId(result.getPropertyAsString(4));
            deleteuserDetails.setUserName(result.getPropertyAsString(2));
            deleteuserDetails.setUserRole(result.getPropertyAsString(3));
            deleteuserDetails.setCreatedDate(result.getPropertyAsString(1));
            deleteuserDetails.setCreatedBy(result.getPropertyAsString(0));
            userdetail.add(deleteuserDetails);
        }
    

    Now deleteuserDetails.setCreatedBy(result.getPropertyAsString(0)); gets null value from webservice, so I need to convert it into string "null".

    LogCat:

    12-20 18:48:52.608: W/System.err(2174): java.lang.NullPointerException
    12-20 18:48:52.608: W/System.err(2174):     at org.ksoap2.serialization.SoapObject.getPropertyAsString(SoapObject.java:165)
    12-20 18:48:52.608: W/System.err(2174):     at com.mvss.admin.Deleteuser$deteUserIdLoad.doInBackground(Deleteuser.java:81)
    12-20 18:48:52.608: W/System.err(2174):     at com.mvss.admin.Deleteuser$deteUserIdLoad.doInBackground(Deleteuser.java:1)
    12-20 18:48:52.608: W/System.err(2174):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
    12-20 18:48:52.608: W/System.err(2174):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    12-20 18:48:52.608: W/System.err(2174):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    12-20 18:48:52.608: W/System.err(2174):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
    12-20 18:48:52.608: W/System.err(2174):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
    12-20 18:48:52.608: W/System.err(2174):     at java.lang.Thread.run(Thread.java:1096)
    
  • NickL
    NickL over 11 years
    Then you are still calling .toString() on getPropertyAsString. Which you can't do, because if getPropertyAsString returns null, it is like calling null.toString().
  • Vivek Shankar
    Vivek Shankar over 11 years
    could u please tell me elabrately
  • Avadhani Y
    Avadhani Y over 11 years
    Place the condition as: if(result.getPropertyAsString(0).toString()!=null){deleteuse‌​rDetails.setCreatedB‌​y(result.getProperty‌​AsString(0).toString‌​());}
  • NickL
    NickL over 11 years
    His problem is, that getPropertyAsString == null, so calling .toString() on that will cause a NPE.
  • Avadhani Y
    Avadhani Y over 11 years
    ok @NickL then change the code as: if(result.getPropertyAsString(0)!=null){deleteuserDetails.se‌​tCreatedB‌​y(result.‌​getPropertyAsString(‌​0).toString());}
  • NickL
    NickL over 11 years
    @Avadhani then the NULL value still isn't converted to a "null" String. See other answers.
  • Avadhani Y
    Avadhani Y over 11 years
    @NickL What i am trying to tell is check whether the value result.getPropertyAsString(0) is null or not... thats it... if null return; else convert it to string... is it not the correct way to do....?
  • NickL
    NickL over 11 years
    I know, but he wants to 'convert' the NULL value to a "NULL" String, a String which contains the text "NULL". In his current code he is trying to do this with .toString(), which ofcourse will throw a NPE when called on a NULL value.
  • Vivek Shankar
    Vivek Shankar over 11 years
    what can i do NickL and Avadhani
  • Avadhani Y
    Avadhani Y over 11 years
    @user1858826 Please try to use the condition which i placed above and let me know if there are any issues....
  • NickL
    NickL over 11 years
    Could do what Zapl said, it is the only answer that is different then the other 8 answers.
  • Vivek Shankar
    Vivek Shankar over 11 years
    try { deleteuserDetails.setCreatedBy(result.getPropertyAsString(0)‌​.toString()); } catch(Exception e) { deleteuserDetails.setCreatedBy("null"); }
  • Vivek Shankar
    Vivek Shankar over 11 years
    ya tried the above one and if exception rises i stored it as null
  • Vivek Shankar
    Vivek Shankar over 11 years
  • Vivek Shankar
    Vivek Shankar over 11 years
  • NickL
    NickL over 11 years
    No, he caught the exception and then inserted "null". Like Gamb suggested. But @user1858826 is a bad user and probably isn't going to accept an answer anyways.
  • Vivek Shankar
    Vivek Shankar over 11 years
    who said NickL nothing like that
  • Keith Tyler
    Keith Tyler over 3 years
    It may not be good practice, and I don't know why it might not be, but it certainly is effective, simple, reliable, and doesn't seem to have any negative side effects. Although I might advocate that the string value of null ought to be "".