Akhundzade
Akhundzade

Reputation: 93

Weird bug from from inner class

I got this weird error.And I can't figured out why this is happening.

Here is the problem

If I put this code inside of firebaseproblem.onDataChange I got an error.

String pl = "Ali";
     items[0] = pl;

If I put anywhere else it works perfectly... It is items[0] for debugging purposes

Error Log

07-20 12:16:06.605 2720-2720/azcrew.eatapp E/UncaughtException: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
                                                                at azcrew.eatapp.halfdone.listComp$2.onDataChange(listComp.java:98)
                                                                at com.google.android.gms.internal.zzbmz.zza(Unknown Source)
                                                                at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source)
                                                                at com.google.android.gms.internal.zzboc$1.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:155)
                                                                at android.app.ActivityThread.main(ActivityThread.java:5696)
                                                                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:1028)
                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
07-20 12:16:06.845 2720-2720/azcrew.eatapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                         Process: azcrew.eatapp, PID: 2720
                                                         java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
                                                             at azcrew.eatapp.halfdone.listComp$2.onDataChange(listComp.java:98)
                                                             at com.google.android.gms.internal.zzbmz.zza(Unknown Source)
                                                             at com.google.android.gms.internal.zzbnz.zzYj(Unknown Source)
                                                             at com.google.android.gms.internal.zzboc$1.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:155)
                                                             at android.app.ActivityThread.main(ActivityThread.java:5696)
                                                             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:1028)
                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

Here is the full code

public class listComp extends AppCompatActivity {
    ListView listView;
    String[] id;
    String[] items = new String[20];
    int index = -1;
    static String pl;
    listMVP l = new listMVP();

    private static final String TAG = "test";

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

        listView = (ListView) findViewById(R.id.listcomp);

        Bundle b = this.getIntent().getExtras();
        id = b.getStringArray("id");

        firebaseproblem1();

        removingnull();

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.item1, items);
        listView.setAdapter(adapter);



        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int d, long l) {
                String a = ((TextView) view).getText().toString();

                for(int ind = 0; d<items.length;ind++){
                 if(items[ind].equals(a)){
                        index = ind;
                     break;
                    }
                }
                //intentIn();
            }
        });
}


    private void firebaseproblem1() {
        for(int i=0; i<id.length;i++){
            if(id[i]!= null){
                DatabaseReference ref5 = FirebaseDatabase.getInstance().getReference();
                DatabaseReference q = ref5.child("Restoran").child(id[i]).child("id");

                l.setI(i);
                final int finalI = i;
                q.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    pl = "Ali";
                    items[0] = pl;
                }

                @Override
                 public void onCancelled(DatabaseError databaseError) {
                }
                });
            }
        }
    }

    private void removingnull() {
        List<String> list = new ArrayList<String>();
        for(String s: items){

            if(s!= null){
                list.add(s);
            }
        }
        items = list.toArray(new String[list.size()]);
    }

    private void intentIn() {
        Bundle c = new Bundle();
        c.putString("resId",id[index]);
        Intent i1 = new Intent(this, ListMain.class);
        i1.putExtras(c);
        startActivity(i1);
    }
}

Upvotes: 2

Views: 65

Answers (1)

Vincent Mimoun-Prat
Vincent Mimoun-Prat

Reputation: 28541

Your onDataChanged function may be called any time in the future.

In the meantime, your code has called the removingnull function, which has reset the items array to an empty array.

If I put anywhere else it works perfectly

That is because that is executed synchronously before the removingnull function and thus the items array is still a String[20]

You can Log a few messages just to convince yourself of the order of execution of your code.


Suggested fix:

Forget the array, let items be a List<String>, you will not run into this kind of problems.

Upvotes: 1

Related Questions