-1

Hi first of all i searched some similar questions like mine but unfortunately i couldn't find the similarity of my code to them so please here me out

I have a Main Activity Class

public class MainActivity extends AppCompatActivity {

public ProgressDialog loading;
public String[] itemer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    RetrofitHandler handler = new RetrofitHandler();
    handler.getContacts(MainActivity.this);
}

public void getter(String[] response, int size) {
    String[] itemer;
    itemer = new String[size];
    if (response != null) {
        itemer = response;
        Toast.makeText(MainActivity.this, itemer[0], Toast.LENGTH_SHORT).show();
    }
}

And a Handler for my result

public class RetrofitHandler {
public String[] item;
public static final String ROOT_URL = "http://api.androidhive.info";
public List<Contacts> contacts;

// final MainActivity main = new MainActivity();
public void getContacts(final Context context) {
    final ProgressDialog loading = ProgressDialog.show(context, "Fetching Data", "Please wait...", false, false);

    RestAdapter adapter = new RestAdapter.Builder().setEndpoint(ROOT_URL).build();
    ContactsAPI api = adapter.create(ContactsAPI.class);

    api.getContacts(new Callback<Contacts>() {
        @Override
        public void success(Contacts contacts, Response response) {
            loading.dismiss();
            MainActivity update = new MainActivity();
            List<Contact> contactList = contacts.getContacts();

            item = new String[contactList.size()];
            int size = contactList.size();
            for (int i = 0; i < contactList.size(); i++) {
                item[i] = contactList.get(i).getName();
            }
            update.getter(item, size);
        }

        @Override
        public void failure(RetrofitError error) {
            Toast.makeText(context, "Error Occured", Toast.LENGTH_LONG).show();
        }
    });


}

But I get an error on my response in the main activity here is my log

              java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
                                                                         at android.content.ContextWrapper.getResources(ContextWrapper.java:87)
                                                                         at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:81)
                                                                         at android.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:542)
                                                                         at android.widget.Toast.<init>(Toast.java:102)
                                                                         at android.widget.Toast.makeText(Toast.java:259)
                                                                         at com.exist.kelvs.retrofit2.MainActivity.getter(MainActivity.java:55)
                                                                         at com.exist.kelvs.retrofit2.RetrofitHandler$1.success(RetrofitHandler.java:41)
                                                                         at com.exist.kelvs.retrofit2.RetrofitHandler$1.success(RetrofitHandler.java:28)
                                                                         at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45)
                                                                         at android.os.Handler.handleCallback(Handler.java:739)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                         at android.os.Looper.loop(Looper.java:148)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I maked sure that the handler item where not null tested it with toast but when i pass it to getter it gives me the error where did i do wrong? :(

user3262438
  • 73
  • 1
  • 8
  • what is line no.55 in your MainActivity?? – SripadRaj Jul 27 '16 at 09:29
  • 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) – Sufian Aug 03 '16 at 15:58

2 Answers2

2
MainActivity update = new MainActivity();

Never instantiate activities with new. They are not initialized to be useful.

Instead, you can pass your activity as a reference where needed. Change

public void getContacts(final Context context)

to e.g.

public void getContacts(final MainActivity mainActivity)

and use mainActivity where you need an activity Context (such as with Dialogs) and when you need to invoke a method on MainActivity.

Note that generally passing activity references to async operations can be prone to significant memory leaks, and you need to take activity lifecycle into account - the activity might not be active when the async operation finishes.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • Thank you very much that works very well. but one last thing what if I need to call my handler with another class what approach should i use? – user3262438 Jul 28 '16 at 01:27
  • @laalto http://stackoverflow.com/questions/39219078/error-with-progress-dialog can you please help me out.. I am stuck on this problem only on Marshmallow – Asmi Aug 31 '16 at 12:13
0

try to delete the toast in your main activity or replace Mainactivity.this to getApplicationContext() .

from : Toast.makeText(MainActivity.this, itemer[0], Toast.LENGTH_SHORT).show();

to :

    Toast.makeText(getApplicationContext(), itemer[0], Toast.LENGTH_SHORT).show();
MedAmine.Rihane
  • 1,193
  • 10
  • 17
  • Tryed your solution when i delete the toast it runs good but when i tried your getApplicationContext() the error is still the same :( – user3262438 Jul 27 '16 at 09:44
  • i think that your array itemer is empty . you are declaring itemer 2 times in the class and in the getter function check it maybe that cause the emty value of your array . – MedAmine.Rihane Jul 27 '16 at 10:03