-6

I am having an issue with a click listener for a choose file button.

This is the error.

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener 

I understand what a null pointer exception is and I have search thoroughly for an issue similar to mine. I tried a lot of solution for issue similar to mine but I get always an error and this is my code :

public class Main2Activity extends AppCompatActivity {

Button btnChoiseFile;
ImageView imgView;
private static final int SELECT_PICTURE = 0;
String filePath = "";

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if((Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ) {
            ActivityCompat.requestPermissions((Activity) this, new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE}, 300);
        }
    }

    btnChoiseFile = (Button) findViewById(R.id.button2);
    btnChoiseFile.setOnClickListener(listenerChoiceFile);
    ImageView imgView = (ImageView) findViewById(R.id.img_view);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case SELECT_PICTURE:
            if (data != null) {


                Uri selectedImage = data.getData();
                filePath = getPath(selectedImage);
                Log.d("path", filePath);

                imgView.setImageBitmap(BitmapFactory.decodeFile(filePath));

            }
            break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

public String getPath(Uri uri) {
    String[] projection = {MediaStore.Images.Media.DATA};
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

View.OnClickListener listenerChoiceFile = new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, SELECT_PICTURE);
        intent.setType("*/*");
        startActivityForResult(intent, SELECT_PICTURE);
    }
};

}

Yassine
  • 45
  • 7
  • `btnChoiseFile.setOnClickListener(new View.OnClickListener() ...` Try this version. The error maybe could be, to the error reference id `button2` or many other things. i recommmned that you should put more code... – R. García May 18 '18 at 11:26
  • @Killer this post it has nothing to do with it! – R. García May 18 '18 at 11:30
  • smple issue, check id of button given is correct by checking xml – dev May 18 '18 at 11:31
  • @R.García it is ... since the ID of the button is probably incorrect, there is a NPE during the call of `setOnClickListener`. By the way, the instance `listenerChoiceFile` is initialiazed below, as a class member, it exist and isn't null – AxelH May 18 '18 at 11:32
  • 1
    @R.GarcíaI have never seen compiler lie to me. Just take it as a funny manner – Shubham AgaRwal May 18 '18 at 11:32
  • Why do you call `startActivityForResult` twince? – grabarz121 May 18 '18 at 11:33

2 Answers2

0

Make sure you have a button with id = button2 in your activity_main layout(or that you are using the layout you intended). The button will usually only be null here if you don't have it defined in your layout.

Cody W.
  • 376
  • 2
  • 6
0

check if u have Button with id button2 in activity_main. Post stack trace or more details