-11

When I attempt to call the method loadUserList() from another class, I get the following error:

Attempt to invoke virtual method android.content.res.Resources 
android.content.Context.getResources() on a null object reference

This is my loadUserList method

public void loadUserList()
{
    final ProgressDialog dia = ProgressDialog.show(this, null, getString(R.string.alert_loading));

    ParseQuery<ParseObject> query = ParseQuery.getQuery("Chat_User");
    query.whereEqualTo("receiver", ParseUser.getCurrentUser());
    query.include("sender");

    query.findInBackground(new FindCallback<ParseObject>()
    {

        @Override
        public void done(List<ParseObject> li, ParseException e)
        {
            dia.dismiss();

            if (li != null)
            {
                if (li.size() == 0)
                    Toast.makeText(com.yarnyard.UserList.this, R.string.msg_no_user_found,
                            Toast.LENGTH_SHORT).show();

                uList = new ArrayList<ParseObject>(li);
                ListView list = (ListView) findViewById(R.id.list);
                list.setAdapter(new UserAdapter());
                list.setOnItemClickListener(new OnItemClickListener()
                {
                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3)
                    {
                        loadUserList();
                        /*startActivity(new Intent(com.yarnyard.UserList.this, Chat.class)
                                .putExtra(Const.EXTRA_DATA, uList.get(pos).getUsername()));*/
                    }
                });
            }
            else
            {
                Utils.showDialog(
                        com.yarnyard.UserList.this,
                        getString(R.string.err_users) + " "
                                + e.getMessage());
                e.printStackTrace();
            }
        }
    });
}

This is how I am calling it

public void done(ParseException e)
{
    UserList UserList = new UserList();
    UserList.loadUserList();
}

My error log

07-30 14:14:56.219  13383-13383/com.anonimv2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.anonimv2, PID: 13383
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:85)
        at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74)
        at android.content.Context.getString(Context.java:376)
        at com.yarnyard.UserList.loadUserList(UserList.java:96)
        at com.yarnyard.custom.CustomActivity.onOptionsItemSelected(CustomActivity.java:116)
        at android.app.Activity.onMenuItemSelected(Activity.java:2882)
        at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:353)
        at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1131)
        at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:761)
        at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
        at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:904)
        at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:894)
        at android.widget.ActionMenuView.invokeItem(ActionMenuView.java:587)
        at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:141)
        at android.view.View.performClick(View.java:4832)
        at android.view.View$PerformClick.run(View.java:19839)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:211)
        at android.app.ActivityThread.main(ActivityThread.java:5321)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)

Any advice would be much appreciated.

1
  • Your code is not match with your posted logcat. So make it proper first.
    – M D
    Jul 30, 2015 at 6:36

2 Answers 2

2

Normal, non-static methods are called on objects, not on classes. This is a fine point that you really need to learn before using object oriented languages. Only then you will see, in practice, how you can connect objects by passing references.

The UserList objects that you construct in the done method is not the one you want to call, it's just an empty, uninitialized shell.

2
  • 3
    It's confusing, but it looks like it is actually being called on an object instance, but the instance reference is defined with the same name as the class. The OPs code is just wrong on so many levels.... Jul 30, 2015 at 6:47
  • @DanielNugent Yes, I noticed it is called on the instance - I tried to say that it's an instance that is created just before the call, not the instance OP wants to call (which is, judging from the stacktrace, a current activity).
    – fdreger
    Jul 30, 2015 at 9:09
1

As I understand, you extend UserList from Context, right?
Then keep in mind that in Android never initial a Context object by yourself, it's framework's job..
In your case, because you create a context instance without attach to any baseContext, so when access to common resource, you got a NPE.
I guess that you are trying to move to a new Fragment or Actvity? Then read these link start activity switch fragment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.