19

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)
Gavriel Cohen
  • 4,355
  • 34
  • 39
Vivek Shankar
  • 691
  • 3
  • 14
  • 48
  • 1
    `deleteuserDetails.setCreatedBy(String.valueOf(result.getPropertyAsString(0));` will transform null into "null". – assylias Dec 20 '12 at 13:33
  • 2
    This will still cause a NPE. Writing String.valueOf((Object)result.getPropertyAsString(0)) would probably work. – NickL Dec 20 '12 at 13:47

11 Answers11

34

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

Pang
  • 9,564
  • 146
  • 81
  • 122
Prashant K
  • 869
  • 9
  • 9
9

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
Pang
  • 9,564
  • 146
  • 81
  • 122
Prateek
  • 12,014
  • 12
  • 60
  • 81
  • It may not be good practice, and I don't know why it might not be, but it certainly is effective, simple, reliable, and doesn't seem to have any negative side effects. Although I might advocate that the string value of null ought to be "". – Keith Tyler Mar 25 '21 at 00:23
8

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)));
zapl
  • 63,179
  • 10
  • 123
  • 154
6

Try this:

deleteuserDetails.setCreatedBy(result.getPropertyAsString(0) == null ? "null": result.getPropertyAsString(0));
Igorry
  • 177
  • 3
  • Then you are still calling .toString() on getPropertyAsString. Which you can't do, because if getPropertyAsString returns null, it is like calling null.toString(). – NickL Dec 20 '12 at 13:45
6

Use this,

try {
    deleteuserDetails.setCreatedBy(result.getPropertyAsString(0).toString());
}
catch(Exception e) {
    deleteuserDetails.setCreatedBy("null");
}
Sahil Mahajan Mj
  • 11,033
  • 8
  • 53
  • 100
3
if(result.getPropertyAsString(0)==null)
{
deleteuserDetails.setCreatedBy("");
}
else
{
deleteuserDetails.setCreatedBy(result.getPropertyAsString(0).toString());
}
Kanaiya Katarmal
  • 5,974
  • 4
  • 30
  • 56
3

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));
}
Abubakkar
  • 15,488
  • 8
  • 55
  • 83
3

"Hi this will be " + null;

Prints out "Hi this will be null" in String

HannahCarney
  • 3,441
  • 2
  • 26
  • 32
2

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

Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
  • could u please tell me elabrately – Vivek Shankar Dec 20 '12 at 13:47
  • Place the condition as: if(result.getPropertyAsString(0).toString()!=null){deleteuserDetails.setCreatedBy(result.getPropertyAsString(0).toString());} – Avadhani Y Dec 20 '12 at 13:49
  • 2
    His problem is, that getPropertyAsString == null, so calling .toString() on that will cause a NPE. – NickL Dec 20 '12 at 13:54
  • ok @NickL then change the code as: if(result.getPropertyAsString(0)!=null){deleteuserDetails.setCreatedB‌​y(result.getPropertyAsString(0).toString());} – Avadhani Y Dec 20 '12 at 13:56
  • 2
    @Avadhani then the NULL value still isn't converted to a "null" String. See other answers. – NickL Dec 20 '12 at 13:59
  • @NickL What i am trying to tell is check whether the value result.getPropertyAsString(0) is null or not... thats it... if null return; else convert it to string... is it not the correct way to do....? – Avadhani Y Dec 20 '12 at 14:01
  • I know, but he wants to 'convert' the NULL value to a "NULL" String, a String which contains the text "NULL". In his current code he is trying to do this with .toString(), which ofcourse will throw a NPE when called on a NULL value. – NickL Dec 20 '12 at 14:03
  • what can i do NickL and Avadhani – Vivek Shankar Dec 21 '12 at 04:19
  • @user1858826 Please try to use the condition which i placed above and let me know if there are any issues.... – Avadhani Y Dec 21 '12 at 05:39
  • Could do what Zapl said, it is the only answer that is different then the other 8 answers. – NickL Dec 21 '12 at 06:13
  • try { deleteuserDetails.setCreatedBy(result.getPropertyAsString(0).toString()); } catch(Exception e) { deleteuserDetails.setCreatedBy("null"); } – Vivek Shankar Dec 21 '12 at 09:31
  • ya tried the above one and if exception rises i stored it as null – Vivek Shankar Dec 21 '12 at 09:40
  • http://stackoverflow.com/questions/13987579/illegalstateexception-in-android-tablelayout – Vivek Shankar Dec 21 '12 at 10:22
  • No, he caught the exception and then inserted "null". Like Gamb suggested. But @user1858826 is a bad user and probably isn't going to accept an answer anyways. – NickL Dec 21 '12 at 10:26
  • who said NickL nothing like that – Vivek Shankar Dec 21 '12 at 10:35
0

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

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

krotsch
  • 1
  • 2