0

I have this code that converts the .db file to .csv and saves in sdcard of my android device. Unfortunately the activity stops and causes a java.lang.NullPointerException in doInBackground method

protected Boolean doInBackground(final String... args) {
    File dbFile=getDatabasePath(logindatabaseadapter3.DATABASE_NAME);
    //  DbClass DBob = new DbClass(MyDatabaseActivity.this);
    File exportDir = new File(Environment.getExternalStorageDirectory(), "");
    if (!exportDir.exists()) {
        exportDir.mkdirs();
    }

    File file = new File(exportDir, "designtry.csv");

    try {
        file.createNewFile();
        CSVWriter csvWrite = new CSVWriter(new FileWriter(file));

        //SQLiteDatabase db = DBob.getReadableDatabase();
        Cursor curCSV=db.rawQuery("select * from " + Table_Name,null);
        //  Cursor curCSV = db.rawQuery("SELECT * FROM table_ans12",null);

        csvWrite.writeNext(curCSV.getColumnNames());

        while(curCSV.moveToNext()) {
            String arrStr[] ={curCSV.getString(0),curCSV.getString(1),curCSV.getString(2),curCSV.getString(3),curCSV.getString(4),
                    curCSV.getString(5),curCSV.getString(6),curCSV.getString(7),curCSV.getString(8),curCSV.getString(9),
                    curCSV.getString(10),curCSV.getString(11),curCSV.getString(12),curCSV.getString(13),curCSV.getString(14),
                    curCSV.getString(15),curCSV.getString(16),curCSV.getString(17),curCSV.getString(18),curCSV.getString(19)};


            csvWrite.writeNext(arrStr);

        }

        csvWrite.close();
        curCSV.close();
        return true;

    }

    catch(SQLException sqlEx) {
        Log.e("MainActivity", sqlEx.getMessage(), sqlEx);
        return false;
    }
    catch (IOException e) {
        Log.e("MainActivity", e.getMessage(), e);
        return false;
    }
}
10
  • at which line you get error..?? Commented Jun 19, 2013 at 7:11
  • post the stack trace. should you not mention the folder name File exportDir = new File(Environment.getExternalStorageDirectory(), "my folder");? Commented Jun 19, 2013 at 7:11
  • @Raghunandan he wants to store inside sdcard, so I think is better File exportDir = Environment.getExternalStorageDirectory() Commented Jun 19, 2013 at 7:13
  • No the above correction does not make a difference, I am able to export the file in my sdcard but it is empty and the application crashes. The error occurs in this line Commented Jun 19, 2013 at 7:21
  • Cursor curCSV=db.rawQuery("select * from " + Table_Name,null); Commented Jun 19, 2013 at 7:22

1 Answer 1

0

I found the solution I was simply placing it in the wrong class.

Sign up to request clarification or add additional context in comments.

Comments

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.