16

我编写了一个 android 程序来将值从 web 服务加载到表行。但是 value 为 null 所以我需要将其转换为字符串。有人可以告诉我这样做的方法吗?

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);
    }

现在deleteuserDetails.setCreatedBy(result.getPropertyAsString(0));从 webservice 获取 null 值,所以我需要将其转换为 string "null"

日志猫:

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)
4

11 回答 11

30

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".

于 2015-01-08T06:10:12.430 回答
8

虽然这不是一个好习惯,但您可以将 null 值与 "" 连接以使其成为字符串。

例如:

String str=null;
System.out.println((str+"").length()); /// prints 4
于 2012-12-20T13:33:56.487 回答
8

result.getPropertyAsString(0)当属性在内部为空时,单独会导致 NPE。您的堆栈跟踪指向的SoapObject.getPropertyAsString(SoapObject.java:165)应该是

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

sourcepropertyInfo - 它会在或 propertyInfo.getValue()is 时崩溃null

为防止这种情况发生,您需要获取属性,而不是通过getPropertyAsStringgetProperty然后手动将其转换为String.

您可以将其封装到一些实用方法中

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
}

然后做

deleteuserDetails.setUserId(getPropertyAsString(result, getPropertyAsString(4)));
deleteuserDetails.setUserName(getPropertyAsString(result, getPropertyAsString(2)));
deleteuserDetails.setUserRole(getPropertyAsString(result, getPropertyAsString(3)));
于 2012-12-20T14:05:17.183 回答
6

尝试这个:

deleteuserDetails.setCreatedBy(result.getPropertyAsString(0) == null ? "null": result.getPropertyAsString(0));
于 2012-12-20T13:29:42.600 回答
5

用这个,

try {
    deleteuserDetails.setCreatedBy(result.getPropertyAsString(0).toString());
}
catch(Exception e) {
    deleteuserDetails.setCreatedBy("null");
}
于 2012-12-20T13:34:04.953 回答
3
if(result.getPropertyAsString(0)==null)
{
deleteuserDetails.setCreatedBy("");
}
else
{
deleteuserDetails.setCreatedBy(result.getPropertyAsString(0).toString());
}
于 2012-12-20T13:29:31.697 回答
3

您可以简单地检查它是否为空,然后为它设置一个字符串。

if(result.getPropertyAsString(0) == null){
 deleteuserDetails.setCreatedBy("NULL");
}else{
 deleteuserDetails.setCreatedBy(result.getPropertyAsString(0));
}
于 2012-12-20T13:29:52.677 回答
3

"Hi this will be " + null;

在 String 中打印出“Hi this will be null”

于 2017-07-16T13:04:50.063 回答
2

取而代之的是,放置条件if(String!=null)

于 2012-12-20T13:29:23.050 回答
0

当您尝试将空值转换为字符串时出现错误(主要是 BigDecimal)

因此,您要做的就是在程序尝试将 null 转换为 String 并为 String 变量手动设置 null 值时捕获异常。

try{
    deleteuserDetails.setUserId(result.getPropertyAsString(4));
}catch (Exception e){
    deleteuserDetails.setUserId(null);
}
于 2019-10-09T08:03:00.447 回答
0

去 Piggybag 骑@prateek 所说的话。

 String str=null;
    System.out.println((str+""));

不使用 .length 进行操作,这样您实际上就可以打印出字符串

于 2019-10-16T12:20:08.313 回答