取得数据集并以2D阵列打印

时间:2019-01-19 06:09:07

标签: java

因此,我已将代码编写到程序中,该程序应该初始化来自不同位置的总计数据集,并按原样显示它们。由于某种原因,我在第一个for循环中始终遇到运行时错误,无法弄清原因。有人可以帮我弄清楚我在做什么错吗?

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];
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您得到NullPointerException是因为sales在第null行中是for (row = 0; row < sales.length; row++)

即使您在构造函数sales中为Sales()变量设置了一个值,也永远不会调用该构造函数(如new Sales())。

因此,要修复此NullPointerException,您可以像这样在Sales方法中调用main()构造函数:

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

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

编辑
修复NullPointerException之后,您的代码仍然没有其他问题。

calCityTotal()内部,sales[0].length应该更正为sales[row].length

citySum数组被初始化为sales的长度。这意味着citySum.length等于“行”的数量。但是随后您写了citySum[col],它可能导致ArrayIndexOutOfBoundsException,因为“列”的数量可能超过citySum.length。 (这实际上在您的程序中发生。因为行数= 5,列数= 6)。

答案 1 :(得分:0)

您当前的代码有2个问题:

1)变量未初始化。为此,您可以使用Sales方法创建main对象(从calCityTotalcalMonthlyTotal中删除静态关键字)。

2)解决以上问题后,您将遇到Arrayindexoutofboundsexception,因为citySum的长度为sales.length,而您在calCityTotal中的循环转到sales[0].length

将变量声明为static并在constructor中对其进行初始化没有任何意义。静态变量应独立于任何实例。 通过Java: when to use static methods来了解何时声明静态变量。如果要将变量声明为static,则应在static块(Static Block in Java)中对其进行初始化。

以下代码将起作用:

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];
        }
    }
}

}