Question

I'm having a problem (Null Pointer Exception) when calling startActivityForResult() from class inside another class, here is the code :

public class OCRActivity extends Activity {

public OCRActivity(String operator)
{
    this.operator = operator;
}


public void startCameraActivity() {

    final Intent capture_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(capture_intent, 1); // I get the Null pointer Exception here
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) {

        if(requestCode == 1){
            //get the Uri for the captured image
            picUri = data.getData();
            //carry out the crop operation
            performCrop();
        }
}

and here is where I call startCameraActivity()

public class WayToFillActivity extends Activity implements OnClickListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.way_to_fill);

    CAMERA_BTN = (Button) findViewById(R.id.camera_btn);
    CAMERA_BTN.setOnClickListener(this);
    Intent operator_intent =  getIntent();
    OPERATOR = operator_intent.getStringExtra("operator");
}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.camera_btn)
    {
        OCRActivity ocr = new OCRActivity(OPERATOR);
        ocr.startCameraActivity();

    }

}

So please tell me if I'm doing something wrong !!

The Logcat

01-13 16:22:26.583: E/AndroidRuntime(32425): java.lang.NullPointerException
01-13 16:22:26.583: E/AndroidRuntime(32425):    at     android.app.Activity.startActivityForResult(Activity.java:3190)
01-13 16:22:26.583: E/AndroidRuntime(32425):    at com.almannaa.EasyRecharge.OCRActivity.startCameraActivity(OCRActivity.java:176)
Was it helpful?

Solution

You are treating OCRActivity as an ordinary Java class, and not like another Activity. Due to this, when you call startActivityForResult() you get a NPE as the Activity's onCreate() has not been called, which means that its current instance is not valid for calling startActivityForResult()

Instead, try using:

public class WayToFillActivity extends Activity implements OnClickListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.way_to_fill);

    CAMERA_BTN = (Button) findViewById(R.id.camera_btn);
    CAMERA_BTN.setOnClickListener(this);
    Intent operator_intent =  getIntent();
    OPERATOR = operator_intent.getStringExtra("operator");
}

@Override
public void onClick(View v) {
    if (v.getId() == R.id.camera_btn)
    {
        OCRActivity ocr = new OCRActivity(OPERATOR);
        ocr.startCameraActivity();

    }

}

public void startCameraActivity() {

    final Intent capture_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(capture_intent, 1); // I get the Null pointer Exception here
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) {

        if(requestCode == 1){
            //get the Uri for the captured image
            picUri = data.getData();
            //carry out the crop operation
            performCrop();
        }
}
}

You should probably move functions like performCrop() (and any other functions that don't really need to be in the activity itself, and can work by receiving data as parameters and returning it) into a separate class (call it Utility or whatever).

OTHER TIPS

Maybe you should check if the data is not null.

@Override        
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1 && data != null) {

        if(resultCode == RESULT_OK){
            //get the Uri for the captured image
            picUri = data.getData();
            //carry out the crop operation
            performCrop();
        } }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top