user3004159
user3004159

Reputation: 45

NullPointerException when NON-null actual values has been checked right before calling method

public class InvoiceApp 
{

public static void main(String[] args) 
{
    //create instances
            Client c = new Client(...);    //some client value
            Vendor v = new Vendor(...);    //some vendor value

    Project p = new Project(...);  //some project value

            //passing in some values for invoice, with p being the project created
    Invoice i = new Invoice(105, p, Calendar.getInstance(), true);


}

}

For the Invoice class,

    private int id;
    private Project p;
    private Calendar iDate;
    private boolean vatApp; //is vat payable on this invoice?
    private Costing c;

public Invoice(int id, Project p, Calendar iDate, boolean vatApp) {

    this.id = id;
    this.p= p;
    this.iDate = iDate;
    this.vatApp= vatApp;


            c.calculateTotal(p, vatApp); 

}

& lastly, the Costing class

private Project p;

public void calculateTotal(Project p, boolean vatA) 
{
System.out.print("HELLO 2");
//actual computation formula
}

The NullException refers to c.calculateTotal(p, vatApp); line. Any idea why is this happening, even though I already checked using System.out.println to ensure that those value right before calling the method, DOES have the right value?

Upvotes: 0

Views: 84

Answers (2)

muthukumar
muthukumar

Reputation: 2243

private Costing c;

c.calculateTotal(p, vatApp);

Not Initialized to calculteTotal method is called

Upvotes: 2

Rahul
Rahul

Reputation: 45040

The Costing instance c is still uninitialized(it is null by default). That's why you get the NullPointerException, when you try to invoke a method on it.

private Costing c = new Costing(...); // You need to initialize this also.

Upvotes: 5

Related Questions