尝试从SQLite数据库中获取SUM的NullPointerException

时间:2014-07-07 18:50:58

标签: android nullpointerexception android-sqlite

从我的片段中,我正在尝试检索并更新数据库中列的SUM。

我正在运行updateDollar(); 片段:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainFragment extends Fragment {

    int mCurrentPage;
    int tableCount;
    DatabaseHelper db = new DatabaseHelper(getActivity());
    Button btDeposit;
    Button btWithdraw;
    TextView tvBalanceDollar;
    TextView tvBalanceCent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /** Getting the arguments to the Bundle object */
        Bundle data = getArguments();

        /** Getting integer data of the key current_page from the bundle */
        mCurrentPage = data.getInt("current_page", 0);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_account, container,false);
        tvBalanceDollar = (TextView) v.findViewById(R.id.tvBalanceDollar);

        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        db = new DatabaseHelper(getActivity());
        ArrayList<String> a = db.listTables();
        tableCount = a.size();
        Toast.makeText(getActivity(), a + "" + tableCount, Toast.LENGTH_SHORT).show();
        db.close();
    }

    public void updateDollar(){
        db = new DatabaseHelper(getActivity());
        int balance = db.getSum();
        //tvBalanceDollar.setText(Integer.toString(balance));
        Log.d("sum", "" + balance);
        db.close();

    }

在我的databasehelper中运行以下内容:

// GET SUM
    public int getSum(){
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.rawQuery("SELECT SUM (" + KEY_CREDIT + ") FROM " + TABLE_DEFAULT_ACCOUNT, null);
        if(c.moveToFirst()) {
            sum = c.getInt(0);
        }
        return sum;
    }

以下是我得到的以下错误:

07-07 14:49:56.104: E/AndroidRuntime(18313): FATAL EXCEPTION: main
07-07 14:49:56.104: E/AndroidRuntime(18313): java.lang.NullPointerException
07-07 14:49:56.104: E/AndroidRuntime(18313):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
07-07 14:49:56.104: E/AndroidRuntime(18313):    at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:231)
07-07 14:49:56.104: E/AndroidRuntime(18313):    at com.rcd.simpleregister.DatabaseHelper.getSum(DatabaseHelper.java:89)
07-07 14:49:56.104: E/AndroidRuntime(18313):    at com.rcd.simpleregister.MainFragment.updateDollar(MainFragment.java:57)
07-07 14:49:56.104: E/AndroidRuntime(18313):    at com.rcd.simpleregister.MainActivity$2$1.onClick(MainActivity.java:114)
07-07 14:49:56.104: E/AndroidRuntime(18313):    at android.view.View.performClick(View.java:3511)
07-07 14:49:56.104: E/AndroidRuntime(18313):    at android.view.View$PerformClick.run(View.java:14105)
07-07 14:49:56.104: E/AndroidRuntime(18313):    at android.os.Handler.handleCallback(Handler.java:605)
07-07 14:49:56.104: E/AndroidRuntime(18313):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-07 14:49:56.104: E/AndroidRuntime(18313):    at android.os.Looper.loop(Looper.java:137)
07-07 14:49:56.104: E/AndroidRuntime(18313):    at android.app.ActivityThread.main(ActivityThread.java:4424)
07-07 14:49:56.104: E/AndroidRuntime(18313):    at java.lang.reflect.Method.invokeNative(Native Method)
07-07 14:49:56.104: E/AndroidRuntime(18313):    at java.lang.reflect.Method.invoke(Method.java:511)
07-07 14:49:56.104: E/AndroidRuntime(18313):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-07 14:49:56.104: E/AndroidRuntime(18313):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-07 14:49:56.104: E/AndroidRuntime(18313):    at dalvik.system.NativeStart.main(Native Method)

有人看到可能导致这种情况的原因吗?

0 个答案:

没有答案