空点异常SQLite数据库

时间:2015-04-11 14:48:56

标签: sqlite android-studio android-sqlite

您好我正在创建一个包含三个表的数据库,以便用户管理他们的卡路里。当他们在那里注册时,BMI和BMR被计算并显​​示并保存在用户表中。如果用户将新产品添加到产品表中,则将产品的卡路里与用户ID和日期一起输入卡路里表。我的问题是当我尝试在主页上显示当天剩余的卡路里时。 我正在接受这行代码的零点异常。

CalLeftover = (Double.parseDouble(String.valueOf(users.get_BMR()))- cal.get_calories());

用户表:

//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
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表:

//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";

我有一个INNER加入用户和Calorie表的查询,以便用户calorieintake与他们的个人资料相关联,并且可以在他们登录时查看。

查询:

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

主页应显示用户名,BMI,BMR,然后显示当天剩余的卡路里,但我得到Caloriesleftover声明的Null点例外。 主页:

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


}

我想知道我的查询结构错了吗?我坚持这个错误,我不知道是什么导致它。是我的getallIntakes查询没有返回get_calories的值吗?任何帮助都会很棒,因为我特别坚持这个错误。

0 个答案:

没有答案