将空对象转换为字符串

将空对象转换为字符串

本文介绍了将空对象转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个 android 程序来将值从 Web 服务加载到表行.但是 value 为 null,所以我需要将其转换为字符串.谁能告诉我怎么做?

尝试{SoapObject 请求 = service.getRequest();SoapSerializationEnvelope 信封 = 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 结果 = (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);}

现在 deleteuserDetails.setCreatedBy(result.getPropertyAsString(0)); 从 webservice 获取空值,所以我需要把它转换成字符串 "null".>

LogCat:

12-20 18:48:52.608: W/System.err(2174): java.lang.NullPointerException12-20 18:48:52.608: W/System.err(2174): 在 org.ksoap2.serialization.SoapObject.getPropertyAsString(SoapObject.java:165)12-20 18:48:52.608: W/System.err(2174): 在 com.mvss.admin.Deleteuser$deteUserIdLoad.doInBackground(Deleteuser.java:81)12-20 18:48:52.608:W/System.err(2174):在 com.mvss.admin.Deleteuser$deteUserIdLoad.doInBackground(Deleteuser.java:1)12-20 18:48:52.608:W/System.err(2174):在 android.os.AsyncTask$2.call(AsyncTask.java:185)12-20 18:48:52.608: W/System.err(2174): 在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)12-20 18:48:52.608: W/System.err(2174): 在 java.util.concurrent.FutureTask.run(FutureTask.java:137)12-20 18:48:52.608: W/System.err(2174): 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)12-20 18:48:52.608: W/System.err(2174): 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)12-20 18:48:52.608: W/System.err(2174): 在 java.lang.Thread.run(Thread.java:1096)
解决方案

使用这个,

尝试{deleteuserDetails.setCreatedBy(result.getPropertyAsString(0).toString());}捕获(异常 e){deleteuserDetails.setCreatedBy("null");}

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)
解决方案

Use this,

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

这篇关于将空对象转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 08:55