Nullpointerexception error in running my program

Asked

Viewed 138 times

1

I am doing an activity with the following statement:

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

24 hour format

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

Format 12 hours

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 must be an option to display the hours in 12 hours or 24 hours format. If the selection is made to display in 12-hour format, it must be displayed a.m. or p.m. as appropriate.

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

watch class

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 12 hs format I have 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 initializing variables in the methods that appear in the error message but so far I have not found the problem.

1 answer

3


Your mistake is here:

public void Relogio(){

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

}

Constructors have no return specification as they return nothing. When placing a return, it ceases to be a constructor and becomes any method, although it has the same class name.

As the method is never called, the variables within it are never started. Remove this void signature for the method to work as constructor, and the code will run without popping this nullpointer.

Recommended reading:

  • Thank you very much. That was the problem. The recommended reading was very helpful too.

Browser other questions tagged

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