Null return when using Double type variable

Asked

Viewed 268 times

0

I am having difficulties in some methods (when you will have to return or when you are void ,parameters or not etc). Why the last two methods of Employee is returning null?

package employee;


public class Employee {
   private  String firstName,lastName;
   private  Double salarioMensal,anual,aumentoAnual;


public Employee(String nome,String sobrenome,double salario){

    firstName = nome;
    lastName = sobrenome;
    salarioMensal = salario;


}
public void setFirstName(String nome){

    firstName = nome;
}
 public void setlastName(String sobrenome){

     lastName = sobrenome;
 }
 public void setSalarioMensal(double salario){

     salarioMensal = salario;
 }
 public String getFirstName(){

     return firstName;
 }
 public String getLastName(){

     return lastName;
 }
 public Double getSalarioMensal(){

     return salarioMensal;

     }

 public void boasVindas(){

    System.out.printf("\n Seja bem vindo Sr(a) %s %s \n",getFirstName(),getLastName());
}
 public void setAnual(double salario){
    salarioMensal = salario;
     anual = salarioMensal*12;

 }
 public void setAmentoAnual(double anualtotal){
     anualtotal = anual;
     aumentoAnual = anualtotal*0.10;
 }
 public Double getAnual(){

     return anual;
 }
 public Double getAumentoAnual(){

     return aumentoAnual;
 }
 public void displayFinish1(){

     System.out.printf("\n Sr(a). %s .\nO seu salário acumulado em 12 meses será:%.2f.\n",getFirstName(),getAnual());
     System.out.printf("Após 12 meses terá um aumento de 10 por cento,ficando:\n %.2f \n.",getAumentoAnual());

 }

}

/***O TESTE****/

package employee;

import java.util.Scanner;

public class EmployeeTest {



    public static void main(String [] args){

    String nome,nome2 , sobrenome,sobrenome2;
    Double salario ,salario2;

Scanner entrada = new Scanner(System.in);

System.out.printf("\n Entre com o primeiro nome\n");
nome = entrada.nextLine();
System.out.printf("Entre com o Sobrenome\n");
sobrenome = entrada.nextLine();
System.out.printf("Entre com o salário mensal\n");
salario = entrada.nextDouble();

Employee employee1 = new Employee(nome,sobrenome,salario);

employee1.boasVindas();
employee1.displayFinish1();


    }

}
  • 1

    It would be because you never defined the variable aumentoAnual? If you do not set a value, it is always null because it is a type Double.

1 answer

0


Double and double are different things, hence perhaps your surprise. The first is an object and as such, whenever it is not explicitly initialized, it will be null by default (it is the same behavior as String, for example). If by chance, however, it was double, i.e., a primitive type, the initialization happens automatically with the value 0.0.

In this way, your methods return null simply because the variables manipulated/displayed by them were not explicitly initialized with any value, ie are null.

To avoid this behavior, you have two options: 1) initialize them or else change their type to double.

Browser other questions tagged

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