Java Nullpointerexeption, schedule does not insert appointment

0

I have a project for college, a simple agenda, with no graphics or database.

There are four classes: Contact, Commitment, Schedule and Test. In the Contact and Commitment classes we have only the basic variables and their constructors, getters and setters. In the Agenda class we have the methods insert contact, insert appointment, delete contact, delete appointment, list contact and list appointment, and Test class is the main class.

The whole of the contacts works, but the commit insert methods returns me "Exception in thread" main "java.lang.NullPointerException"

Commitment Class Code.

public class Compromisso {

    private String data;
    private String descricao;

    public Compromisso(String data, String descricao) {
        this.data = data;
        this.descricao = descricao;
    }

    public Compromisso() {
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getDescricao() {
        return descricao;
    }

    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }

}

Methods Code Insert Contact and Insert Commitment:

    private int qtdMaxContato = 100;
    private int qtdContato;
    private int qtdMaxCompromisso = 100;
    private int qtdCompromisso;
    private Contato[] Contato;
    private Compromisso[] Compromisso;  

    public Agenda() {
        this.Contato = new Contato[100];
        this.Compromisso = new Compromisso[100];
    }

    // Getters e Setters e outros métodos.

     public void InserirContato(String nome, String telefone, String celular, String email, String nascimento) {
        Contato novo = new Contato(nome, telefone, celular, email, nascimento);
        Contato[qtdContato] = novo;
        qtdContato++;
    }

    public void InserirCompromisso(String data, String descricao) {
        this.Compromisso[qtdCompromisso].setData(data);
        this.Compromisso[qtdCompromisso].setDescricao(descricao);
        qtdCompromisso++;
    }

Test class code:

   public static void main(String[] args) {
        Agenda a1 = new Agenda();
        a1.InserirCompromisso("22/22/22", "testetestetestetestetesteteste");
        a1.ListarCompromisso();
    }

Does this error only happen on my computer? How do I solve this?

    
asked by anonymous 06.11.2015 / 19:03

1 answer

0

When you are entering the appointment, all items in the Compromisso array are null, so the call to this.Compromisso[qtdCompromisso].setData(...) will fail with this error.

You need to create the object to put in the array before accessing it:

public void InserirCompromisso(String data, String descricao) {
    Compromisso novoCompromisso = new Compromisso(data, descricao);
    this.Comprimisso[qtdCompromisso] = novoCompromisso;
    qtdCompromisso++;
}

or

public void InserirCompromisso(String data, String descricao) {
    Compromisso novoCompromisso = new Compromisso();
    novoCompromisso.setData(data);
    novoCompromisso.setDescricao(descricao);
    this.Comprimisso[qtdCompromisso] = novoCompromisso;
    qtdCompromisso++;
}
    
06.11.2015 / 19:05