i ma trying to write a program where about the payments of employees and other staaff, I have an abstract superclass called Employee, and then the following FullTimeEmployee,PartTimeEmployee ,Orders and Salesman , are sub classes of Employee.
I have only one problem with null pointers in Salesman, where i try to calculate the bonus they will get for each sale.
here is my code in Salesman class
public Salesman(String firstname, String lastname,int code, String address, String city,int tk,int phone,String email,int deptcode,int card,double hours,String cat,int orderno,double salary,Orders[] ord/*,double sales*/){
super( firstname, lastname, code, address, city, tk, phone, email, deptcode, card, hours, cat );
this.orderno=orderno;
setBaseSalary( salary );
//setGrossSales( sales );
Orders[] order= ord.clone();//new Orders[orderno];//create the array for the orders made by this salesman
//order=ord;
setSallary(order );
}
public void setSallary( Orders[] order ){
//double sum=0;
for(int i=0;i<=order.length;) {
grossSales+=order[i++].getamount(); //get the value of the sales done
System.out.println(grossSales);
}
if (grossSales<10000 ){
baseSalary+=baseSalary*0.05;
}
else if((grossSales>=10000)||(grossSales<=20000)){
baseSalary+=baseSalary*0.07;
}
else
baseSalary+=baseSalary*0.1;
}
in main i have
Orders[] array=new Orders[20];
array[0]=new Orders(1,"14/5/2010","agora aftolinitwn",2000.0);
array[1]=new Orders(2,"14/5/2010","agora aftolinitwn",20000.0);
Salesman sales1 =new Salesman("giannis", "antoniou",35, "vavilonos 7", "leffkosia",11475,69486931,"gäntoniou@hotmail.com",100,40,160.0,"salesman",2,1300.0,array);
Salesman sales2 =new Salesman("andreas", "antoniou",35, "vavilonos 7", "lefkosia",11475,69486931,"äntonioua@hotmail.com",100,41,160.0,"salesman",1,1200.0,array);
basically what i do is creating an array with the salsmans info( amount earned from sale etc) and then sending it in to Saleman Constructor. i then copy the conents of tha array to another array an try to calulate the bonus. but i get
Exception in thread “main” java.lang.NullPointerException
at misthodosia.Salesman.setSallary(Salesman.java:36)
at misthodosia.Salesman.<init>(Salesman.java:28)
at misthodosia.Misthodosia.main(Misthodosia.java:30)
Java Result: 1
heres the Orders class as well
public class Orders {
private int orderNo;
private String orderDate;
private String description;
private double orderAmount;
private Salesman man;
public Orders(int no,String date,String descrip,double amount/*,Salesman man*/){
orderNo=no;
orderDate=date;
description=descrip;
orderAmount=amount;
//this.man=man;
//Orders[] orders=new Orders[orderNo];//create the array for the orders made by this salesman
}
public double getamount(){
return orderAmount;
}
}
Can you help me ?? I know i am doing something wrong with the object array but i cant figure it out :S
It is on this line:
grossSales+=order[i++].getamount();
The order array is allocated to a size of 20, but only the first 2 are initialized, therefore when it accesses order[2].getamount() that value is null, hence the null pointer.
I strongly recommend using an ArrayList for the list of orders, you don’t have to preallocate a number of orders.