if 条件在 try 过程中

if condition in try process

我在 "try" 过程中尝试实现条件时遇到一些问题,"globalnidn" 的结果总是 isi ,因此我检查了 logcat 结果很好. :(

 protected void onPostExecute(String data) {
        try {   JSONObject jObject = new JSONObject(data);
            globalnidn  = null;
            globalnidn  = jObject.getString("nidn");
            String password = jObject.getString("password");
            Toast.makeText(getBaseContext(),"nidn = "+globalnidn,Toast.LENGTH_SHORT).show();
            if (globalnidn.equals(null)){
                Toast.makeText(getBaseContext(),"kosong",Toast.LENGTH_SHORT).show();

            } else {
                Toast.makeText(getBaseContext(),"isi",Toast.LENGTH_SHORT).show();
            }

        }catch (Exception e) {
            e.printStackTrace();
        }

    }

要测试 null 你不应该使用 globalnidn.equals(null)

而是

globalnidn==null

try catch 块用于 get string value from JsonObject

String globalnidn  = "";
try {
   JSONObject jObject = new JSONObject(data);
} catch (Exception e) {
    globalnidn = "";
}

检查 null

的条件
if(globalnidn.equals("")){
   // If your string is null
}else{
   // If your string is not null            
}

完成

如果"globalnidn"等于NULL那么"globalnidn"不是一个对象,在这种情况下你不能使用“.equals()”,所以尝试使用if(globalnidn == null)