JAVA, keyboard input, bufferedReder

0

Very good, I could be given a hand with this error, I need to be able to parse the input (whole) from the keyboard and pass it to an object

/**Clase Main: **/


public class app {


    public static void main(String[] args) {

        Persona per =new Persona();
        Fecha fecha =new Fecha();

    PersonaConNacimiento persona = new PersonaConNacimiento();
    persona.inputPersonaConNacimiento(per,fecha);
    //persona.showPersonaConNacimiento();
    }

}

Class PersonaConNacimiento :

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PersonaConNacimiento extends Persona {

    Fecha nacimiento;

    public PersonaConNacimiento() {
        super();
    }

    public void inputPersonaConNacimiento(Persona persona, Fecha fecha) {

        BufferedReader dato = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Ingrese Nombre");

        try {
            this.setNombre(dato.readLine());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }

        System.out.println("Ingrese Apellido");

        try {
            this.setApellido(dato.readLine());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }

        System.out.println("Ingrese dni");

        try {
            this.setDni(Integer.parseInt(dato.readLine()));
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }       
    /***************************HELP*****************************************/  
        System.out.println("Ingrese Fecha- : día, formato dd");

        try {

            this.nacimiento.setDia(Integer.parseInt(dato.readLine()));  

        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
        System.out.println("Ingrese mes, formato mm");
        try {
            this.nacimiento.setMes(((Integer.parseInt(dato.readLine()))));
        } catch (IOException e) {
            System.out.println(e.getMessage());

        }
        System.out.println("Ingrese año, formato aaaa");
        try {
            this.nacimiento.setAnho((Integer.parseInt(dato.readLine())));
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }       
        /*************************FIN HELP *******************************************/
    }

    public void showPersonaConNacimiento() {
        System.out.println("la fecha es: " + this.nacimiento.getDia() + "/" 
                                           + this.nacimiento.getMes() + "/" 
                                           + this.nacimiento.getAnho());

        System.out.println("Nombre: " + this.getNombre() + "\n"
                        + " Apellido: " + this.getApellido() + "\n"
                        + "Dni: " + this.getDni());
    }

    @Override
    public String toString() {

        return "Trabaj práctico 1 enunciado 2";
    }

    public Fecha getNacimiento() {
        return nacimiento;
    }

    public void setNacimiento(Fecha nacimiento) {
        this.nacimiento = nacimiento;
    }

}

This is the output with the error.

Ingrese Nombre

name Enter Surname surname Enter ID 2. 3. 4. 5 Enter Date-: day, dd format 2. 3 Exception in thread "main" java.lang.NullPointerException     at PersonConNacimiento.inputPersononaConNacimiento (PersonaConNacimiento.java:45)     at app.main (app.java:11)

    
asked by Dent007 30.03.2018 в 18:37
source

2 answers

0

The problem that this error generates:

  

Exception in thread "main" java.lang.NullPointerException at   clasedemo.PersonaConNacimiento.inputPersonaConNacimiento ()

is because you try to call a method on an instance with null value, in this case, nacimiento has a null value since you have not initialized it:

this.nacimiento.setDia(Integer.parseInt(dato.readLine()));

initialize and in this way you can add the values of the date without problems:

...
...
System.out.println("Ingrese Fecha- : día, formato dd");

    try {

        nacimiento = new Fecha(); //*** Crea una instancia de la clase Fecha()

        this.nacimiento.setDia(Integer.parseInt(dato.readLine()));  

    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
 ...
 ...
    
answered by 30.03.2018 в 18:57
-1

SOLVED:

Date of birth;

I was using this statement "Birth date" without having instantiated an object of it. Problem solved !!!

Thanks

    
answered by 30.03.2018 в 19:53