Error on Android's rawQuery()
<p>i got nullPointerException when trying to fetch some data from database with rawQuery.
Here's an error: </p>

<blockquote>
  <p>03-11 18:09:01.522: E/AndroidRuntime(2057): Uncaught handler: thread
  main exiting due to uncaught exception 03-11 18:09:01.532:
  E/AndroidRuntime(2057): java.lang.NullPointerException 03-11
  18:09:01.532: E/AndroidRuntime(2057):     at
  com.math.scan.FormulaModel.setFormulaList(FormulaModel.java:27)</p>
</blockquote>

<p>Look at my code:</p>

<p><strong>DbHelper</strong></p>

<pre><code>    package com.math.scan;

import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

import android.content.Context;
import android.content.res.AssetManager;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DbHelper extends SQLiteOpenHelper{

    public static final String TABLE_NAME = "formulas";

    public static final String COLUMN_ID = "_id";
    public static final String COLUMN_UNKNOWN = "unknown";
    public static final String COLUMN_FORMULA = "formula";
    public static final String COLUMN_CTG = "ctg";

    private static final String DB_NAME = "formulas.db";
    private static int DB_VERSION = 1;

    private static final String DB_CREATE = "CREATE TABLE "+TABLE_NAME+
                                                "("+COLUMN_ID+" integer primary key autoincrement,"
                                                +COLUMN_UNKNOWN+" text not null,"
                                                +COLUMN_FORMULA+" text not null,"
                                                +COLUMN_CTG+" text not null );";

    public Context mCtx;


    public DbHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        this.mCtx = context;
    }

    @Override
    public void onCreate(SQLiteDatabase database) {

        database.execSQL(DB_CREATE);

        // reading formulas
        AssetManager asset = mCtx.getAssets();
        try { 
            InputStream input = asset.open("formulas");

            int size = input.available();

            byte[] buffer = new byte[size];
            input.read(buffer);
            input.close();

            String content = new String(buffer);

            Scanner scan = new Scanner(content);

            String current;
            String[] f = new String[2];
            String[] formula = new String[2];

            while(scan.hasNextLine()) {
                current = scan.nextLine();
                // get category
                f = current.split("!!");
                formula = f[1].split(" = ");
                database.execSQL("INSERT INTO `formulas` VALUES (NULL, '"+formula[0]+"', '"+formula[1]+"', '"+f[0]+"');");
                Log.d("DB", "INSERT INTO `formulas` VALUES (NULL, '"+formula[0]+"', '"+formula[1]+"', '"+f[0]+"');");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(DbHelper.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}
</code></pre>

<p><strong>FormulaModel</strong></p>

<pre><code>package com.math.scan;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class FormulaModel {
    private SQLiteDatabase database;
    private DbHelper dbHelper;

    public FormulaModel(Context context) {
        dbHelper = new DbHelper(context);
    }

    public void open() throws SQLException {
        dbHelper.getWritableDatabase();
    }

    public void close() {
        dbHelper.close();
    }

    public void setFormulaList(String ctg) {
        open();
        // app stops working here
        Cursor cursor = database.rawQuery("SELECT unknown, formula FROM formulas WHERE ctg = '"+ctg+"'", null);
        int results = cursor.getCount();
        cursor.moveToFirst();

        Global.FormuleResult = new String[results];
        Global.FormuleTable = new String[results];

        for(int i = 0; i &lt; results; i++) {
            Global.FormuleTable[i] = cursor.getString(1);
            Global.FormuleResult[i] = cursor.getString(0);
        }
        close();
    }
}
</code></pre>

<p>This is activity, where I call setFormulaList() method in FormulaModel class.</p>

<pre><code>package com.math.scan;

import net.sourceforge.jeval.EvaluationException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ProblemActivity extends Activity {

    private EditText prob;
    private Button solve;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.problem);

        // text field
        prob = (EditText) findViewById(R.id.problem);

        // confirm btn
        solve = (Button) findViewById(R.id.solve);

        // check if confirm button was pressed
        solve.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                // get text from the field
                String problem = prob.getText().toString();

                // check if expression is not empty
                if(problem.length() == 0) {
                    // string is empty!
                    Toast.makeText(ProblemActivity.this, getString(R.string.empty_field), Toast.LENGTH_SHORT).show();
                } else {
                    FormulaModel f = new FormulaModel(ProblemActivity.this);
                    f.setFormulaList("mech");
                    pears.doMagic(problem);
                    try {
                        String str = Global.eval.getVariableValue(Global.UNKNOWN);
                        TextView answ = (TextView) findViewById(R.id.answer);
                        answ.setText(getString(R.string.prob_answ) + str);
                    } catch (EvaluationException e) {
                        Toast.makeText(ProblemActivity.this, getString(R.string.prob_error), Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }

}
</code></pre>

<p>I can't figure out what is wrong here.
Any ideas?</p>
