ainunqalbi06 ainunqalbi06 - 5 months ago 13
Android Question

Error:NullPointerException when put button.setOnclickListener in onResponse method

When i click my button in my activity, it will stop and show an error like this :

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference


In my activity, i put button.setOnclickListener in onResponse() method from Response.Listener. I do this because i want to get the data from JSONObject and use that data when i click my button.

Sorry, I'm not too fluent in English :)

My activity code:

public class MenuDaftarNilai2 extends AppCompatActivity {

TextView tvMatkulDN, tvKlpDN, tvMhsDN;
Button bUasDN;

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

tvMatkulDN = (TextView) findViewById(R.id.tvMatkulDN) ;
tvKlpDN = (TextView) findViewById(R.id.tvKlpDN) ;
tvMhsDN = (TextView) findViewById(R.id.tvMhsDN) ;

bUasDN = (Button) findViewById(R.id.bUasDN) ;


Intent intent = getIntent();
final String nama_matkul = intent.getStringExtra("nama_matkul");
final String nama_klp = intent.getStringExtra("nama_klp");
final String nama_mhs = intent.getStringExtra("nama_mhs");

tvMatkulDN.setText(nama_matkul);
tvKlpDN.setText(nama_klp);
tvMhsDN.setText(nama_mhs);

Response.Listener responseListener1 = new Response.Listener<String>() {

@Override
public void onResponse(String response) {

try {

JSONObject jsonResponse = new JSONObject(response);

final String nilai_uas = jsonResponse.getString("nilai_uas");
final String id_uas = jsonResponse.getString("id_uas");

bUasDN.setText("Nilai Uas : " + nilai_uas);
bUasDN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent intent = new Intent(MenuDaftarNilai2.this, EditUas.class);

intent.putExtra("nilai_uas", nilai_uas);
intent.putExtra("nama_matkul", nama_matkul);
intent.putExtra("nama_klp", nama_klp);
intent.putExtra("nama_mhs", nama_mhs);
intent.putExtra("id_uas", id_uas);

MenuDaftarNilai2.this.startActivity(intent);
finish();

}
});


}catch (JSONException e) {
e.printStackTrace();
}

}
};
ResponUas resUas = new ResponUas(nama_klp,nama_mhs, responseListener1);
RequestQueue queue1 = Volley.newRequestQueue(MenuDaftarNilai2.this);
queue1.add(resUas);


My XML code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".LoginAsis.MenuAsisten.MenuDaftarNilai.MenuDaftarNilai2"
android:background="#ad4f4f">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nama Mata Kuliah"
android:id="@+id/textView54"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#ffffff" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nama Kelompok"
android:id="@+id/textView58"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView54"
android:layout_alignParentLeft="true"
android:textColor="#ffffff" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nama Mahasiswa"
android:id="@+id/textView59"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView58"
android:layout_alignParentLeft="true"
android:textColor="#ffffff" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":"
android:id="@+id/textView60"
android:textColor="#ffffff"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/textView61"
android:layout_alignStart="@+id/textView61" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:text=":"
android:id="@+id/textView61"
android:textColor="#ffffff"
android:layout_below="@+id/textView60"
android:layout_alignLeft="@+id/textView62"
android:layout_alignStart="@+id/textView62" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:text=":"
android:id="@+id/textView62"
android:textColor="#ffffff"
android:layout_marginLeft="68dp"
android:layout_marginStart="40dp"
android:layout_below="@+id/textView61"
android:layout_toRightOf="@+id/textView54"
android:layout_toEndOf="@+id/textView54" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:text="Medium Text"
android:id="@+id/tvMatkulDN"
android:layout_alignTop="@+id/textView60"
android:layout_toRightOf="@+id/textView60"
android:textColor="#ffffff"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:text="Medium Text"
android:id="@+id/tvKlpDN"
android:layout_below="@+id/tvMatkulDN"
android:layout_toRightOf="@+id/textView60"
android:textColor="#ffffff"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:text="Medium Text"
android:id="@+id/tvMhsDN"
android:layout_below="@+id/tvKlpDN"
android:layout_toRightOf="@+id/textView61"
android:textColor="#ffffff"
android:layout_alignRight="@+id/tvKlpDN"
android:layout_alignEnd="@+id/tvKlpDN"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Belum ada nilai uas"
android:id="@+id/bUasDN"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="@id/textView59"
android:gravity="left|center_vertical"
android:drawableRight="@drawable/ic_create_black_24dp"/>
</RelativeLayout>

Answer

just put this outside the response parenthesis ...............

bUasDN.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            Intent intent = new Intent(MenuDaftarNilai2.this, EditUas.class);

                            intent.putExtra("nilai_uas", nilai_uas);
                            intent.putExtra("nama_matkul", nama_matkul);
                            intent.putExtra("nama_klp", nama_klp);
                            intent.putExtra("nama_mhs", nama_mhs);
                            intent.putExtra("id_uas", id_uas);

                            MenuDaftarNilai2.this.startActivity(intent);
                            finish();

                        }
                    });

enjoy coding..........