1

データベースの列の下にあるすべてのデータをフェッチして文字列配列に格納できるようにするコードを作成しました。情報を含む文字列配列をdouble配列に変換します。二重配列のすべての値が合計されて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];RunningCostsがnullであるため、31行目は失敗します。

于 2012-04-29T23:19:19.583 に答える
1

あなたのgetCostメソッドはnull、次のようなものを返す必要があるときに戻ってきますcostarraylist.toArray(new String[0])

于 2012-04-29T23:21:25.680 に答える