diego-bf
diego-bf

Reputation: 11

NullPointerException on button click

I get a NullpointerException error when I click my button. The logcat indicates that the error is in the line 62

String Titre = Title.getText().toString();

I think it's because my EditTexts are not initialized, that's why I created the initFields() method, but I still don't know where to call it because wherever I call it i get a NullPointerException too

here is my code :

public class MainActivity extends Activity implements
android.view.View.OnClickListener{

private EditText Title, Description, Coordonnees, Tel, mail;
private Bitmap photo;
private ImageView imageView;
//private PhotoPicker photoPicker;
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Title = (EditText) findViewById(R.id.titre);
    Description = (EditText) findViewById(R.id.Description);
    imageView = (ImageView) findViewById(R.id.images);
    Coordonnees = (EditText) findViewById(R.id.Coordonnees);
    Tel = (EditText) findViewById(R.id.tel);
    mail = (EditText) findViewById(R.id.Mail);

    setContentView(R.layout.activity_main);
    //initFields();
    ((Button)this.findViewById(R.id.bouton1)).setOnClickListener(this);
    ((ImageView)this.findViewById(R.id.images)).setOnClickListener(this);



}

@Override
public void onClick(View v) {

    switch (v.getId()) {

    case R.id.images:
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
        break;
    case R.id.bouton1:
        String Titre = Title.getText().toString();
        String Desc = Description.getText().toString();
        String Coord = Coordonnees.getText().toString();
        String tel = Tel.getText().toString();
        String Mailtxt = mail.getText().toString();

        boolean trueMail = Pattern
        .compile("^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$",
                Pattern.CASE_INSENSITIVE).matcher(Mailtxt)
                .matches();
        boolean trueTel = Pattern.matches("^[+]?[0-9]{3,}$", tel);

        if (imageView == null || !trueMail || !trueTel || Titre.equals("")
                || Desc.equals("") || Coord.equals("")){
            Toast.makeText(this, R.string.missing_field, Toast.LENGTH_SHORT).show();
        }
        break;
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            imageView = (ImageView) findViewById(R.id.images);
            imageView.setImageDrawable(getResources().getDrawable(
                    R.drawable.register_photo));
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize=16; //image will be 16x smaller than an original. Note, that it's better for perfomanse to use powers of 2 (2,4,8,16,32..).
            Bitmap bmp = BitmapFactory.decodeFile(selectedImagePath, options); 
            Drawable d = new BitmapDrawable(bmp);

            imageView.setImageDrawable(d);
        }
    }

}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
private void initFields() {
    Title.setText("");
    Description.setText("");
    Coordonnees.setText("");
    Tel.setText("");
    mail.setText("");
}

}

Upvotes: 0

Views: 134

Answers (4)

Raghunandan
Raghunandan

Reputation: 133580

Try the below

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Title = (EditText) findViewById(R.id.titre);
    ... // rest of the code  

You need to inflate the layout first then initialize views. findViewById looks for a view in the current inflated layout. So you need to set the content of the layout to the activity first and then initialize your views.

You have already initialized imageView like below no need to initialize it again

  imageView = (ImageView) findViewById(R.id.images);

So replace this

  ((ImageView)this.findViewById(R.id.images)).setOnClickListener(this);

By

  imageView.setOnClickListener(this);

Upvotes: 2

Hardik Joshi
Hardik Joshi

Reputation: 9507

You need to use setContentView before your intialization of views.

Always keep setContentView after super.onCreate(savedInstanceState);

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


    Title = (EditText) findViewById(R.id.titre);
    Description = (EditText) findViewById(R.id.Description);
    imageView = (ImageView) findViewById(R.id.images);
    Coordonnees = (EditText) findViewById(R.id.Coordonnees);
    Tel = (EditText) findViewById(R.id.tel);
    mail = (EditText) findViewById(R.id.Mail);

Upvotes: 2

Serj Moya
Serj Moya

Reputation: 149

As some users say, you have to create and set the layout before get views from it.

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

    Title = (EditText) findViewById(R.id.titre);
    Description = (EditText) findViewById(R.id.Description);
    imageView = (ImageView) findViewById(R.id.images);
    Coordonnees = (EditText) findViewById(R.id.Coordonnees);
    Tel = (EditText) findViewById(R.id.tel);
    mail = (EditText) findViewById(R.id.Mail);

And then you can associate onClickListener to you buttons.

Upvotes: 0

Alexis C.
Alexis C.

Reputation: 93902

You should inflate your layout before finding the elements.

 super.onCreate(savedInstanceState);    
 setContentView(R.layout.activity_main); <-- move this line here
 Title = (EditText) findViewById(R.id.titre);
 /**/
  //initFields();

From the doc :

Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View)

/**/

onCreate(Bundle)is where you initialize your activity. Most importantly, here you will usually call setContentView(int)with a layout resource defining your UI, and using findViewById(int)to retrieve the widgets in that UI that you need to interact with programmatically.

Upvotes: 2

Related Questions