jeudi 26 novembre 2015

Java Null Pointer exception when querying SQLite Database in Android [duplicate]

This question already has an answer here:

Ok, I'm an Android noob so bear with me, spent a good few hours on this and searching on here.

I have an activity MainActivity that creates a new activity which has a Pager for several Fragments. I have a Sqlite Database that has a table with a sequence number that I wish to increment upon clicking a button on the MainActivity and then send the sequence number to a text view on a fragment.

So I have my Main Activity where the button is pressed:

private boolean addFactFind(){
    //Get a new sequence number
    newSeq();
    //Create new factFind
    Intent factFindIntent = new Intent(this, EditFactFind.class);
    //Give fact find new sequence number
    setSeqOnFrag(newSeqNo);
    //Open new fact find
    startActivity(factFindIntent);
    return true;

}

public void setSeqOnFrag(int newSeqNo) {
    Summary.Trans frag_Trans = (Summary.Trans)getFragmentManager().findFragmentById(R.id.drawer_layout_sum);
    frag_Trans.updateFFID(newSeqNo,ffidTextView);

}

public void newSeq() {
    Fact_FindDAO fact_findDAO = new Fact_FindDAO(this);
    fact_findDAO.setSequence();
    newSeqNo=fact_findDAO.NewSeq;
}

My DB Helper (A snippet, I've left out the long list of columns etc) :

  public Fact_FindDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

@Override
public void onCreate(SQLiteDatabase database) {
    database.execSQL(DATABASE_CREATE);
    database.execSQL(DATABASE_CREATE_SEQ);
    }

and the DAO (again left out longer lists):

public Fact_FindDAO(Context context) {
    dbHelper = new Fact_FindDBHelper(context);
}

public void open() {
    database = dbHelper.getWritableDatabase();
}

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


public void deleteFactFind(Fact_FindDBList factFindDBList){
    long id = factFindDBList.getId();
    System.out.println("Fact find deleted with id:" +id);
    database.delete(Fact_FindDBHelper.TABLE_PERSONAL, Fact_FindDBHelper.PERSONAL_ID + " = " + id, null);

}

public List<Fact_FindDBList> getAllFactFinds(){
    List<Fact_FindDBList> fact_findDBLists = new ArrayList<Fact_FindDBList>();

    Cursor cursor = database.query(Fact_FindDBHelper.TABLE_PERSONAL, allColumns,null,null,null,null,null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()){
        Fact_FindDBList fact_findDBList = cursorToFactFind(cursor);
        fact_findDBLists.add(fact_findDBList);
        cursor.moveToNext();
    }
    //ALWAYS close cursor
    cursor.close();
    return fact_findDBLists;
}

private Fact_FindDBList cursorToFactFind(Cursor cursor) {
    Fact_FindDBList fact_findDBList = new Fact_FindDBList();
    fact_findDBList.setId(cursor.getLong(0));
    fact_findDBList.setClient(cursor.getString(1));
    return fact_findDBList;
}

public int setSequence(){

    Cursor cursor = database.rawQuery("Select "+Fact_FindDBHelper.SEQUENCE_NO+" from "+Fact_FindDBHelper.TABLE_SEQUENCE,null,null);
    CurrentSeq = cursor.getInt(0);
    cursor.moveToFirst();
    cursor.close();
    NewSeq = CurrentSeq ++;
    String updateQuery = "UPDATE "+Fact_FindDBHelper.TABLE_SEQUENCE
            +" SET "+Fact_FindDBHelper.SEQUENCE_NO
            +" = "+NewSeq
            +" WHERE "
            +Fact_FindDBHelper.SEQUENCE_NO
            +" = "+CurrentSeq;
    database.execSQL(updateQuery);
    return NewSeq;
}

When I click the button to add I get a null pointer exception:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[], android.os.CancellationSignal)' on a null object reference
                                                 at uk.co.mcclure_solicitors.wwjmcclure.Fact_FindDAO.setSequence(Fact_FindDAO.java:106)
                                                 at uk.co.mcclure_solicitors.wwjmcclure.MainActivity.newSeq(MainActivity.java:181)
                                                 at uk.co.mcclure_solicitors.wwjmcclure.MainActivity.addFactFind(MainActivity.java:162)
                                                 at uk.co.mcclure_solicitors.wwjmcclure.MainActivity.onClick(MainActivity.java:53)

The relevant lines are above in the code snippets aside from 53 which just calls the addFactFind method.

Now I'm a bit stuck with what to do now, I think that the null pointer is coming from the databases not being created by this point so there is effectively nothing to query? Might be something else completely stupid. Sorry, new to this as I said so please go easy!

Aucun commentaire:

Enregistrer un commentaire