Hi, I seem to be having a consistent error when I try to add the values of an array I created to a String. I keep getting a NullPointerException Error. I went through and checked to make sure that all the values in the array that I am trying to print out are not null. The two arrays are salespersonTotal and productTotal. I don't know what I am doing wrong. Here is my class and tester. Please help if you can. I am pretty new to JAVA and could use all the help I can get.
Class:
public class Sales { public Sales() { } public Sales(double[][] sales) { this.sales=sales; } public void salesSlip(int salespersonNumber, int productNumber, double sum) { sales [salespersonNumber-1][productNumber-1]+=sum; } public void productTotal() { int cols=0; double sum=0; double[] productTotal= new double[sales[0].length]; for(cols=0; cols<sales[0].length; cols++) { for(int rows=0; rows<sales.length; rows++) { sum+= sales[rows][cols]; } productTotal[cols]=sum; } } public void salespersonTotal() { int rows=0; double sum=0; double[] salespersonTotal= new double[sales.length]; for(rows=0; rows<sales.length; rows++) { for(int cols=0; cols<sales[0].length; cols++) { sum+= sales[rows][cols]; } salespersonTotal[rows]= sum; } System.out.println(salespersonTotal[0]); System.out.println(salespersonTotal[1]); System.out.println(salespersonTotal[2]); } public void calcTotal() { double total=0; for(int i=0; i<5; i++) total+= 0; } public String toString() { toString="\t\tProduct 1\tProduct 2\tProduct 3\tProduct 4\tProduct 5\tTOTAL\nSalesperson 1"; for(int i=0; i<sales[0].length; i++) toString+="\t"+sales[0][i]+"0"; toString+="\t"+salespersonTotal[0]; toString+="\nSalesperson 2"; for(int i=0; i<sales[0].length; i++) toString+="\t"+sales[1][i]; toString+="\t"+salespersonTotal[1]; toString+="\nSalesperson 3"; for(int i=0; i<sales[0].length; i++) toString+="\t"+sales[2][i]; toString+="\t"+salespersonTotal[2]; toString+="\nTOTAL\t"; for(int i=0; i<productTotal.length; i++) toString+= "\t"+productTotal[i]; toString+="\t"+total+"0"; return toString; } private double[][] sales; private double[] productTotal; private double[] salespersonTotal; private String toString; private double total; }
Tester:
public class SalesTester { public static void main(String[] args) { double[][] sales={{0.00,0.00,0.00,0.00,0.00},{0.00,0.00,0.00,0.00,0.00},{0.00,0.00,0.00,0.00,0.00}} ; Sales tester= new Sales(sales); tester.salesSlip(1,1,20.00); tester.salesSlip(1,2,30.00); tester.salesSlip(1,3,0.00); tester.salesSlip(1,4,10.00); tester.salesSlip(1,5,0.00); tester.salesSlip(2,1,0.00); tester.salesSlip(2,2,0.00); tester.salesSlip(2,3,10.00); tester.salesSlip(2,4,0.00); tester.salesSlip(2,5,30.00); tester.salesSlip(3,1,50.00); tester.salesSlip(3,2,60.00); tester.salesSlip(3,3,50.00); tester.salesSlip(3,4,30.00); tester.salesSlip(3,5,35.00); tester.productTotal(); tester.salespersonTotal(); tester.calcTotal(); System.out.println(tester.toString()); } }
This is my output and error:
60.0
100.0
325.0
java.lang.NullPointerException
at Sales.toString(Sales.java:62)
at SalesTester.main(SalesTester.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Native MethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(De legatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:271)
>
The three numbers are the values in the array. Can you all take a look.