-3

I'm struggling with CAMERA2 API and SQLite. After the video captured the clip name must save in the db Those are the steps I'm trying to figure out.

1.In the video capturing class, will call for the db activities class

    VS_dbActivity dbActivity = new VS_dbActivity();
    String msg = dbActivity.sendData(mNextVideoAbsolutePath);

2.In the dbActivity class trying to send mNextVideoAbsolutePath into db

public class VS_dbActivity extends AppCompatActivity{

private VS_dbConfig dbHelp;

public String sendData(String filePath) {
    dbHelp = new VS_dbConfig(this);
    SQLiteDatabase db = dbHelp.getWritableDatabase();
    // some example
}

}

In the above-mentioned class, I didn't add the constructor. Is it necessary adding constructor? What happens if didn't use the constructor.

When I'm run this app it will appear error in

SQLiteDatabase db = dbHelp.getWritableDatabase();

Error log :---->>

FATAL EXCEPTION: main
                                                                                Process: com.android.ejsoft.video_upload_000web, PID: 334
                                                                                java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
                                                                                    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:274)
                                                                                    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
                                                                                    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                                                    at com.android.ejsoft.video_upload_000web.dbManage.VS_dbActivity.sendData(VS_dbActivity.java:28)
                                                                                    at com.android.ejsoft.video_upload_000web.Camera2VideoFragment.sendFilePath(Camera2VideoFragment.java:578)
                                                                                    at com.android.ejsoft.video_upload_000web.Camera2VideoFragment.access$1200(Camera2VideoFragment.java:60)
                                                                                    at com.android.ejsoft.video_upload_000web.Camera2VideoFragment$4$2.onInfo(Camera2VideoFragment.java:555)
                                                                                    at android.media.MediaRecorder$EventHandler.handleMessage(MediaRecorder.java:1159)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:135)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5753)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

Isn't there anyway without doing constructor for getting the context of the activity? Thank you for your valuable time.

E J Chathuranga
  • 927
  • 12
  • 27

1 Answers1

2

If something is not an Activity, don't extend an Activity!

You can pass a valid Context into this method or a constructor of the class. Don't just think "I need this to be a Context so I'll just make it an Activity".

public class VS_dbHelper {

    private final VS_dbConfig dbHelp;

    public VS_dbHelper(Context c) {
        dbHelp = new VS_dbConfig(c);
    } 

    public String sendData(String filePath) {
        // For example 
        SQLiteDatabase db = dbHelp.getWritableDatabase();
    } 

And you now need new VS_dbHelper(MainActivity.this) for getting the Context correctly

Alternatively, you can move the sendData method into the VS_dbConfig class which is already your SqliteOpenHelper, it seems. There's no reason to hide your database helper behind another one

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245