0

My problem is that I have a ListActivity class and when I go to execute logout function I have a nullpointerexception because getApplicationContext is null.

The problem is here:

        userFunction.logoutUser(getApplicationContext());

class Cloudy:

  public class Cloudy extends ListActivity {

        private static final int LOGOUT = 4;

        private LinkedList<String> mListItems;
        private PullToRefreshListView mPullRefreshListView;
        private ArrayAdapter<String> mAdapter;

        private UserFunctions userFunction;

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

            mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);

            // Set a listener to be invoked when the list should be refreshed.
            mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
                @Override
                public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                    String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME
                            | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);

                    // Update the LastUpdatedLabel
                    refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);

                    // Do work to refresh the list here.
                    new GetDataTask().execute();
                }
            });

            // Add an end-of-list listener
            mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

                @Override
                public void onLastItemVisible() {
                    Toast.makeText(Cloudy.this, "End of List!", Toast.LENGTH_SHORT).show();
                }
            });

            ListView actualListView = mPullRefreshListView.getRefreshableView();

            // Need to use the Actual ListView when registering for Context Menu
            registerForContextMenu(actualListView);

            mListItems = new LinkedList<String>();
            mListItems.addAll(Arrays.asList(mStrings));

            mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);

            /**
             * Add Sound Event Listener
             */
            SoundPullEventListener<ListView> soundListener = new SoundPullEventListener<ListView>(this);
            soundListener.addSoundEvent(State.PULL_TO_REFRESH, R.raw.pull_event);
            soundListener.addSoundEvent(State.RESET, R.raw.reset_sound);
            soundListener.addSoundEvent(State.REFRESHING, R.raw.refreshing_sound);
            mPullRefreshListView.setOnPullEventListener(soundListener);

            // You can also just use setListAdapter(mAdapter) or
            // mPullRefreshListView.setAdapter(mAdapter)
            actualListView.setAdapter(mAdapter);
        }

        private class GetDataTask extends AsyncTask<Void, Void, String[]> {

            @Override
            protected String[] doInBackground(Void... params) {
                // Simulates a background job.
                try {
                    Thread.sleep(4000);
                } catch (InterruptedException e) {
                }
                return mStrings;
            }

            @Override
            protected void onPostExecute(String[] result) {
                mListItems.addFirst("Added after refresh...");
                mAdapter.notifyDataSetChanged();

                // Call onRefreshComplete when the list has been refreshed.
                mPullRefreshListView.onRefreshComplete();

                super.onPostExecute(result);
            }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            menu.add(Menu.NONE, LOGOUT, Menu.NONE, "Logout");
            return super.onCreateOptionsMenu(menu);
        }

        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;

            menu.setHeaderTitle("Item: " + getListView().getItemAtPosition(info.position));
            menu.add("Item 1");
            menu.add("Item 2");
            menu.add("Item 3");
            menu.add("Item 4");

            super.onCreateContextMenu(menu, v, menuInfo);
        }   

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {

            switch (item.getItemId()) {
            case LOGOUT:
                userFunction.logoutUser(getApplicationContext());
                Intent intent = new Intent(Cloudy.this, Login.class);

                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);

                finish();
                break;

            }

            return super.onOptionsItemSelected(item);
        }

        private String[] mStrings = { "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis",
                "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre", "Allgauer Emmentaler", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance",
                "Ackawi", "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre", "Allgauer Emmentaler" };
    }

log:

05-13 21:17:41.717: E/AndroidRuntime(3716): FATAL EXCEPTION: main
05-13 21:17:41.717: E/AndroidRuntime(3716): Process: com.ramon.cloudy, PID: 3716
05-13 21:17:41.717: E/AndroidRuntime(3716): java.lang.NullPointerException
05-13 21:17:41.717: E/AndroidRuntime(3716):     at com.ramon.cloudy.Cloudy.onOptionsItemSelected(Cloudy.java:142)
05-13 21:17:41.717: E/AndroidRuntime(3716):     at android.app.Activity.onMenuItemSelected(Activity.java:2687)
05-13 21:17:41.717: E/AndroidRuntime(3716):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1103)
05-13 21:17:41.717: E/AndroidRuntime(3716):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
05-13 21:17:41.717: E/AndroidRuntime(3716):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
05-13 21:17:41.717: E/AndroidRuntime(3716):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
05-13 21:17:41.717: E/AndroidRuntime(3716):     at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:177)
05-13 21:17:41.717: E/AndroidRuntime(3716):     at android.widget.AdapterView.performItemClick(AdapterView.java:308)
05-13 21:17:41.717: E/AndroidRuntime(3716):     at android.widget.AbsListView.performItemClick(AbsListView.java:1478)
05-13 21:17:41.717: E/AndroidRuntime(3716):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:3480)
05-13 21:17:41.717: E/AndroidRuntime(3716):     at android.widget.AbsListView$3.run(AbsListView.java:4823)
05-13 21:17:41.717: E/AndroidRuntime(3716):     at android.os.Handler.handleCallback(Handler.java:733)
05-13 21:17:41.717: E/AndroidRuntime(3716):     at android.os.Handler.dispatchMessage(Handler.java:95)
05-13 21:17:41.717: E/AndroidRuntime(3716):     at android.os.Looper.loop(Looper.java:157)
05-13 21:17:41.717: E/AndroidRuntime(3716):     at android.app.ActivityThread.main(ActivityThread.java:5356)
05-13 21:17:41.717: E/AndroidRuntime(3716):     at java.lang.reflect.Method.invokeNative(Native Method)
05-13 21:17:41.717: E/AndroidRuntime(3716):     at java.lang.reflect.Method.invoke(Method.java:515)
05-13 21:17:41.717: E/AndroidRuntime(3716):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
05-13 21:17:41.717: E/AndroidRuntime(3716):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
05-13 21:17:41.717: E/AndroidRuntime(3716):     at dalvik.system.NativeStart.main(Native Method)
  • Why are you using `getApplicationContext()` in the first place? That is rarely the right answer. http://www.doubleencore.com/2013/06/context/ – CommonsWare May 13 '14 at 19:20
  • where do you create `userFunction` because I dont see it created anywhere meaning its null – tyczj May 13 '14 at 19:21

1 Answers1

0

The NPE means that userFunctionis null, not the result of getApplicationContext().

Ridcully
  • 22,583
  • 7
  • 66
  • 81
  • Thanks! That's it! Where you see that the NPE is of userFunction? – user3633957 May 13 '14 at 19:29
  • A NPE is thrown if you want to access a property or a method of a variable that is null, i.e. does not point to an actual object. As userFunction is the only variable in that line where you try to access a method, so it has to be it. – Ridcully May 14 '14 at 07:50