-1

The Problem is: My ContextMenu Fails to Remove a Row from a ListView with a Custom Adapter and the app crashes with a NullPointerException, see Logcat output.

I have done Google searches and searched stackoverflow. None of the information I have found solves this problem.

My Question is: What is wrong with my code? Please provide the correct code to solve this problem.

Java Code: class TestActivity

    public class TestActivity extends ListActivity {

    private String itemNameArray[];
    private String dateArray[];

    private ListView listview;
    CustomListViewAdapter customListViewAdapter;

                    @Override
                    public void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);

                        setContentView(R.layout.activity_test);

                        registerForContextMenu(getListView());

                        root = Environment.getExternalStorageDirectory() + File.separator + "/ListTestFiles";
                        getDir(root);

        listview = getListView();

        name=itemNameArray;
        lastmod=dateArray;

        CustomListViewAdapter customListViewAdapter = new CustomListViewAdapter(this, name, lastmod);

        listview.setAdapter(customListViewAdapter);

  } // End of onCreate code.


    class CustomListViewAdapter extends ArrayAdapter<String> {
        Context context;
        String[] nameArray;
        String[] modifiedArray;

        CustomListViewAdapter(Context c, String[] name, String[] lastmod)
        {
            super(c, R.layout.layout_item_view, R.id.rowtext, name);
            this.context = c;
            this.nameArray = name;
            this.modifiedArray = lastmod;
        }

        public View getView(int position, View convertView, ViewGroup parent)
        {
            LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.layout_item_view, parent, false);

            TextView myName;
            TextView myLastMod;

            myName=(TextView) row.findViewById(R.id.rowtext);
            myName.setText(nameArray[position]);

            myLastMod=(TextView) row.findViewById(R.id.textView6);
            myLastMod.setText(modifiedArray[position]);

            return row;
        }
    }


     @Override
     public void onCreateContextMenu(ContextMenu menu, View v,
                                     ContextMenu.ContextMenuInfo menuInfo) {
         super.onCreateContextMenu(menu, v, menuInfo);
             MenuInflater inflater = getMenuInflater();
             inflater.inflate(R.menu.context_menu, menu);
     }

    @Override
    public boolean onContextItemSelected(MenuItem itemMenu) {

        final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)itemMenu.getMenuInfo();

        switch (itemMenu.getItemId()) {
            case R.id.context_menu_rename:
                Toast.makeText(this, "Rename", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.context_menu_delete:
                // NOTE TESTING the line of code below caused a runtime error: java.lang.NullPointerException
                customListViewAdapter.remove(customListViewAdapter.getItem(info.position));
                customListViewAdapter.notifyDataSetChanged();

                Toast.makeText(getApplicationContext(), "position = " + info.position, Toast.LENGTH_SHORT).show();
                Toast.makeText(this, "Delete", Toast.LENGTH_SHORT).show();

                return true;

                return super.onContextItemSelected(itemMenu);
        }
    }

}

Logcat output:

11-12 12:46:59.965  14265-14265/com.testing.listapp D/AndroidRuntime﹕ Shutting down VM
    --------- beginning of crash
11-12 12:46:59.981  14265-14265/com.testing.listapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.testing.listapp, PID: 14265
    java.lang.NullPointerException: Attempt to invoke virtual method 'void com.testing.listapp.TestActivity$CustomListViewAdapter.remove(java.lang.Object)' on a null object reference
            at com.testing.listapp.TestActivity.onContextItemSelected(TestActivity.java:5454)
            at android.app.Activity.onMenuItemSelected(Activity.java:2905)
            at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:4701)
            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 com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:167)
            at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:1082)
            at android.widget.AdapterView.performItemClick(AdapterView.java:305)
            at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
            at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
            at android.widget.AbsListView$3.run(AbsListView.java:3860)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            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:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Logcat output #2 :

11-13 10:45:21.229  21432-21432/com.testing.listapp D/AndroidRuntime﹕ Shutting down VM
    --------- beginning of crash
11-13 10:45:21.246  21432-21432/com.testing.listapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.testing.listapp, PID: 21432
    java.lang.UnsupportedOperationException
            at java.util.AbstractList.remove(AbstractList.java:638)
            at java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:75)
            at java.util.AbstractCollection.remove(AbstractCollection.java:229)
            at android.widget.ArrayAdapter.remove(ArrayAdapter.java:244)
            at com.testing.listapp.TestActivity.onContextItemSelected(TestActivity.java:5468)
            at android.app.Activity.onMenuItemSelected(Activity.java:2905)
            at com.android.internal.policy.impl.PhoneWindow$DialogMenuCallback.onMenuItemSelected(PhoneWindow.java:4701)
            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 com.android.internal.view.menu.MenuDialogHelper.onClick(MenuDialogHelper.java:167)
            at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:1082)
            at android.widget.AdapterView.performItemClick(AdapterView.java:305)
            at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
            at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
            at android.widget.AbsListView$3.run(AbsListView.java:3860)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            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:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
user2308699
  • 239
  • 1
  • 5
  • 14
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Bö macht Blau Nov 13 '17 at 18:39

1 Answers1

1

Try to change the line

CustomListViewAdapter customListViewAdapter = new CustomListViewAdapter(this, name, lastmod);

to:

customListViewAdapter = new CustomListViewAdapter(this, name, lastmod);

just curious if is the local variable, anyway the instance variable CustomListViewAdapter has been initialized.

Have a look to the famous SO post that usually is given when there are NPE

trocchietto
  • 2,607
  • 2
  • 23
  • 36
  • As you suggested, I removed "CustomListViewAdapter" from the line of code, now the app crashes with this error: java.lang.UnsupportedOperationException – user2308699 Nov 13 '17 at 18:50
  • Possibly you solved the Null Pointer Exception, I guess this is a different error. In SO we so One question one solution. Anyway please have a look at this, possibly it helps https://stackoverflow.com/questions/2965747/why-do-i-get-an-unsupportedoperationexception-when-trying-to-remove-an-element-f if not I suggest you to start a new post and accept my question, I will try to help you on the new bus as well – trocchietto Nov 13 '17 at 18:57
  • I reviewed the post and answers at the link you provided. I didn't find a solution to the error “ UnsupportedOperationException”. – user2308699 Nov 13 '17 at 19:38
  • could you post the stacktrace?(logcat output) – trocchietto Nov 13 '17 at 19:39
  • created a chat https://chat.stackoverflow.com/rooms/158894/room-for-trocchietto-and-user2308699 – trocchietto Nov 13 '17 at 19:43
  • Per your request, I posted Logcat output #2. – user2308699 Nov 13 '17 at 19:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158898/discussion-between-trocchietto-and-user2308699). – trocchietto Nov 13 '17 at 20:05
  • instead of extends ArrayAdapter use ArrayAdapter> we should continue in chat is not allowed to write a lot of comments. Also please consider that you should do a post apart and possibly accept my answer. Is confusing for future users. One question, one Post – trocchietto Nov 13 '17 at 20:08