Object array gives error when I try to access an element of it

Asked

Viewed 71 times

2

package br.com.cursoemvideo;

public class Olho {
    private String cor;
    private boolean funciona;

    public String getCor() {
        return cor;
    }
    public void setCor(String cor) {
        this.cor = cor;
    }
    public Boolean getFunciona() {
        return funciona;
    }
    public void setFunciona(Boolean funciona) {
        this.funciona = funciona;
    }
    public Olho(String cor, Boolean funciona) {
        this.cor = cor;
        this.funciona = funciona;
    }

    public void verificar() {
        System.out.println("A cor do olho é: " +this.getCor());
        System.out.println("O olho funciona ? " +this.getFunciona());
    }
}

Class of the main programme:

 package br.com.cursoemvideo;

    public class Aula_test {

        public static void main(String[] args) {
            Olho[] ol = new Olho[1];

            ol[0] = new Olho("Castanho",true);
            ol[1] = new Olho("Azul",false);

            ol[0].verificar();
        }

    }

And the error appears:

Exception in thread "main" java.lang.Arrayindexoutofboundsexception: 1 at br.com.cursoemvideo.Aula_test.main(Aula_test.java:9)

2 answers

2


You declared the array with capacity for one element, then tried to fill 2 elements. The second gives error.

Maybe you’re thinking the one in the statement of the array indicates which is the last element, but is not, it indicates how many are. there is not the index of array, the index is only used in the last row when accessing the element (could access element 1 as well). So if you want 2 elements you need to use 2 in the declaration.

class Main {
    public static void main(String[] args) {
        Olho[] ol = new Olho[2];
        ol[0] = new Olho("Castanho", true);
        ol[1] = new Olho("Azul", false);
        ol[0].verificar();
    }
}

class Olho {
    private String cor;
    private boolean funciona;
    public String getCor() {
        return cor;
    }
    public void setCor(String cor) {
        this.cor = cor;
    }
    public Boolean getFunciona() {
        return funciona;
    }
    public void setFunciona(Boolean funciona) {
        this.funciona = funciona;
    }
    public Olho(String cor, Boolean funciona) {
        this.cor = cor;
        this.funciona = funciona;
    }
    public void verificar() {
        System.out.println("A cor do olho é: " + getCor());
        System.out.println("O olho funciona ? " + getFunciona());
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • It worked, but now I’m confused. I took an online course in which the teacher says that the vector starts at position 0. So I thought that if I wanted a vector with 2 elements it would have to have index 1 (the count being 0.1)

  • That’s what I said (I realized I was confusing it with index), there you are not putting an index, you are putting how many elements should fit in this array, are separate things.

  • True, now I understand. Thank you !

2

Solution:

public static void main(String[] args) 
    {
        Olho[] ol = new Olho[2];

        ol[0] = new Olho("Castanho",true);
        ol[1] = new Olho("Azul",false);

        ol[0].verificar();
    }

Note that in the code you passed, you are declaring an array of only one position new Olho[1], and at the same time you try to fill two positions for him, so the mistake:

Exception in thread "main" java.lang.Arrayindexoutofboundsexception: 1 at br.com.cursoemvideo.Aula_test.main(Aula_test.java:9)

Note that the error message itself, already indicates the problem, just reading, already says that in thread main has an error related to a array with Indice out of bounds in line 9

Browser other questions tagged

You are not signed in. Login or sign up in order to post.