1

I have written some code which should allow me to fetch all the data under a column in a database and store it in a string array. I the convert the string array containing the information into a double array. All of the values in the double array are there added together to get one total value and this value is then displayed in the EditText. However whenever I start the activity an error appears saying java.lang.NullPointerException.

This is code for the arrays:

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 says the incident occurs at line 31, which is

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

This is the getData method I am using in my database:

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

Does anyone know what could be the problem? Thanks

2
  • 2
    I would suggest stepping through this in the debugger. Commented Apr 29, 2012 at 23:18
  • Since you have the line throwing the NullPointer just look at the line and try to understand which part of that line could possibly be assuming an existing object (large hint - calling a method on an object assumes the object is there and will throw a null pointer if it is not). Commented Apr 29, 2012 at 23:21

3 Answers 3

3

Change

return null;

to

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

on the last line of your getCost() method.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahh yes it seems very simple now, thanks everyone for your help and all of you were spot om
1

Your getCost method is returning null when it should be returning something like costarraylist.toArray(new String[0])

Comments

1

The member function public String[] getCost() returns null and not an actual cost value. Therefore, double[] doubleRunningCosts = new double[RunningCosts.length]; at line 31 will fail, because RunningCosts is null.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.