Problem inserting data into a vector

3

I have the classes Port, Building and main:

public class Porta {
boolean aberta;
String cor;
double dimensaoX, dimensaoY, dimensaoZ;

void abre() {
    this.aberta = true;
}
void fecha() {
    this.aberta = false;
}
void pinta(String cor) {
    this.cor = cor;
}
boolean estaAberta() {
    boolean estaAberta = false;
    if(aberta == true) estaAberta = true;
    return estaAberta;
}



public class Edificio {
String cor;
int totalDePortas;
int totalDeAndares;
Porta portas[];
int elementos = 0;

void pinta(String cor) {
    this.cor = cor;
}

int quantasPortasEstaoAbertas() {
    int qtdPortasAbertas=0;
    for(int i=0; i<this.portas.length; i++) {
        if(portas[i].aberta == true) qtdPortasAbertas++;
    }
    return qtdPortasAbertas;
}

void adicionaPorta(Porta porta) {
    for(int i=0; i<this.portas.length; i++) {
        if(this.portas[i] == null) this.portas[i] = porta;
    }
}

int totalDePortas() {
    int totalPortas=0;
    for(int i=0; i<this.portas.length; i++) {
        if(this.portas[i] != null) totalPortas++;
    }
    return totalPortas++;
}

void adicionaAndar() {
    this.totalDeAndares = totalDeAndares+=1;
}

int totalDeAndares() {
    return this.totalDeAndares;
}


public class Exer05 {

public static void main(String[] args) {
    //A
    Porta p1 = new Porta();
    p1.abre();
    p1.fecha();
    p1.pinta("Azul");
    p1.pinta("Verde");
    p1.dimensaoX = 1.80;
    p1.dimensaoY = 1.00;
    p1.dimensaoZ = 1.80;
    if(p1.estaAberta() == true) System.out.println("A porta"
            + " está aberta");
    else System.out.println("A porta está fechada");

    //B
    Edificio ed = new Edificio();

    Porta p2 = new Porta();
    p1.fecha();
    ed.adicionaPorta(p2);
    Porta p3 = new Porta();
    p1.abre();
    ed.adicionaPorta(p3);
    Porta p4 = new Porta();
    p1.abre();
    ed.adicionaPorta(p4);
    Porta p5 = new Porta();
    p1.abre();
    ed.adicionaPorta(p5);
    Porta p6 = new Porta();
    p1.abre();
    ed.adicionaPorta(p6);
    Porta p7 = new Porta();
    p1.fecha();
    ed.adicionaPorta(p7);

    ed.quantasPortasEstaoAbertas();

}

It happens that when I call the method AddAut () in class main, the following error occurs:

Exception in thread "main" java.lang.NullPointerException
at br.edu.utfpr.exer05.Edificio.adicionaPorta(Edificio.java:23)
at br.edu.utfpr.exer05.Exer05.main(Exer05.java:24)

How to solve?

Thank you

    
asked by anonymous 24.05.2015 / 03:47

2 answers

4

Dude, why do not you try to use the ArrayList of java.util? It is easier to insert and treat Array data.

import java.util.ArrayList;
import java.util.List;

public class Edificio{
    private List<Porta> portas = new ArrayList<Porta>();
    public void adicionaPorta(Porta porta){
        this.portas.add(porta);
    }
}

This way you create a Port ArrayList, and with the add method you add a new item to it. Then, if you need to, you can fetch the value using the get method.

    
24.05.2015 / 04:05
3

@Rodrigo, You have only declared the Porta porta[] attribute, so you are giving NullPointerException ;

In case you did not start up:

Porta porta[] = new Porta[100]; //um vetor de 100 portas

Ref .: link

    
24.05.2015 / 04:05