1

I have been trying to export the SQLite database column values to a log.csv file. So far I have come to a point where, file is exported however without any kind of data. After the method is executed I recieve a java.lang.NullPointerException on a row where rawQuery is executed. Can somebody please assist me on this matter.

thank you for your time, view, and answer on this issue in advance.

Code for database:

public class happinessDb  extends SQLiteOpenHelper {

public static final String KEY_ROWID = "_id";
public static final String KEY_DATE = "date";
public static final String KEY_TIME = "time";
public static final String KEY_HAPPY = "happy";
public static final String KEY_NORMAL = "normal";
public static final String KEY_SAD = "sad";

public static final String DATABASE_NAME = "happinesMeter";
public static final String DATABASE_TABLE = "practiceReport";
public static final int DATABASE_VERSION = 2;

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

   private static class DbHelper extends SQLiteOpenHelper{

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                        KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                        KEY_DATE + " TEXT NOT NULL, " +
                        KEY_TIME + " TEXT NOT NULL, " +
                        KEY_HAPPY + " TEXT NOT NULL, " +
                        KEY_NORMAL + " TEXT NOT NULL, " +
                        KEY_SAD + " TEXT NOT NULL);"
        );

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }
}

    public happinessDb(Context c){ //Error
        ourContext = c;
}
public happinessDb open() throws SQLException{
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}
public void close(){
    ourHelper.close();
}

method for converting the database:

 public boolean convertHappiness() {
      String FILENAME = "log.csv";
      File directoryDownload = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
      File logDir = new File(directoryDownload, FILENAME);
      try {
          logDir.createNewFile();
          CSVWriter csvWriter = new CSVWriter(new FileWriter(logDir));
          Cursor curCSV = ourDatabase.rawQuery("SELECT * FROM practiceReport", null);
          csvWriter.writeNext(curCSV.getColumnNames());
          while (curCSV.moveToNext()) {
              String arrStr[] = { curCSV.getString(1)+ ",", curCSV.getString(2)+ ",",
                      curCSV.getString(3)+ ",", curCSV.getString(4)+ ",", curCSV.getString(5)+ ","};
              csvWriter.writeNext(arrStr);
          }
          csvWriter.close();
          curCSV.close();
          return true;
      } catch (Exception e) {
          Log.e("MainActivity", e.getMessage(), e);
          return false;
      }

}

1 Answer 1

2

You have to initialize database variable before using it with this command :

ourDatabase = this.getWritableDatabase();//you missed this line..
Cursor curCSV = ourDatabase.rawQuery("SELECT * FROM practiceReport", null);
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks for the hint about missed line, however still no data. getWritableDatabse(); is marked with red text giving me options in android studio: create getter, create property or create method.
This is the error which points to getWritableDatabase(); 10-27 15:42:29.470 22382-22382/com.sportelitemanagement.animus.happyapp E/MainActivity﹕ null java.lang.NullPointerException at com.sportelitemanagement.animus.happyapp.happinessDb.convertHappiness(happinessDb.java:143)
If android studio gave you options to create getter or setter... that means it doesn't recognized this method You can use this.getWriteDatabase() inside class extends with SQLiteOpenHelper for example public class DBHelper extends SQLiteOpenHelper{ because of this indicates to SQLiteOpenHelper other case you have to pass db Variable in the method from outside pre definded.... public boolean convertHappiness(SQLiteOpenHelper db) { I don't know exactly where is your convertHappiness() function located...
my convertHappines() is located in different activity. After onClick button is pressed in that different activity, the SQLite should be converted to csv and automatically attached to the email. That is the whole idea why I need to convert it.
public void exportCsv(View view) { //TODO - create a method which would read database happinessDb convertHappiness = new happinessDb(this); convertHappiness.convertHappiness(); readFromFile(); attachReportAndSendEmail(); }
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.