Convert null object to String

2019-02-16 11:30发布

I have written an android program to load values to tablerow from web service. But a value comes null so I need do convert it into string. Can some one 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)

9条回答
Ridiculous、
2楼-- · 2019-02-16 11:35

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 wil change it to "null"

查看更多
Luminary・发光体
3楼-- · 2019-02-16 11:36

"Hi this will be " + null;

Prints out "Hi this will be null" in String

查看更多
姐就是有狂的资本
4楼-- · 2019-02-16 11:38

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)));
查看更多
趁早两清
5楼-- · 2019-02-16 11:40
if(result.getPropertyAsString(0)==null)
{
deleteuserDetails.setCreatedBy("");
}
else
{
deleteuserDetails.setCreatedBy(result.getPropertyAsString(0).toString());
}
查看更多
冷血范
6楼-- · 2019-02-16 11:45

Instead of that, place the condition if(String!=null).

查看更多
仙女界的扛把子
7楼-- · 2019-02-16 11:47

Try this:

deleteuserDetails.setCreatedBy(result.getPropertyAsString(0) == null ? "null": result.getPropertyAsString(0));
查看更多
登录 后发表回答