r/javahelp • • Aug 23 '22

I get "java.lang.NullPointerException" on my program and I don´t know why

Hi

My program compiles and it works almost perfectly. The only error I get comes when I try to enter a number (double) in this field.

if(indice>= 0)
{
                                            
 System.out.println("============================================");
     System.out.println("Presupuesto:");
     presupuesto = sc.nextDouble();
                                            
     if(presupuesto > 0)
     {
                                                        
      contable.setPresupuestoProyecto(presupuesto);
     }else
     {
          System.out.println("Por favor asigne un presupuesto válido");
          exit = true;
     }
     exit = false;
}

For context, I scan a double type nomber to set a price for a project. The method "contable.setPresupuestoProyecto(presupuesto)" is already defined in class "Contable" and all.

public void setPresupuestoProyecto(double presupuesto)
    {
        this.proyecto.setPresupuesto(this.presupuesto);
        presupuestoAsignado = true;
        this.proyecto.setPresupuestoAsignado(presupuestoAsignado);
    }

this would be the method. I get "java.lang.NullPointerException" in console every time I try to enter a price on this field. What am I missing?

0 Upvotes

5 comments sorted by

6

u/NautiHooker Software Engineer Aug 23 '22

Please post the entire exception and the entire code.

With this snippet we have no idea whats initialized and what is not.

3

u/erikorenegade1 Aug 23 '22

wild guess OP but I think this is the offending line:

    this.proyecto.setPresupuesto(this.presupuesto);

in this line you're using this.presupuesto instead of the one passed as an argument.

0

u/xRageNugget Aug 23 '22

Wild guess: proyecto is not initialized. Also, you seem fluent enough in english, make yourself and everyones life easier and write english code!