Return null when using variable of type Double

0

I'm having difficulties in some methods (when it will have to return or when it is void, parameters or not etc). Why does the last two Employee methods return 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();


    }

}
    
asked by anonymous 20.08.2018 / 12:51

1 answer

0

Double and double are different things, hence perhaps to 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 the String, for example). If by chance, however, it is double , that is, a primitive type, the initialization happens automatically with the value 0.0.

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

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

    
20.08.2018 / 13:11