Java检查字符串是否为null或空
我得到了一些字符串,并在使用图表上的值之前执行了一些检查。这是我的密码:Java检查字符串是否为null或空,java,string,nullpointerexception,Java,String,Nullpointerexception,我得到了一些字符串,并在使用图表上的值之前执行了一些检查。这是我的密码: for (int j = 0; j < shareByTypeList.size(); j++) { if (j == 0) { facebookShare = shareByTypeList.get(j).getTotalShare(); } else if (j == 1) { googleplusSh
for (int j = 0; j < shareByTypeList.size(); j++) {
if (j == 0) {
facebookShare = shareByTypeList.get(j).getTotalShare();
} else if (j == 1) {
googleplusShare = shareByTypeList.get(j).getTotalShare();
} else {
twitterShare = shareByTypeList.get(j).getTotalShare();
}
}
if (facebookShare != null && !facebookShare.isEmpty()
&& twitterShare != null && !twitterShare.isEmpty()
&& googleplusShare != null && !googleplusShare.isEmpty()) {
txtDisplayFacebook.setText(facebookShare);
txtDisplayTwitter.setText(twitterShare);
txtDisplayGooglePlus.setText(googleplusShare);
totalShare = Integer.parseInt(facebookShare)
+ Integer.parseInt(googleplusShare)
+ Integer.parseInt(twitterShare);
txtDisplayShareAmt.setText(String.valueOf(totalShare));
}
我通过分离3个变量来执行检查,然后将值设置为textview来解决这个问题:
if (facebookShare != null && !facebookShare.isEmpty()) {
txtDisplayFacebook.setText(facebookShare);
} else if (twitterShare != null && !twitterShare.isEmpty()) {
txtDisplayTwitter.setText(twitterShare);
} else if (googleplusShare != null && !googleplusShare.isEmpty()) {
txtDisplayGooglePlus.setText(googleplusShare);
}
if (!txtDisplayFacebook.getText().equals("0")
&& !txtDisplayTwitter.getText().equals("0")
&& !txtDisplayGooglePlus.getText().equals("0")) {
totalShare = Integer.parseInt(facebookShare)
+ Integer.parseInt(googleplusShare)
+ Integer.parseInt(twitterShare);
txtDisplayShareAmt.setText(String.valueOf(totalShare));
}
粘贴堆栈跟踪?它必须是不同的一行。哪一行是第191行?你能告诉我们哪一行是空指针出现的吗?你能准确地指定哪一行是第191行吗?请…你给出了3个变量的默认值吗?
if (facebookShare != null && !facebookShare.isEmpty()) {
txtDisplayFacebook.setText(facebookShare);
} else if (twitterShare != null && !twitterShare.isEmpty()) {
txtDisplayTwitter.setText(twitterShare);
} else if (googleplusShare != null && !googleplusShare.isEmpty()) {
txtDisplayGooglePlus.setText(googleplusShare);
}
if (!txtDisplayFacebook.getText().equals("0")
&& !txtDisplayTwitter.getText().equals("0")
&& !txtDisplayGooglePlus.getText().equals("0")) {
totalShare = Integer.parseInt(facebookShare)
+ Integer.parseInt(googleplusShare)
+ Integer.parseInt(twitterShare);
txtDisplayShareAmt.setText(String.valueOf(totalShare));
}