javaandroidgoogle-cloud-firestoremultiautocompletetextview

java.lang.UnsupportedOperationException Error ArrayAdapter


I have a MultiAutoCompleteTextView where data is taken from Firestore. When i set the data in ArrayAdapter, i get an error like below. I followed the following method https://stackoverflow.com/a/2965808/2123594 , but I'm still confused where i should put Arrays.asList to my code.

Error :

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.learn.count, PID: 21871
    java.lang.UnsupportedOperationException
        at java.util.AbstractList.add(AbstractList.java:404)
        at java.util.AbstractList.add(AbstractList.java:425)
        at android.widget.ArrayAdapter.add(ArrayAdapter.java:179)
        at com.learn.count.addproducts.AddItems$4.onComplete(AddItems.java:147)
        at com.google.android.gms.tasks.zzj.run(Unknown Source)
        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)

MultiAutoCompleteTextView Code :

String[] arrayUoM = new String[]{null};
ArrayAdapter<String> adapterUoM = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayUoM);

// Check if document is available
collectionUoM.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (DocumentSnapshot documentSnapshot : task.getResult()) {
                if (task.getResult().isEmpty()) {
                    Log.e("TAG", "UoM not available");
                } else {
                    String getName = documentSnapshot.getString("uoMName");
                    adapterUoM.add(getName);
                }
                }
        }
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        Toast.makeText(AddItems.this, "Error : " + e.getMessage(), Toast.LENGTH_SHORT).show();
    }
});

MultiCompleteUoM.setAdapter(adapterUoM);
MultiCompleteUoM.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

Solution

  • Actually there are multiple problems in your code.

    Problem - 1: You have initialized your ArrayAdapter with array of null, which cause NullPointerExceptio.

    Problem - 2: You have used array of String String[] to create ArrayAdapter. The ArrayAdapter, on being initialized by an array, converts the array into a AbstractList (List) which cannot be modified. Hence it cause UnsupportedOperationException, when you try to add item to ArrayAdapter.

    Solution: To resolve both of the problem, You have to use ArrayList instead of array.

    ArrayList<String> arrayUoM = new ArrayList<>();