1

so I have written the code to a program that should initialize a data set of totals from different locations and display them as such. for some reason I keep getting a run time error on the first for-loop and can't figure out why. can someone please help me figure out what I'm doing wrong?

public class Sales {

private static String[] months;
private static String[] cities;
private static int[] citySum;
private static int[] monthlySum;
private static int[][] sales;
private static int col;
private static int row;


/**
 * @param args the command line arguments
 */
public static void main(String[] args) {

    calCityTotal();
    calMonthlyTotal();
    displayTable();

}

public Sales() {

    months = new String[] {"January","Febuary","March","April",
                           "May","June"};

    cities = new String[] {"Chilliwack","Kamloops","Kelowna",
                           "NanaimoSurrey","Vancouver","Victoria"};

    sales = new int[][] {{400,500,500,600,500,600},
                        {600,800,800,800,900,900},
                        {700,700,700,900,900,1000},
                        {500,600,700,800,700,700},
                        {900,900,900,1000,1100,1100}};

    citySum = new int[sales.length];

    monthlySum = new int[sales[0].length];
}

public static void calCityTotal() {

    for (row = 0; row < sales.length; row++){
        for (col = 0; col < sales[0].length; col++){
            citySum[col] += sales[row][col];
        }
    }
}

public static void calMonthlyTotal() {

    for (row = 0; row < sales.length; row++){
        for (col = 0; col < sales[0].length; col++){
            monthlySum[row] += sales[row][col];
        }
    }
}
Jordan
  • 79
  • 2
  • 11
  • What error are you getting? Also, where is the method `displayTable()` in your code? – Nicholas K Jan 19 '19 at 06:18
  • I haven't finished writing the displayTable() method yet so I didn't include it but I am getting: run: Exception in thread "main" java.lang.NullPointerException at sales.Sales.calCityTotal(Sales.java:57) at sales.Sales.main(Sales.java:30) /Users/jordan/Library/Caches/NetBeans/8.2/executor-snippets/run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds) – Jordan Jan 19 '19 at 06:28
  • Now which is line 57? – Nicholas K Jan 19 '19 at 06:34
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Nicholas K Jan 19 '19 at 06:48

2 Answers2

0

You are getting the NullPointerException because sales is null at line for (row = 0; row < sales.length; row++).

Even though you set a value to sales variable in the constructor Sales(), you are never calling that constructor (like new Sales()).

So, to fix this NullPointerException you can call the Sales constructor in the main() method like this:

public static void main(String[] args)
{
  new Sales();

  calCityTotal();
  calMonthlyTotal();
  displayTable();
}

EDIT
After fixing the NullPointerException, your code still has few other problems.

Inside calCityTotal(), sales[0].length should be corrected as sales[row].length I think.

citySum array is initialized to the length of sales. This means citySum.length is equal to the number of "rows". But then you write citySum[col] which can lead to ArrayIndexOutOfBoundsException because number of "columns" can exceeds citySum.length. (This actually happens in your program. Because number of row = 5 and number of columns = 6.)

Prasad Karunagoda
  • 2,048
  • 2
  • 12
  • 16
0

There are 2 problems with your current code:

1) Variables are not getting initialized. For that, you can create Sales object in main method(remove static keyword from calCityTotal and calMonthlyTotal).

2) Once above problem is resolved, you will encounter Arrayindexoutofboundsexception because citySum has length of sales.length whereas your loop in calCityTotal goes to sales[0].length.

It doesn't make sense to declare variables as static and initialize them in constructor. Static variables should be independent of any instances. Go through Java: when to use static methods to know when to declare static variables. If you want to declare variables as static, you should initialize them in static block(Static Block in Java).

Below code will work:

public class Sales {

private String[] months;
private String[] cities;
private int[] citySum;
private int[] monthlySum;
private int[][] sales;
private int col;
private int row;


/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Sales salesObj = new Sales();
    salesObj.calCityTotal();
    salesObj.calMonthlyTotal();

}

public Sales() {


    months = new String[]{"January", "Febuary", "March", "April",
            "May", "June"};

    cities = new String[]{"Chilliwack", "Kamloops", "Kelowna",
            "NanaimoSurrey", "Vancouver", "Victoria"};

    sales = new int[][]{{400, 500, 500, 600, 500, 600},
            {600, 800, 800, 800, 900, 900},
            {700, 700, 700, 900, 900, 1000},
            {500, 600, 700, 800, 700, 700},
            {900, 900, 900, 1000, 1100, 1100}};

    citySum = new int[sales.length+1];

    monthlySum = new int[sales[0].length];
}

public void calCityTotal() {

    for (row = 0; row < sales.length; row++) {
        for (col = 0; col < sales[0].length; col++) {
            citySum[col] += sales[row][col];
        }
    }
}

public void calMonthlyTotal() {

    for (row = 0; row < sales.length; row++) {
        for (col = 0; col < sales[0].length; col++) {
            monthlySum[row] += sales[row][col];
        }
    }
}

}

mukesh210
  • 2,792
  • 2
  • 19
  • 41
  • thank you that helped a lot! do you have any advice on how I would print this with the cities on the left data in the middle city totals on the right and monthly totals underneath? – Jordan Jan 19 '19 at 07:24
  • You can use for loop to print it. – mukesh210 Jan 19 '19 at 07:33