1

I am getting the error: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference . I am trying to figure out the exact problem but unable to pinpoint the exact location. Was hoping someone could potentially help. I have 2 separate layouts with 2 separate adapters and list views. Been struggling on this for a while and would appreciate a solution. Much Appreciated!

    public class viewContacts extends AppCompatActivity {

      private static final String TAG = viewContacts.class.getSimpleName();
      ListView listView_Android_Contacts;
      ListView viewContacts;
      View currentSelected;
      Cursor c;
      String contactName;
      CardView saveContacts;
      String phoneNumber;
      CheckBox cb;
      String currentContactName;
      ArrayList<String> selectedContacts;
      ArrayList<String> contacts;
      ArrayList<String> listMessages;
      LinearLayout linearMain;
      ListView currentContactsList;
      String contactEntry;
      public List<String> savedContacts;

      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contacts_list);
        final ListView currentContactsList = findViewById(R.id.currentContactsList);
        setContentView(R.layout.activity_view_contacts);
        saveContacts = findViewById(R.id.saveContacts);
        final ListView viewContacts = findViewById(R.id.contactsView);
        Dexter.withActivity(this)
                .withPermission(Manifest.permission.READ_CONTACTS)
                .withListener(new PermissionListener() {
                    @Override
                    public void onPermissionGranted(PermissionGrantedResponse response) {
                        get();
                    }


                    @Override
                    public void onPermissionDenied(PermissionDeniedResponse response) {
                        // check for permanent denial of permission
                        if (response.isPermanentlyDenied()) {
                            // navigate user to app settings
                        }
                    }


                    @Override
                    public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) {
                        token.continuePermissionRequest();
                    }
                }).check();
        final ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this, android.R.layout.simple_list_item_multiple_choice, contacts
        );
        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, selectedContacts);
        viewContacts.setAdapter(adapter);
        currentContactsList.setAdapter(arrayAdapter);
        viewContacts.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String selectedFromList = (String) viewContacts.getItemAtPosition(position);
                Toast.makeText(viewContacts.this, selectedFromList,Toast.LENGTH_SHORT).show();
            }
        });
        saveContacts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectedContacts = new ArrayList<>();
                List<Integer> checkedIDs = new ArrayList<Integer>();
                final SparseBooleanArray checkedItems = viewContacts.getCheckedItemPositions();
                for (int i = 0; i < checkedItems.size(); i++){
                    final boolean isChecked = checkedItems.valueAt(i);
                    if (isChecked){
                        final int position = checkedItems.keyAt(i);
                        String s = (String) viewContacts.getItemAtPosition(position);
                        selectedContacts.add(s);
                        checkedIDs.add(position);
                    }

                }
                startActivity(new Intent(viewContacts.this, contactsList.class));
            }
        });
    }

    public void get(){

        c = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.Contacts.DISPLAY_NAME );
        contacts = new ArrayList<>();
        while(c.moveToNext()){
            contactName = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            phoneNumber = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            contacts.add( contactName + "\n" + phoneNumber);

            Log.d(TAG, "show contacts:");
        }
        c.close();
    }
}
papaya
  • 1,505
  • 1
  • 13
  • 26
  • Please attach the error log also...Even you pinpoint the error, others may see more useful information from that in order to help you. – Anjana Jan 12 '20 at 07:18
  • Seeing the code It seems that it could be on the line ```checkedItems.size()``` But you can figure it out from the error logs It would say something like ```Caused by: SomeClass.java:123``` The 123 is the line number that caused the error. – Vishal Jan 12 '20 at 07:20
  • U r using two setContentView() in your code....remove one of them. – Prajwal Waingankar Jan 12 '20 at 07:22

1 Answers1

0

The only place where you call size() in the code is here:

        for (int i = 0; i < checkedItems.size(); i++){

Assuming that that is where the exception is thrown, checkedItems must be null.

And therefore viewContacts.getCheckedItemPositions() must be returning null.

And that is as far as we can go with the information you have provided.

Over to you to do the rest of the diagnosis.


UPDATE

It appears that you are calling setContentView twice with different views. That won't work. An Activity has exactly one content view.

That probably explains NPE. One of the two views won't be (cannot be) wired into the Activity and that's probably why it is returning the null.

You need to create a layout that contains / combines your two layouts. Or maybe switch between the two layouts. (Not sure ...)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I think you are right, the line that cause the error was currentContactsList.setAdapter(arrayAdapter); The reason I changed the view is because the list I want to use is from a different activity and I want to update the list from within my current activity. What do you suggest I do? Appreciate the response. – Jean Van Niekerk Jan 12 '20 at 07:34