1

sorry i just learning to use AsyncTask, but I am having a problem when running my code..

I have three class The first class for RestClient

public class LoginPembayaran {

private static final String urlServer = "https://webservicetagihan.herokuapp.com";

RestTemplate restTemplate = new RestTemplate();

public void login(String username, String password) throws GagalLoginException{
    Map<String, String> logindata = new HashMap<>();
    logindata.put("username",username);
    logindata.put("password",password);

    Map<String,Object> hasil = restTemplate.postForObject(urlServer + "/api/login",logindata,Map.class,new Object[]{});

    if (hasil == null ){
        throw new GagalLoginException("Respon valid");
    }

    if (!(Boolean)hasil.get("sukses")){
        throw new GagalLoginException("user name / password salah");
    }


}

class two for AsyncTask..

public class loginAktifity extends Fragment {
private LoginPembayaran loginPembayaran = new LoginPembayaran();

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    final View fragment = inflater.inflate(R.layout.fr_login,container,false);

    Button button = (Button)fragment.findViewById(R.id.BtnLogin);
    button.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    String username = ((EditText)fragment.findViewById(R.id.Etusernama)).getText().toString();
                    String password = ((EditText)fragment.findViewById(R.id.EtPassword)).getText().toString();

                    new AsyncTask<String,Void,Boolean>(){

                        String ErrorMassage;
                        @Override
                        protected Boolean doInBackground(String... params) {

                            try {
                                loginPembayaran.login(params[0], params[1]);
                                return true;
                            }catch (GagalLoginException err){
                                ErrorMassage = err.getMessage();
                                return false;
                            }
                        };


                        @Override
                        protected void onPostExecute(Boolean sukses) {
                            if(sukses){
                                Intent intent = new Intent(getContext(),SesudahLoginActivity.class);
                                startActivity(intent);
                            }
                            else {
                                Toast.makeText(getContext(),ErrorMassage,Toast.LENGTH_LONG).show();
                            }
                        }
                    }.execute(username,password);


                }
            }
    );
    return fragment;
}

}

Class tree for Exception

public class GagalLoginException extends Exception {
public GagalLoginException(String message) {
    super(message);
}

}

my logcat..

    03-18 09:57:35.780 30964-31227/com.khoiron.apptagihan E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
  Process: com.khoiron.apptagihan, PID: 30964
  java.lang.RuntimeException: An error occured while executing doInBackground()
      at android.os.AsyncTask$3.done(AsyncTask.java:300)
      at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
      at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
      at java.util.concurrent.FutureTask.run(FutureTask.java:242)
      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
      at java.lang.Thread.run(Thread.java:841)
   Caused by: java.lang.NullPointerException
      at com.khoiron.apptagihan.RestClient.LoginPembayaran.login(LoginPembayaran.java:31)
      at com.khoiron.apptagihan.Fragment.loginAktifity$1$1.doInBackground(loginAktifity.java:48)
      at com.khoiron.apptagihan.Fragment.loginAktifity$1$1.doInBackground(loginAktifity.java:41)
      at android.os.AsyncTask$2.call(AsyncTask.java:288)
      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
      at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
      12q
      at java.lang.Thread.run(Thread.java:841)

Please help me n Please guide me....

Said
  • 51
  • 1
  • 5
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Atef Hares Mar 18 '17 at 15:23
  • Also see http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this for more suggestions about how to solve this. – Code-Apprentice Mar 18 '17 at 23:38

2 Answers2

0

If it's line 31

if (!(boolean)hasil.get("sukses")){

So you made a typo with label, get() returns null and casting it to boolean causes NPE.

piotrpo
  • 12,398
  • 7
  • 42
  • 58
0

From the exception log, you can see that the exception is originated from LoginPembayaran.login, line 31.

Caused by: java.lang.NullPointerException
      at com.khoiron.apptagihan.RestClient.LoginPembayaran.login(LoginPembayaran.java:31)

As already mentioned in the other answer, it's possible that the map hasil do not contain the key item sukses, hence the exception.

To debug this problem, you can use Android Studio's debugger (see official guide) and set a breakpoint in line 31, to inspect if the map hasil indeed contains the key sukses.

Andree
  • 3,033
  • 6
  • 36
  • 56