NullPointerException error in running my program

1

I'm doing an activity with the following statement:

  

You should implement a digital clock in Java. For this you   should create three classes, namely: Counter, Clock and other class   to test execution (Java application) with the name TestRelog. In   Counter and Clock classes shall include the modifier methods and   access methods for the created attributes. The result of your   execution should display all possible clock times,   example:

     

24 hour format

00:00
00:01
00:02
00:03
00:04
00:05
.
.
.
23:59
     

12 hour format

12:00 a.m.
00:01 a.m.
00:02 a.m.
.
.
.
11:54 p.m.
11:55 p.m.
11:56 p.m.
11:57 p.m.
11:58 p.m.
12:00 a.m.

There should be an option to display 12-hour or 24-hour time. If the selection is made to display in the 12 hour format, it should be displayed a.m. or p.m. as the case may be.

My classes are:

public class Contador {

    private int contar;

    //Metodo construtor
    public void Contador(){
        this.contar = 0;
    }

    //add 1
    public void contar(){

        this.contar = (contar +1 ) % 100;
    }

    //set
    public void setContar(int num){

        this.contar = num;
    }

    //get
    public int getContar(){

        return this.contar;
    }


}//end of class

class clock

public class Relogio {

    //Campos da classe
    private Contador minutos; 
    private Contador horas;

    //Constrio Objeto a partir da classe relogio relogio usando 2 objetos do tipo contador
    public void Relogio(){

        this.horas = new Contador();
        this.minutos = new Contador();

    }


    //Metodos pra contar horas e minutos.
    public void contarHoras(){

        int limitar;

        this.horas.contar();

        limitar = horas.getContar();

        limitar = ( limitar % 25 );

        this.horas.setContar(limitar); 
    }

    public void contarMinutos(){

        int limitar;

        this.minutos.contar();

        limitar = minutos.getContar();

        limitar = ( limitar % 61 );

        this.minutos.setContar(limitar);
    }


    //Metodos get e set dos campos da classe:

    public void setHoras(int hs){

        this.horas.setContar( hs );

    }//fim do SetHoras

    public int getHoras(){

        return this.horas.getContar();
    }//fim do getHoras

    public void setMinuto(int m){

        this.minutos.setContar( m );

    }//fim do SetMinuto

    public int getMinutos(){

        return this.minutos.getContar();
    }//fim do getMinutos


    //Metodos para printar em formato 12 hs ou em formato 24 hs

    public void metodo12hs(){

        int contador = 0;

        while(contador < 24){

            if(contador <= 12){

                System.out.print(getHoras() + ":" + getMinutos() + "a.m");

            }//end of if(contador <= 12)

            else{

                setHoras(1);//reiniciar o contador
                System.out.print(getHoras() + ":" + getMinutos() + "p.m");

            }//end of else

            contador++;
            contarHoras();
            contarMinutos();

        }//end of while(contador < 24)
    }

    public void metodo24hs(){

        //metodo a ser implementado, quero fazer o metodo12hs funcionar primeiro 


    }



}//fim da Classe Relogio

Test class clock

import java.util.Scanner;

public class TesteRelogio {

    public static void main(String[] args){

        Scanner input = new Scanner(System.in);
        Relogio rel = new Relogio();

        int op;



        while(true){

            System.out.print("Formato de horas: ");
            op = input.nextInt();

            switch(op){

                case 12: rel.metodo12hs(); break;

                case 24: rel.metodo24hs(); break;               


            }//end of switch(op)


        }//end of while(true)



      //rel.metodo12hs();

    }//end of main method


}//end of class

When compiling, after choosing the format 12 hs I get the following error message:

 Exception in thread "main" java.lang.NullPointerException
  at Relogio.getHoras(Relogio.java:55)
  at Relogio.metodo12hs(Relogio.java:80)
  at TesteRelogio.main(TesteRelogio.java:22)
/home/thiago/.cache/netbeans/8.1/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 3 seconds)

I have tried to make changes and initialization of variables in the methods that appear in the error message but until now I have not found the problem.

    
asked by anonymous 01.04.2018 / 18:08

1 answer

2

Your error is here:

public void Relogio(){

    this.horas = new Contador();
    this.minutos = new Contador();

}

Constructors have no return specification because they do not return anything. When putting a return, it stops being a constructor and turns any method, although it has the same name of the class.

Since the method is never called, the variables inside it are never started. Remove this void from the signature so that the method works as a constructor, and the code will run without crashing this nullpointer.

Recommended reading:

01.04.2018 / 18:34