Error Exception in thread "main" java.lang.NullPointerException in class and main [duplicate]

0

I get this error, please help, I will leave the class where the error comes

Exception in thread "main" java.lang.NullPointerException
    at Afiliado.anioAfiliado(Afiliado.java:106)
    at Afiliado.ValorFecha(Afiliado.java:93)
    at Afiliado.calculoInteres(Afiliado.java:26)
    at ProyectoFinal.prestamoPagoAfiliado(ProyectoFinal.java:384)
    at ProyectoFinal.main(ProyectoFinal.java:160)



public class Afiliado extends Persona implements Interes{ 

    private Afiliado [] beneficiario;
    private Fecha fechaAfiliacion;
    private double interes;
    private final int MAXBENEFICIARIO=3;
    private final int MAXPRESTAMOS=5;
    private Prestamo [] prestamos;
    private char tipoAfiliado;


    public Afiliado() {

    }

    public Afiliado(Fecha fechaAfiliacion, char tipoAfiliacion) {

    }

    public void calculoInteres() {


        if(getTipoAfiliado()=='c' || getTipoAfiliado()=='C') {

            interes=1.5-(0.5-ValorFecha());//aca sale error

        } 
        if(getTipoAfiliado()=='b' || getTipoAfiliado()=='B') {

            interes=1.5-(0.2-ValorFecha());

        } 

    }

    public Afiliado[] getBeneficiario() {
        return beneficiario;
    }

    public Fecha getFechaAfiliacion() {
        return fechaAfiliacion;
    }

    public double getInteres() {
        return interes;
    }


    public int getMAXBENEFICIARIO() {
        return MAXBENEFICIARIO;
    }

    public int getMAXPRESTAMOS() {
        return MAXPRESTAMOS;
    }

    public Prestamo[] getPrestamos() {
        return prestamos;
    }

    public char getTipoAfiliado() {

        return tipoAfiliado;
    }

    public void setBeneficiario(Afiliado[] beneficiario) {
        this.beneficiario = beneficiario;
    }

    public void setFechaAfiliacion(Fecha fechaAfiliacion) {
        this.fechaAfiliacion = fechaAfiliacion;
    }

    public void setInteres(double interes) {
        this.interes = interes;
    }

    public void setPrestamos(Prestamo[] prestamos) {
        this.prestamos = prestamos;
    }

    public void setTipoAfiliado(char tipoAfiliado) {
        this.tipoAfiliado = tipoAfiliado;
    }



    public double ValorFecha() {
        double valorFecha;


        valorFecha=anioAfiliado()*0.02; //aca sale error

        return valorFecha;
    }

    public  int anioAfiliado() {
        int anioAfiliado;
        int sw=0;



        Prestamo pres=new Prestamo();

        anioAfiliado=pres.getFechaPrestamo().getAnio()-getFechaAfiliacion().getAnio(); //aca sale error

        if(getFechaAfiliacion().getMes()<pres.getFechaPrestamo().getMes()) {
            if(getFechaAfiliacion().getDia()<pres.getFechaPrestamo().getDia()) {
                sw=1;
            }
        }

        if(sw==1) {
            return anioAfiliado;

        }else {
            return anioAfiliado=anioAfiliado-1;
        }


    }

}

The line where errors are commented as "here goes error"

The last two errors that come out are in the class that contains the main and it is also related to that class.

    
asked by MZ39720 23.06.2018 в 00:06
source

1 answer

0

With respect to the null pointer you can do the following, I'll explain it to you in the order how your methods are executed when you call the calculoInteres method.

First what you have to do when instantiating a Loan object in the anioAfiliado method, the Loan object that instances must receive the necessary arguments in its constructor in order to require them correctly in the future. So you can have your lending class.

public class Prestamo {
private LocalDate fechaPrestamo;

public Prestamo(LocalDate fechaPrestamo) {
    this.fechaPrestamo = fechaPrestamo;
}

public LocalDate getFechaPrestamo() {
    return fechaPrestamo;
}

public void setFechaPrestamo(LocalDate fechaPrestamo) {
    this.fechaPrestamo = fechaPrestamo;
}

You could also leave the Date class as well.

public class Fecha {
private LocalDate date;

public Fecha(LocalDate date) {
    this.date = date;
}

public LocalDate getDate() {
    return date;
}

public void setDate(LocalDate date) {
    this.date = date;
}

}

-The anioAfiliado method must have the parameter Date type of LocalDate, because you will need it to instantiate the Loan class.

It would stay that way.

 public  int anioAfiliado(LocalDate fechaPrestamo) {
    int anioAfiliado;
    int sw=0;

    Prestamo pres = new Prestamo(fechaPrestamo);

    anioAfiliado = pres.getFechaPrestamo().getYear() -  getFechaAfiliacion().getDate().getYear();
     //Si la fecha de afiliacion es menor a la fecha del prestamo
    if(getFechaAfiliacion().getDate().isBefore(pres.getFechaPrestamo())){
            sw=1;
    }

    if(sw == 1) {
        return anioAfiliado;

    }else {
        return anioAfiliado=anioAfiliado-1;
    }
}

When you change the anioAffiliate class, the calculus-method also requires that you enter the datePrestamos as the method's parameter. This method would be left.

public void calculoInteres(LocalDate fechaPrestamo) {


    if(getTipoAfiliado()=='c' || getTipoAfiliado()=='C') {

        interes=1.5-(0.5-ValorFecha(fechaPrestamo));

    } 
    if(getTipoAfiliado()=='b' || getTipoAfiliado()=='B') {

        interes=1.5-(0.2-ValorFecha(fechaPrestamo));

    } 

}

It only remains to run the main.

public static void main(String[] args) {
    Afiliado afiliado = new Afiliado(new Fecha(LocalDate.of(2018,1, 1)), 'c');
    afiliado.calculoInteres(LocalDate.of(2018, 8, 16));
    System.out.println("Interes: " + afiliado.getInteres());
}

I hope I have helped you, with your problem.

    
answered by 23.06.2018 в 19:13