将null对象转换为String

时间:2012-12-20 13:27:02

标签: java android nullpointerexception

我编写了一个android程序,用于从Web服务向tablerow加载值。但是一个值为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值,因此我需要将其转换为字符串"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)

11 个答案:

答案 0 :(得分:22)

使用,而不是捕获异常或放置条件 的将String.valueOf(result.getPropertyAsString(0));

它将调用参数的 toString()方法,并将其转换为String,如果result.getPropertyAsString(0)为null,则将其更改为&#34;空&#34;

答案 1 :(得分:8)

当属性内部为空时,

result.getPropertyAsString(0)单独将导致NPE。您的stacktrace指向SoapObject.getPropertyAsString(SoapObject.java:165),应该是

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

source - propertyInfo或{}时会崩溃 propertyInfo.getValue()null

为了防止这种情况发生,您需要不是通过getPropertyAsString而是通过getProperty获取该属性,并将其手动转换为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)));

答案 2 :(得分:7)

使用此,

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

答案 3 :(得分:5)

试试这个:

deleteuserDetails.setCreatedBy(result.getPropertyAsString(0) == null ? "null": result.getPropertyAsString(0));

答案 4 :(得分:4)

虽然这不是一个好习惯,但你可以将空值与“”连接起来,使其成为一个字符串。

例如:

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

答案 5 :(得分:3)

if(result.getPropertyAsString(0)==null)
{
deleteuserDetails.setCreatedBy("");
}
else
{
deleteuserDetails.setCreatedBy(result.getPropertyAsString(0).toString());
}

答案 6 :(得分:3)

您只需检查其是否为null,然后为其设置字符串。

if(result.getPropertyAsString(0) == null){
 deleteuserDetails.setCreatedBy("NULL");
}else{
 deleteuserDetails.setCreatedBy(result.getPropertyAsString(0));
}

答案 7 :(得分:2)

而不是那样,放置条件if(String!=null)

答案 8 :(得分:2)

"Hi this will be " + null;

在字符串

中打印出“嗨这将为空”

答案 9 :(得分:0)

当您尝试将null值转换为String(主要是BigDecimal)时,就会出现错误

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

try{
    deleteuserDetails.setUserId(result.getPropertyAsString(4));
}catch (Exception e){
    deleteuserDetails.setUserId(null);
}

答案 10 :(得分:0)

骑着小猪@prateek说的话。

public class guy
{
    public address address { get; set; }
    public int age { get; set; }

    public guy()
    {
        address = new address();
    }
}

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