0

In my application i have been using retrofit for my webservice calls. It is not working fine. This is the error that caused when my application started

04-16 07:41:26.162 5860-5860/com.pance.taugaksih E/AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException at com.pance.taugaksih.activity.SplashscreenActivity$1.success(SplashscreenActivity.java:54) at com.pance.taugaksih.activity.SplashscreenActivity$1.success(SplashscreenActivity.java:42) at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45) at android.os.Handler.handleCallback(Handler.java:725) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method)

This is my code that callled retrofit with response NullpointerExeption

 RESTClient.get().getPost(new Callback<ResponsePost>() {
        @Override
        public void failure(RetrofitError error) {
            // TODO Auto-generated method stub
            progress.dismiss();
            Toast.makeText(getApplicationContext(), "AKSES KE SERVER GAGAL" + error.getMessage(), Toast.LENGTH_LONG).show();
            finish();
        }

        @Override
        public void success(ResponsePost post, Response response) {
            // TODO Auto-generated method stub
            if (post.getPost().size() > 0) {
                PostController chb = new PostController(myContext);
                chb.open();
                chb.deleteData();
                for (int y = 0; y < post.getPost().size(); y++) {
                    PostModel tmpHasil = post.getPost().get(y);
                    chb.insertPost(tmpHasil);
                }
                chb.close();

                Intent sendIntent = new Intent(myContext,PostActivity.class);
                startActivity(sendIntent);
                finish();
            } else {
                Toast.makeText(getApplicationContext(), "DATA SEDANG TIDAK TERSEDIA", Toast.LENGTH_LONG).show();
            }
            progress.dismiss();
        }
    });

And this is code my RESTClient class

public class RESTClient {
private static APIPost REST_CLIENT;
private static String URL_TAUGAKSIH = "http://10.108.109.30/taugaksih_slim";

static {
     setupRestClient();
}

private RESTClient() {}

public static APIPost get() {
    if(REST_CLIENT==null){
        setupRestClient();
    }
      return REST_CLIENT;
}

private static void setupRestClient() {
    RestAdapter builder = new RestAdapter.Builder().setEndpoint(URL_TAUGAKSIH).build();
    REST_CLIENT = builder.create(APIPost.class);
}

}

Cœur
  • 37,241
  • 25
  • 195
  • 267
Pance S N
  • 1
  • 3
  • What's on your lines `45` & `54` of `SplashscreenActivity.java`? This is not a retrofit problem! – Dut A. Apr 16 '16 at 08:16
  • This is code in lines 45 & 54 of SplashscreenActivity.java this is the line 45 : RESTClient.get().getPost(new Callback() { and this is line 54 if (post.getPost().size() > 0) { – Pance S N Apr 16 '16 at 08:18
  • check for post.getPost() is null or not. like if(post.getPost()!=null && post.getPost().size() > 0) – Ramesh Kumar Apr 16 '16 at 09:20

2 Answers2

0

I think you are running the code inside the loop, update the full code.

Panneer Selvam R
  • 443
  • 3
  • 11
  • what do you mean inside the loop? The code that caused error: if (post.getPost().size() > 0). return NullpointerExeption – Pance S N Apr 16 '16 at 08:44
0

Try it!!!

change

 if (post.getPost().size() > 0) {

to

PostController chb = new PostController(myContext);
            chb.open();
            chb.deleteData();
if(post != null && !post.getPost().isEmpty()){
 for(ResponsePost data: post.getPost()){
 PostModel tmpHasil = data.getName(); // or getYourNameModel()
                chb.insertPost(tmpHasil);
 // enter code here
}
}
chb.close();

Intent sendIntent = new Intent(myContext,PostActivity.class);
startActivity(sendIntent);
finish();
Nguyễn Trung Hiếu
  • 2,004
  • 1
  • 10
  • 22