1

我已经编写了一些代码,它应该允许我获取数据库中列下的所有数据并将其存储在字符串数组中。我将包含信息的字符串数组转换为双精度数组。将双精度数组中的所有值加在一起以获得一个总值,然后该值将显示在 EditText 中。但是,每当我开始活动时,就会出现一个错误,提示 java.lang.NullPointerException。

这是数组的代码:

DatabaseTotalEntries getAllData = new DatabaseTotalEntries(this); //Creates a new method in the database class
    getAllData.open();
    String[] RunningCosts = getAllData.getCost(); //Transfers all the data under the Total Running Costs column into a String Array
    String[] RunningTimes = getAllData.getTime();
    String[] EnergyUsages = getAllData.getEnergy();
    getAllData.close();

    double[] doubleRunningCosts = new double[RunningCosts.length];
    for(int i=0; i<RunningCosts.length; i++)
    {
       doubleRunningCosts[i] = Double.parseDouble(RunningCosts[i]);
    } // Converts the string array 'RunningCosts' into a double array

    double TotalCost = 0;
    for (int in = 0; in <doubleRunningCosts.length; in++)
        TotalCost += doubleRunningCosts[in]; //Adds the values in the double string array together to get one total value

    DecimalFormat decf = new DecimalFormat("##");
    totalCostBox.setText(decf.format(TotalCost)); //Sets the variable 'TotalCost' to appear in the totalCostBox edit text

LogCat 说事件发生在第 31 行,即

double[] doubleRunningCosts = new double[RunningCosts.length];

这是我在数据库中使用的 getData 方法:

public String[] getCost() {
    // TODO Auto-generated method stub
    ArrayList<String> costarraylist = new ArrayList<String>();
    String[] applianceCostcolumns = new String[] { KEY_TOTAL_COST };
    Cursor costcursor = allDatabase.query(DATABASE_TABLE, applianceCostcolumns, null, null, null, null, null);
    String result;

    int iApplianceCost = costcursor.getColumnIndex(KEY_TOTAL_COST);

    for (costcursor.moveToFirst(); !costcursor.isAfterLast(); costcursor
            .moveToNext()) {

        result = costcursor.getString(iApplianceCost);

        costarraylist.add(result);
    };
    return null;
}

有谁知道可能是什么问题?谢谢

4

3 回答 3

3

改变

return null;

return costarraylist.toArray(new String[costarraylist.size()]);

在你getCost()方法的最后一行。

于 2012-04-29T23:21:00.133 回答
1

成员函数public String[] getCost()返回 null 而不是实际成本值。因此,double[] doubleRunningCosts = new double[RunningCosts.length];第 31 行将失败,因为 RunningCosts 为空。

于 2012-04-29T23:19:19.583 回答
1

您的getCost方法null在应该返回类似的东西时返回costarraylist.toArray(new String[0])

于 2012-04-29T23:21:25.680 回答