Java Nullpointerexeption, agenda inserts no compromise

Asked

Viewed 84 times

0

I have a project for the college, a simple schedule, no graphic parts 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 to insert contact, insert commitment, delete contact, delete appointment, list contact and list appointment and the Test class is the main class.

The whole contact part works, but the insert commitment methods returns me "Exception in thread "main" java.lang.Nullpointerexception"

Code of the Commitment Class.

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;
    }

}

Code of Methods 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++;
    }

Code class Test:

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

Does this mistake only happen on my computer? how do I fix it?

  • Which line crashes the error? You can post the stack trace?

1 answer

0


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

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++;
}

Browser other questions tagged

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