This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Zero to AI - A non-technical, hype-free guide to prospering in the AI era and have Nicolò Valigi and Gianluca Mauro on-line!
See this thread for details.
Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

ArrayIndex outof bounds

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good day, thanks for this amazing portal...

So I am learning how to create apps, and I have to write an Instagramclone.. so I followed the Coding with Mitch tuts on Youtube.. amazing stuff I learned a lot...BUT... I ran into a problem:

So I have been tracking a problem and am not sure what is going on, Could you please tell me or show me, or even point me in the right direction...

the error points to another section but I traced it to an empty array:

so I have an Filepaths class:



in another Class I call am getting my file directories and file paths:



I also have a init Fuction to



I have read up, followed other solutions, I even uploaded new data to the phone and Firebase...

Please help, or just explain to me how I can search for a solution...

Thanks
 
Greenhorn
Posts: 5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try debugging the code? Maybe you'll find the cause for the OutOfBounds exception
 
Sheriff
Posts: 3203
475
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please share the stack trace for the exception.
 
Ron McLeod
Sheriff
Posts: 3203
475
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your stack trace information ...

stackTrace:



the code it points to:


and this calls the function:



which call the function filepaths... in the forementioned code

Thanks............
 
Ron McLeod
Sheriff
Posts: 3203
475
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ben van Jaarsveld wrote:

Is that the complete stack trace?
 
Saloon Keeper
Posts: 12399
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

The obvious question is, are you sure that the directories you are listing have files in them?

Can you show us the stack trace? I don't think the error has anything to do with the line you indicated.

There are many issues with your code you can fix though.

  • Don't create a class for the sole purpose of holding constants. Keep constants in the class that uses them. Make them static and final.
  • Don't use String or File to represent file paths. Use Path. This advice may be different for Android though.
  • Don't use more specific types than you need. Use List or Set for the return types of your methods, not ArrayList.
  • Don't name variables of a collection type "array". They're not arrays.
  • Prefer enhanced for-loops over indexed loops, or use the stream API.
  • Don't access a collection twice for the same element. Store the element in a local variable.
  • Don't perform unnecessary null-checks.
  • You don't need to keep track of a list of directories if you use Path, because you can just look up the parent directory.


  • Your problem is most likely caused by not using the Android API correctly though. You should probably be using Context, ContentResolver and MediaStore to look for images.
     
    Ben van Jaarsveld
    Ranch Hand
    Posts: 51
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Ron McLeod wrote:

    Is that the complete stack trace?

    here you go Sir!!

     
    Ben van Jaarsveld
    Ranch Hand
    Posts: 51
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Stephan van Hulst wrote:Welcome to CodeRanch!

    The obvious question is, are you sure that the directories you are listing have files in them?

    Can you show us the stack trace? I don't think the error has anything to do with the line you indicated.

    There are many issues with your code you can fix though.

  • Don't create a class for the sole purpose of holding constants. Keep constants in the class that uses them. Make them static and final.
  • Don't use String or File to represent file paths. Use Path. This advice may be different for Android though.
  • Don't use more specific types than you need. Use List or Set for the return types of your methods, not ArrayList.
  • Don't name variables of a collection type "array". They're not arrays.
  • Prefer enhanced for-loops over indexed loops, or use the stream API.
  • Don't access a collection twice for the same element. Store the element in a local variable.
  • Don't perform unnecessary null-checks.
  • You don't need to keep track of a list of directories if you use Path, because you can just look up the parent directory.


  • Your problem is most likely caused by not using the Android API correctly though. You should probably be using Context, ContentResolver and MediaStore to look for images.



    Thank you for all this information.. I will definitely consider all this in future projects... the project that this pertains to is a tutorial on Youtube that is about 2 years(or more) old and I had/have a couple of problems with the code.. and sorting it all out is all I have been doing for about a week now... But I must say, doing it this way I have learned heaps and heaps... thanks for all the help
     
    Ben van Jaarsveld
    Ranch Hand
    Posts: 51
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Stephan van Hulst wrote:

    The obvious question is, are you sure that the directories you are listing have files in them?



    I have even gone and changed the directories, loaded files... I made sure that ,that was not the problem...
     
    Ben van Jaarsveld
    Ranch Hand
    Posts: 51
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    GalleryFragment.class
     
    Ron McLeod
    Sheriff
    Posts: 3203
    475
    Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Does this stack entry: com.benvanj.android.outstagram.Share.GalleryFragment.setupGridView(GalleryFragment.java:165)
    correspond to this line of code: setImage(imgURLs.get(0), galleryImage, mAppend); ??

    If you are trying to view images for a directory which contains zero files, the imgURLs list will be empty.  Trying to reference element 0 (the first element in the list) will result in an IndexOutOfBoundsException since the list doesn't contain any elements.
     
    Ben van Jaarsveld
    Ranch Hand
    Posts: 51
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Ron McLeod wrote:Does this stack entry: com.benvanj.android.outstagram.Share.GalleryFragment.setupGridView(GalleryFragment.java:165)
    correspond to this line of code: setImage(imgURLs.get(0), galleryImage, mAppend); ??



    yes it does...


    Ron McLeod wrote:
    If you are trying to view images for a directory which contains zero files, the imgURLs list will be empty.  Trying to reference element 0 (the first element in the list) will result in an IndexOutOfBoundsException since the list doesn't contain any elements.



    Please tell me where to look, to find this information, then I could add files, and test it...
     
    Ben van Jaarsveld
    Ranch Hand
    Posts: 51
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    the

    imgURLs

    goes a couple of lines before:



    so if i go to the getfilepaths function:




    isn't this listfiles array, the array that populates imgURLs???

    then that means that the listfiles array is empty???(am I wrong??)
     
    Ron McLeod
    Sheriff
    Posts: 3203
    475
    Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Ben van Jaarsveld wrote:Please tell me where to look, to find this information, then I could add files, and test it...


    You need to verify that the List is not empty before attempting to reference the first element.

    For example:
     
    Ben van Jaarsveld
    Ranch Hand
    Posts: 51
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks...

    the

    imgURLs

    in your if statement then turns red ....i've tried making not final... but it didn't work
     
    Ron McLeod
    Sheriff
    Posts: 3203
    475
    Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Ben van Jaarsveld wrote:the imgURLs in your if statement then turns red ....i've tried making not final... but it didn't work


    You should spend some and get familiar with the IDE that you are using (Android Studio / JetBrains ??) so that you can troubleshoot beyond the statement turns red.

    Anyway, in this case, there was a typo in my example -- should have been imgURLs rather than imgUrls
     
    Ben van Jaarsveld
    Ranch Hand
    Posts: 51
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for all your help...(Android Studio)

    please excuse my terminology... it is late here and I think I am just tired... what I meant to say was...  "Cannot resolve symbol"

    Anyway, in this case, there was a typo in my example -- should have been imgURLs rather than imgUrls



    and that is what I got for just copying and pasting and not typing it out for myself..    
     
    He's my best friend. Not yours. Mine. You can have this tiny ad:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic