1

The Eclipse IDE complains about the for loop using/accessing the array of books being out of bounds. The line (19) it complains about is: if (books[x] == null) {

I do not believe is the problem its complaining about as I have replaced that if code with many different things and it still complains. One line up is the first line of the for loop which is for (int x = 0; x < capacity ; ++x)

I have also triple checked the condition is right and it should be. capacity is 5 meaning the array of objects positions would be at 0, 1, 2, 3, 4 so starting x at 0 should be right from what I know about arrays.

Library Class (The one with the loop)

    package exercises;

    public class Library {
        private int capacity;
        private Book[] books = new Book[capacity];
        public Library(int capacity) {
            if (capacity > 1) {
                this.capacity = capacity;
            }
            else {
                this.capacity = 4;
            }
        }
        public boolean addBook(Book book) {
            int freeLocation = -1;
            @SuppressWarnings("unused")
            int notFreeLocation = -1;
            for (int x = 0; x < capacity ; ++x) {
                if (books[x] == null) { /*this is line 19*/
                    freeLocation = x;
                }
                else {
                    notFreeLocation = x;
                }
            }

            if (freeLocation == -1) {
                return false;
            }
            else {
                books[freeLocation] = book;
                return true;
            }
        }

The Library App class

    package exercises;

    public class LibraryApp {

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Library library = new Library(5);
            library.addBook(new Book("The Lord of the Rings", "J. R. R. Tolkien"));
            library.addBook(new Book("Harry Potter and the Philosopher's Stone", "J. K. Rowling"));
            library.addBook(new Book("1984", "George Orwell"));
            library.addBook(new Book("Where the Wild Things Are", "Maurice Sendak"));
            library.addBook(new Book("The Hitchhiker's Guide to the Galaxy", "Douglas Adams"));
            System.out.println(library);
            Book aBook = library.borrow("1984");
            System.out.println("Book borrowed: " + aBook);
        }

    }

I get the error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at exercises.Library.addBook(Library.java:19) at exercises.LibraryApp.main(LibraryApp.java:8)

2 Answers 2

2

When this is done in Library:

private int capacity;
private Book[] books = new Book[capacity];

the code in your constructor hasn't run yet (those initializations are inserted into your constructor at the very beginning [or just after super() in a subclass]). So capacity has its default value, 0. Later you assign to capacity, but it's too late.

Instead:

public class Library {
    private int capacity;
    private Book[] books;                     // *** Don't initialize it here
    public Library(int capacity) {
        if (capacity > 1) {
            this.capacity = capacity;
        }
        else {
            this.capacity = 4;
        }
        this.books = new Book[this.capacity]; // *** Initialize it here
    }

But there's another useful thing to learn here. When looping through an array or similar, use the array's knowledge of how big it is, not some other source of information (capacity). So:

for (int x = 0; x < this.books.length ; ++x) {
// -----------------^^^^^^^^^^^^^^^^^

Stick with the primary source of truth. :-) (In fact, you probably don't need your capacity instance member at all.)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for the help T.J! This solved my problem. I struggled with the idea of object arrays and how they worked as well as not fully understanding constructors hence not understanding that capacity is 0 as I initialized it to early. Also in terms of using the "primary source of truth" I shall definitely do that in the future :D You have helped a lot as my mid terms are coming up in a week and I am more then happy I figured this out now rather then in the test!
0

You try to make an array with 0 (default int) size. Move the

private Book[] books = new Book[capacity];

to constructor after assigning value to capacity.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.