Java 将空对象转换为字符串
我已经编写了一个android程序,将值从web服务加载到表行。但值为空,所以我需要将其转换为字符串。有人能告诉我做这件事的方法吗Java 将空对象转换为字符串,java,android,string,nullpointerexception,type-conversion,Java,Android,String,Nullpointerexception,Type Conversion,我已经编写了一个android程序,将值从web服务加载到表行。但值为空,所以我需要将其转换为字符串。有人能告诉我做这件事的方法吗 try{ SoapObject request = service.getRequest(); SoapSerializationEnvelope envelope = service.getEnvelope(request); SoapObject response = service.getRes
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);
}
如果(String!=null),则放置条件
试试这个:
deleteuserDetails.setCreatedBy(result.getPropertyAsString(0) == null ? "null": result.getPropertyAsString(0));
您可以简单地检查它是否为null,然后为它设置一个字符串
if(result.getPropertyAsString(0) == null){
deleteuserDetails.setCreatedBy("NULL");
}else{
deleteuserDetails.setCreatedBy(result.getPropertyAsString(0));
}
虽然这不是一个好的做法,但您可以将空值与“”连接起来,使其成为字符串
例如:
String str=null;
System.out.println((str+"").length()); /// prints 4
用这个,
try {
deleteuserDetails.setCreatedBy(result.getPropertyAsString(0).toString());
}
catch(Exception e) {
deleteuserDetails.setCreatedBy("null");
}
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();
}
-当propertyInfo
或
propertyInfo.getValue()
为null
为了防止这种情况发生,您需要通过getpropertyastring
而不是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)));
使用
String.valueOf(result.getPropertyAsString(0))代码>
它将调用参数的toString()方法,并将其转换为字符串,如果result.getPropertyAsString(0)为null,则将其更改为“null”“Hi”将为“+null”代码>
在字符串中打印“Hi this will be null”当您试图将空值转换为字符串时会出现错误(主要是BigDecimal)
所以,当程序试图将null转换为String并手动为String变量设置null值时,您必须捕获异常
try{
deleteuserDetails.setUserId(result.getPropertyAsString(4));
}catch (Exception e){
deleteuserDetails.setUserId(null);
}
把@prateek说的话背在背上
String str=null;
System.out.println((str+""));
在不使用.length的情况下执行此操作,这样您就可以实际打印出字符串deleteuserDetails.setCreatedBy(string.valueOf(result.getPropertyAsString(0));
将null转换为“null”。这仍然会导致NPE。编写字符串时,valueOf((Object)result.getPropertyAsString(0))可能会起作用。然后您仍在调用.toString()你不能这样做,因为如果getPropertyAsString返回null,这就像调用null.toString()。你能告诉我elabrately将条件替换为:if(result.getPropertyAsString(0.toString()!=null){deleteuserDetails.setCreatedBy(result.getPropertyAsString(0.toString());}他的问题是,getPropertyAsString==null,因此对其调用.toString()将导致NPE.ok@NickL,然后将代码更改为:if(result.getPropertyAsString(0)!=null){deleteuserDetails.setCreatedBy(result.getPropertyAsString(0.toString());}@Avadhani然后空值仍然没有转换为“NULL”字符串。请参阅其他答案。这可能不是一种好的做法,我也不知道为什么不是,但它确实有效、简单、可靠,而且似乎没有任何负面影响。尽管我可能主张null的字符串值应该是“”。
try{
deleteuserDetails.setUserId(result.getPropertyAsString(4));
}catch (Exception e){
deleteuserDetails.setUserId(null);
}
String str=null;
System.out.println((str+""));