0
private lateinit var  main: MainActivity

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState) 
        setContentView(R.layout.activity_regions)
 main = MainActivity()
}    

On create method

 override fun onActivityResult(
        requestCode: Int,
        resultCode: Int,
        data: Intent?
    ) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == MainActivity2.SCREEN_RECORD_REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK) {
                //Set file path or Uri depending on SDK version
                main.setOutputPath()
                //Start screen recording
                hbRecorder.startScreenRecording(data, resultCode, this)
            }
        }
    }

here is my Kotlin method, main is my Java Classes object and I am trying to call the setOutPath Method but when I run the program it gives 'android.content.ContentResolver android.content.Context.getContentResolver()' on a null object reference error

ContentResolver resolver;
ContentValues contentValues;
Uri mUri;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void setOutputPath() {
    String filename = generateFileName();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        resolver = getContentResolver();
        contentValues = new ContentValues();
        contentValues.put(MediaStore.Video.Media.RELATIVE_PATH, "Movies/" + "HBRecorder");
        contentValues.put(MediaStore.Video.Media.TITLE, filename);
        contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, filename);
        contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "video/mp4");
        mUri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentValues);
        //FILE NAME SHOULD BE THE SAME
        hbRecorder.setFileName(filename);
        hbRecorder.setOutputUri(mUri);
    }else{
        createFolder();
        hbRecorder.setOutputPath(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) +"/HBRecorder");
    }
    }

here is my java method. It gives me an error on this line.

resolver = getContentResolver();
  • Please show the class definition lines. What classes are these? – David Wasser Oct 12 '20 at 12:19
  • 1
    I don't think that closing this question as duplicate of the canonical NPE question is useful. Clearly OP is calling a method `getContentResolver()` and it isn't obvious how this could generate an NPE. I've reopened the question. – David Wasser Oct 12 '20 at 12:21
  • What is `main` here . I suspect you created an Activity instance? Pls update the question with this detail . – ADM Oct 12 '20 at 13:05
  • main is my Java Classes object, I have already defined main in my question @ADM – salih suat kükrer Oct 12 '20 at 13:45
  • That's not I am asking . I am asking how are you initialising main ? – ADM Oct 12 '20 at 13:46
  • I have edited the question. I hope I get your question correctly. @ADM – salih suat kükrer Oct 12 '20 at 13:53
  • Yeah . I was right .. main = MainActivity() . That right there is a disaster . You never create an instance of an Activity in android the OS handles it for you. Please read about activity . For now you can just drop all this main calling . And just call method Directly you do not need and object to call a method in same class . – ADM Oct 12 '20 at 13:56
  • Thanks for your answer, I am calling the method from another class so I created an object, when I call another method, I can get the result without any error. I do not understand, why I am getting an error when I call this method? These two codes belong to different activities. One of them is java activity and another one is Kotlin activity. If you read carefully you will understand. @ADM – salih suat kükrer Oct 12 '20 at 14:11
  • Java and kotlin does not matter . You should not create an object of activity yourself. – ADM Oct 12 '20 at 14:19

0 Answers0