if condition in try process

102 Views Asked by At

i have some problem when trying to implement condition in "try" process, the result always of "globalnidn" always isi , therefore i've checked in logcat and the result is fine. :(

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

    }
3

There are 3 best solutions below

0
On BEST ANSWER

To test for null you should not use globalnidn.equals(null)

but rather

globalnidn==null
0
On

If "globalnidn" equals NULL then "globalnidn" is not a object, you can't use ".equals()" in this case, so try to use if(globalnidn == null)

0
On

Put try catch block for get string value from JsonObject

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

Check condition for null

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

Done