Vivek Shankar
Vivek Shankar

Reputation: 701

Convert null object to String

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)

Upvotes: 19

Views: 92212

Answers (11)

Prateek
Prateek

Reputation: 12242

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

Upvotes: 9

Prashant K
Prashant K

Reputation: 909

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

Upvotes: 34

krotsch
krotsch

Reputation: 1

to Piggybag ride on what @prateek said.

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

Do it without the.length so you can actually just print out the string

Upvotes: 0

Deshani Vimukthika
Deshani Vimukthika

Reputation: 174

The error comes when you are trying to convert a null value to a String ( mostly it's BigDecimal)

So what you have to do is catch the exception when program tries to convert null to String and set the null value manually for the String variable.

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

Upvotes: 0

HannahCarney
HannahCarney

Reputation: 3631

"Hi this will be " + null;

Prints out "Hi this will be null" in String

Upvotes: 3

zapl
zapl

Reputation: 63945

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

Upvotes: 8

Sahil Mahajan Mj
Sahil Mahajan Mj

Reputation: 11141

Use this,

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

Upvotes: 6

Abubakkar
Abubakkar

Reputation: 15644

You can simply check if its null then set a string for it.

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

Upvotes: 3

Igorry
Igorry

Reputation: 177

Try this:

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

Upvotes: 6

Kanaiya Katarmal
Kanaiya Katarmal

Reputation: 6108

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

Upvotes: 3

Avadhani Y
Avadhani Y

Reputation: 7626

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

Upvotes: 2

Related Questions