Hi I'm making a database with three tables to allow the user to manage their calories. When they register there BMI and BMR are caluclated and displayed and saved in the user table. If the user adds a new product to the product table the Calories of the product are entered along with the user id and the date into the calorie table. My problem is when I try to display the calories left for the day in the homepage. I am reccieving a null point Exception for this line of code.
CalLeftover = (Double.parseDouble(String.valueOf(users.get_BMR()))- cal.get_calories());
User Table:
//USER TABLE
private static final String TABLE_USERS = "Users_Table";
private static final String KEY_U_ID = "U_ID";
private static final String KEY_USERNAME = "Username";
private static final String KEY_PASSWORD = "Password";
private static final String KEY_AGE = "Age";
private static final String KEY_EMAIL = "Email";
private static final String KEY_GENDER = "Gender";
private static final String KEY_HEIGHT = "Height";
private static final String KEY_WEIGHT = "Weight";
private static final String KEY_BMI = "BMI";
private static final String KEY_BMR = "BMR";
Product Table:
//PRODUCT TABLE
private static final String TABLE_PRODUCTS = "Products_Table";
private static final String KEY_PRODUCT_ID = "Product_ID";
private static final String KEY_ITEMNAME = "Item_name";
private static final String KEY_ITEMCALORIES = "Item_Calories";
CalorieIntake Table:
//CALORIEINTAKE TABLE
private static final String TABLE_CALORIE_INTAKE = "CalorieIntake_Table";
private static final String KEY_USER_ID = "USER_ID";
private static final String KEY_INTAKE_DATE = "Intake_Date";
private static final String KEY_INTAKE_DAY = "Intake_Day";
private static final String KEY_CALORIES = "Calorie";
I have a query which INNER JOINS the user and Calorie table so that the users calorieintake is linked to their profile and can be viewed while they are signed in.
Query:
public CalorieIntake getuserIntake(int id, String currentdate) {
CalorieIntake calorieIntake = null;
SQLiteDatabase db = getReadableDatabase();
final String UserintQuery = "SELECT " + KEY_CALORIES + " FROM "
+ TABLE_CALORIE_INTAKE + " c " + " INNER JOIN " + TABLE_USERS + " u " +
"ON" + " c." + KEY_USER_ID + " = u." + KEY_U_ID + " WHERE u." + KEY_U_ID + " =? AND " + KEY_INTAKE_DATE + " =?";
Cursor cursor;
cursor = db.rawQuery(UserintQuery, new String[]{String.valueOf(id), currentdate});
if (cursor.getCount() < 0) {
cursor.moveToFirst();
assert cursor != null;
calorieIntake = new CalorieIntake(cursor.getInt(0),
cursor.getString(1),
cursor.getString(2),
cursor.getDouble(3));
}
cursor.close();
return calorieIntake;
}
The Homepage is supposed to display the user name, BMI, BMR and then the calories left for the day however I get a the Null point Exception for the Caloriesleftover declaration. HomePage:
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Home_Page extends ActionBarActivity {
DatabaseHandler db;
TextView Welcometxt, BMItxt, Caloriestxt, Calorieslefttxt ;
Globals g = Globals.getInstance();
int id;
String currentday;
Date currentdate;
double CalLeftover;
Button addProduct, addCalorie;
@Override
protected void onCreate(Bundle savedInstanceState) {
db = new DatabaseHandler(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home__page);
addCalorie = (Button) findViewById(R.id.addCalbtn);
addProduct = (Button) findViewById(R.id.addProductBtn);
Log.d("Products:", "Product Count" + db.getallProducts().size());
addProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(), AddProduct.class));
}
});
Welcometxt = (TextView) findViewById(R.id.Welcometxt);
BMItxt = (TextView) findViewById(R.id.BMRtxt);
Caloriestxt = (TextView) findViewById(R.id.Calorietxt);
Calorieslefttxt = (TextView) findViewById(R.id.Callefttxt);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
Date d = new Date();
String dayOfTheWeek = sdf.format(d);
id = g.getID();
CalorieIntake cal = db.getuserIntake(id, dayOfTheWeek);
Users users = db.getUser(id);
CalLeftover = (Double.parseDouble(String.valueOf(users.get_BMR()))- cal.get_calories());
Welcometxt.setText("Welcome " + users.get_username());
BMItxt.setText("Your BMI is: " + users.get_BMI());
Caloriestxt.setText("Your Daily Calories is: " + users.get_BMR());
Calorieslefttxt.setText("Calories Left: " + CalLeftover);
}
I'm wondering have I structured my query wrong? I'm stuck on this error and I don't know what is causing it. Is it that my Query for the getallIntakes is not returning a value for get_calories? Any help would be fantastic as I really am stuck on this error in particular.