0

I have a class that contains 4 Calendar variables:

public class Ponto {
    Calendar entrada;
    Calendar saidaAlmoco;
    Calendar entradaTarde;
    Calendar saida;

    public Ponto(Calendar entrada, Calendar saidaAlmoco, Calendar entradaTarde, Calendar saida) {
        this.entrada = entrada;
        this.saidaAlmoco = saidaAlmoco;
        this.entradaTarde = entradaTarde;
        this.saida = saida;
    }

    public void setEntrada(Calendar entrada) {
        this.entrada = entrada;
    }

    public void setSaidaAlmoco(Calendar saidaAlmoco) {
        this.saidaAlmoco = saidaAlmoco;
    }

    public void setEntradaTarde(Calendar entradaTarde) {
        this.entradaTarde = entradaTarde;
    }

    public void setSaida(Calendar saida) {
        this.saida = saida;
    }
}

Then when I try to send the values via set() function i it gives the error:

Attempt to invoke virtual method 'void pt.eu.metaloapp.Ponto.setEntrada(java.util.Calendar)' on a null object reference;

This is the code:

Calendat picagem = Calendar.getInstance();
String hora = sdf.format(picagem.getTime()) + "h";

switch (verificarPonto()) {
      case 1:
         txtHoraEntrada.setText(hora);
         ponto.setEntrada(picagem);
         break;
      case 2:
         txtHoraSaidaAlmoco.setText(hora);
         ponto.setSaidaAlmoco(picagem);
         break;
      case 3:
         txtHoraEntradaTarde.setText(hora);
         ponto.setEntradaTarde(picagem);
         break;
      case 4:
         txtHoraSaida.setText(hora);
         ponto.setSaida(picagem);
         break;
       }

Is there any problem with sending Calendar values to a class or something?

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • Where is declared and initialized `ponto`? – lealceldeiro May 17 '19 at 08:49
  • That suggests `ponto` is null. You haven't shown us where `ponto` is declared or assigned, but that's where you should be looking. Note that you'd have the same problem calling *any* method on `ponto` - the fact that you're calling set methods is irrelevant. – Jon Skeet May 17 '19 at 08:49

1 Answers1

1

You need to call new Ponto() somewhere, ponto is null in your code.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163