-2

When size of data is 600, there is no exceltion. When the size of the data is 800 I am getting following exception, after completing all the iterations and exiting the loop at following line:

'imageAdapter = new ImageAdapter(MainActivity.getAppContext(), posters);'

Issue is not related to splitArray[] which is inside the loop. Please note that it works for smaller size data. Please help.

FATAL EXCEPTION: main
Process: com.udacityproject.svs.popularmovies, PID: 12517
java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
at com.udacityproject.svs.popularmovies.MainActivity$1.deliverResult(MainActivity.java:130)
at com.udacityproject.svs.popularmovies.MainActivity$1.deliverResult(MainActivity.java:57)
at android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:255)
at android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:80)
at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:487)
at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:504)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Constructor for Image Adapter :

    public ImageAdapter(@NonNull Context context, @NonNull Poster[] objects){
        super(context, 0, objects);
        posters = objects;
    }

Function:

    public void deliverResult(String[] data) {

        if (data != null) {
            Poster[] posters = new Poster[data.length];
            for (int i=0; i<data.length; i++) {
                String imagePath = "http://image.tmdb.org/t/p/w185/" ;
                int imageId;
                String title;
                String overview;
                String rating;
                String releaseDate;
                String[] splitArray;
                try {
                    splitArray = data[i].split("[*]");
                } catch (PatternSyntaxException e) {
                    e.printStackTrace();
                    return;
                }
                imagePath+=splitArray[2];
                imageId = Integer.valueOf(splitArray[0]);
                title = splitArray[1];
                rating = splitArray[3];
                overview = splitArray[4];
                releaseDate = splitArray[5];
                posters[i] = new Poster( imageId, imagePath, title, overview, rating, releaseDate);
            }
            imageAdapter = new ImageAdapter(MainActivity.getAppContext(), posters);
            mMoviePoster.setAdapter(imageAdapter);
        }

    }
  • 3
    I am pretty sure the `split` isn't returning what you expect .... All I can say is that you try to access `array[5]` where the `array.length == 5`. Please provide the content of `data[i]` where it fails. (add a `try-catch` and print the line that fails.). You can [edit] your question with that info – AxelH May 29 '18 at 10:14
  • `splitArray[5]` is the problem. check the size of it – Jyoti JK May 29 '18 at 10:15
  • if (splitArray.length >= 5) {} - add this condition – Adil May 29 '18 at 10:24
  • 1
    If the problem lies in `new ImageAdapter(MainActivity.getAppContext(), posters);` then you should add the constructor of the class to your question. Also whenever you're getting an exception, show us the **stack trace**, not only the exception message. – QBrute May 29 '18 at 10:36
  • Now what is at line 130? `releaseDate = splitArray[5];`? – QBrute May 29 '18 at 11:14
  • @QBrute at line 130, imageAdapter = new ImageAdapter(MainActivity.getAppContext(), posters); this line is there. – codingcorgi May 29 '18 at 11:20
  • And what's in ImageAdapter constructor? – Sergey Glotov May 29 '18 at 14:08
  • public ImageAdapter(@NonNull Context context, @NonNull Poster[] objects) { super(context, 0, objects); posters = objects; } – codingcorgi May 30 '18 at 03:13
  • Everything in the constructor is ok. What's at line 57? `MainActivity.java:57` – Sergey Glotov May 30 '18 at 14:46

1 Answers1

2

One of the line included between the 600th and 800th line is incomplete.

Add this following line :

assert splitArray.length > 4 : "Following line lacks a column: " + data[i];

after :

splitArray = data[i].split("[*]");

It should tell you which line lacks the fifth column

Camille Vienot
  • 619
  • 5
  • 6
  • Can you please have a closer look at the question again ? Issue is after exiting the loop, not inside. – codingcorgi May 29 '18 at 10:29
  • if assertion are activated. -ae I think – AxelH May 29 '18 at 10:29
  • 2
    @codingcorgi please take a closer look to your bigger `data` array instead, The exception mention that size of the array that fails. And it is only 5 cells. – AxelH May 29 '18 at 10:29