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);
}
}
对于Invoice类,
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);
}
&安培;最后,成本计算类
private Project p;
public void calculateTotal(Project p, boolean vatA)
{
System.out.print("HELLO 2");
//actual computation formula
}
NullException引用c.calculateTotal(p,vatApp);线。知道为什么会发生这种情况,即使我已经使用System.out.println检查以确保在调用方法之前这些值,DOES是否具有正确的值?
答案 0 :(得分:5)
成本核算实例c
仍为uninitialized
(默认为null
)。这就是为什么当你尝试在其上调用方法时得到NullPointerException
。
private Costing c = new Costing(...); // You need to initialize this also.
答案 1 :(得分:2)
私人成本核算c;
c.calculateTotal(p,vatApp);
未初始化为calculteTotal方法称为