3 votes

Android: NullPointerException in .getIntent().getExtras()

I'm in an Android class project and I have it almost finished but I need to pass some variables from one activity to another, it gives me NullPointerException and searching I have not found anything.


Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'Android.os.Bundle Android.content.Intent.getExtras()' on a null object reference at com.grupo3.cebancburger.AnadirHamburguesa.(AnadirHamburguesa.java:31)

The attempt I make in the classroom.


(The views)

nomb = (EditText)findViewById(R.id.nombre);
dire = (EditText)findViewById(R.id.direccion);
tele = (EditText)findViewById(R.id.telefono);
email = (EditText)findViewById(R.id.email);


intent = new Intent(DatosCliente.this, AnadirHamburguesa. 
intent.putExtra("nombre", nomb.getText().toString());
intent.putExtra("direccion", dire.getText().toString());
intent.putExtra("telefono", tele.getText().toString());
intent.putExtra("email", email.getText().toString());


Bundle extras = getIntent().getExtras();
String nom = extras.getString("nombre");
String dire = extras.getString("direccion");
String tele = extras.getString("telefono");
String email = extras.getString("email");


Carlos Cardoso Points 502

Try to retrieve the information in your AddBurger activity without using Bundle, as follows


Intent intent = getIntent();
String nombre = intent.getStringExtra("nombre");
String dire = intent.getStringExtra("direccion");    

If you use Bundle to pass data, you must create the Bundle and pass it in the intent.

Bundle bundle = new Bundle();
Intent intent = new Intent(DatosCliente.this, AnadirHamburguesa.class)

In AnadirHamburger

Bundle extras = getIntent().getExtras();
String nom = extras.getString("nombre");

In the future try to use Parcelable or Serializable to pass objects from one activity to another. For example if you have an object of type Client that implements the Parcelable class

Cliente cliente = new Cliente(nombre,direccion);

Intent intent = new Intent(getApplicationContext(, SigActividad.class);
intent.putExtra("cliente", cliente);

In SigActividad:

Cliente cliente = getIntent().getExtras().getParcelable("nombre");


HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by: