Rebuild and Retry, 57 of 100 Days of Code
I added logic to the app before I was stopped short due to a familiar runtime error.
You can find a video at the bottom of the page and a link to the source code here.
I added a reference variable, dummyHome which will be used to instantiate copies of each Home object in the original list. I also added a String variable homeString which will hold the return value of the Home.toString() method after it is invoked on each Home object. Finally, I included a for loop which iterates through the ArrayAdapter object containing the list of Home objects.
During iterations, the return value of each invocation of Home.toString() is assigned to the homeString variable. This variable is used as an argument to instantiate each dummyHome object with the Home(String s) constructor.
Each object stored in an ArrayAdapter object is considered an Object instead of their subclass type. This means the Home objects stored in the original_adapter object are considered Objects and the program cannot invoke methods of the Home class upon them.
This creates a problem when checking the zipcode of each Home object since I cannot invoke the Home.getZipCode() method on these Objects. To get around this, I want the application to create copies of each Home object stored in the ArrayAdapter to gain access to the methods of the Home class. But before this, I have a new problem to solve.
It looks like my application was throws an IndexOutOfBoundsException when I hit enter after typing text into the EditText field. This means the application is using a number outside the size of the original adapter as an index.
But how? The for loop uses an int variable, adapterSize, as a counter for the iterations. It is assigned the size of the original adapter so it shouldn’t go outside of this range.
To be sure, I changed the code to output this integer to the terminal during the loop. Sure enough, it output the number 3. I then changed the code again to print original_adapter.getCount(). This was when things got interesting. The program prints the number 0.
I played around with the code and after rebuilding the app, the program output the size as 3. Maybe I wasn’t testing updated code and that’s why the size of the original_adapter ArrayAdapter updated when the app was rebuilt. My next step will be to instantiate the dummy Home object using the homeString variable.
For more on this, watch the YouTube video below. For the source code click here. To read more about these exceptions, check out the Oracle documentation here
Join the mailing list to see updates like this every week!
Coding Fanatic
Image by free stock photos from www.picjumbo.com from Pixabay