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