-3

I want to check the equality of image file path name . When run the app , the application crash and getting error in Constructor in Adapter class at for loop sentence is = for(int n = 0; n <= mStringArray.length; n++) in My Adapter Class.

Here is my ImageAdapter class Consrtuctor

  public ImageAdapter(Context c, String[] fpath) {
            context = c;
            filepath = fpath;

            TypedArray a = obtainStyledAttributes(R.styleable.MyGallery);
            itemBackground = a.getResourceId(R.styleable.MyGallery_android_galleryItemBackground, 0);
            a.recycle();

            if (newFolder.isDirectory()) {
                listFile = newFolder.listFiles();

                filepath = new String[listFile.length];
                for (int i = 0; i < listFile.length; i++)
                {
                    filepath[i] = listFile[i].getAbsolutePath();
                    Log.e(" filepath[i] " , " = " +  filepath[i]);
                }

                for(int m = 0 ; m <= filepath.length; m++)
                {
                    String firstValue= filepath[m];
                    Log.e("Image is ","firstValue = " + firstValue);

                    for(int n = 0; n <= mStringArray.length; n++)
                    {
                        String SecondValue = mStringArray[n];
                        Log.e("Image is ","Secondvalue = " + SecondValue);

                        if(firstValue.equalsIgnoreCase(SecondValue))
                        {
                            Log.e("Image is ","Equal");
                        }
                        else
                        {
                            Log.e("Image is ","Not Equal");
                        }
                    }
                }
            }
        }

Here is my Sqlite Query method

 public List<String> getAll_ImageAudioPath() {
        labels = new ArrayList<String>();

        dbhelper = new MyDbHelper(this);
        SQLiteDatabase db = dbhelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from ActivityObjectList where activityId ='" + strDescription + "'", null);

        if (cursor.moveToFirst()) {
            do {

                labels.add(cursor.getString(cursor.getColumnIndex("imageaudioPath")));
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        int strLabelSize = labels.size();
        Log.e("Value of strLabelSize ", " = " + strLabelSize);

        for (int i = 0; i < labels.size(); i++) {
            Log.e("Value of element ", " = " + i + " & " + labels.get(i));
            strLablepath = labels.get(i);
            Log.e("Value of strLablepath ", " = " + strLablepath);

            mStringArray = new String[labels.size()];
            mStringArray = labels.toArray(mStringArray);

            for(int x = 0; x < mStringArray.length ; x++){
                Log.e("mStringArray[i]  is","" + mStringArray[i]);
            }  
}

Can someone help me how to compare the two String Array with image path in Image Adapter.Thanks to appreciate.

Androiddv
  • 47
  • 10

2 Answers2

1

If you're getting the NullPointerException on this exact line

for(int n = 0; n <= mStringArray.length; n++)

then it can only be because mStringArray is null when you reach the loop. A NullPointerException results from dereferencing a null reference; that is, trying to access a field or method of something that's null. The only place on this line that you dereference anything is in the mStringArray.length expression, and this will give you an NPE if and only if mStringArray is null. It's not possible to verify this from the code you gave, though, because we can't see any other references to mStringArray.

The others' comments are probably right that you intended n < mStringArray.length rather than <=, but this won't result in a NPE in any case. It would be more likely to give an IndexArrayOutOfBoundsException, but in the body of the loop, not in the for statement.

chiastic-security
  • 20,430
  • 4
  • 39
  • 67
0

Use below code:

       for(int m = 0 ; m < filepath.length; m++)
            {
                String firstValue= filepath[m];
                Log.e("Image is ","firstValue = " + firstValue);

                for(int n = 0; n < mStringArray.length; n++)
                {
                    String SecondValue = mStringArray[n];
                    Log.e("Image is ","Secondvalue = " + SecondValue);

                    if(firstValue.equalsIgnoreCase(SecondValue))
                    {
                        Log.e("Image is ","Equal");
                    }
                    else
                    {
                        Log.e("Image is ","Not Equal");
                    }
               }
        }
Saritha G
  • 2,588
  • 1
  • 15
  • 27