-1

Can Anyone help me please? explain to me why this code is "java.lang.NullPointerException" error.

final ImageButton sampleImageButton = (ImageButton) findViewById(R.id.btn_sample);

    final Dialog dd_dialog = new Dialog(MainActivity.this);
    dd_dialog.setContentView(R.layout.Sample_layout);
    dd_dialog.setTitle("Sample");
    dd_dialog.setCancelable(true);

sampleImageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dd_dialog.show();
                }
            });

here's the xml

<Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_sample"
        android:text="Sample"
        android:background="#4372AA"
        android:layout_marginRight="10dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:textColor="#FFFFFF"
        android:layout_marginTop="30dp"/>
Kiel
  • 41
  • 7
  • Also sampleImageButton isn't same as prescribeImageButton – Anton Shkurenko Jan 13 '16 at 09:55
  • i already edit the code.. but the error is pointing to this code sampleImageButton.setOnClickListener(new View.OnClickListener() { – Kiel Jan 13 '16 at 09:56
  • hmmm,getting `NPE` because probably ImageView is inside layout of Dialog. so try it as :`final ImageButton sampleImageButton = (ImageButton) dd_dialog .findViewById(R.id.btn_sample); ` – ρяσѕρєя K Jan 13 '16 at 09:56
  • 1
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – yennsarah Jan 13 '16 at 09:57
  • @ρяσѕρєя K actually the error is pointing to this part sampleImageButton.setOnClickListener(new View.OnClickListener() { – Kiel Jan 13 '16 at 09:58
  • @anupDasari here's the logcat errors FATAL EXCEPTION: main Process: sample.sample.com.sampledesign, PID: 32593 java.lang.NullPointerException – Kiel Jan 13 '16 at 10:02
  • at sample.sample.com.sampledesign.MainActivity$3.onClick(MainActivity.java:116) at android.view.View.performClick(View.java:4637) at android.view.View$PerformClick.run(View.java:19422) – Kiel Jan 13 '16 at 10:03
  • at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5479) – Kiel Jan 13 '16 at 10:03
  • at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) – Kiel Jan 13 '16 at 10:03
  • at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) at dalvik.system.NativeStart.main(Native Method) – Kiel Jan 13 '16 at 10:03
  • @ρяσѕρєя it didn't work either sorry – Kiel Jan 13 '16 at 10:06
  • @Kiel: Show your updated code – ρяσѕρєя K Jan 13 '16 at 10:07
  • @ρяσѕρєя here's the new structure of the codes: final Dialog dd_dialog = new Dialog(MainActivity.this); final ImageButton sampleImageButton = (ImageButton) dd_dialog.findViewById(R.id.btn_sample); dd_dialog.setContentView(R.layout.Sample_layout); dd_dialog.setTitle("Sample"); dd_dialog.setCancelable(true); then the onclicklistener here – Kiel Jan 13 '16 at 10:14
  • @ρяσѕρєяK I found the problem just now, thanks for the effort :) – Kiel Jan 13 '16 at 10:18

3 Answers3

1

problem is here you have taken button in xml layout and in java file you declare ImageButton so change from ImageButton to Button.

change this line from

final ImageButton sampleImageButton = (ImageButton) findViewById(R.id.btn_sample);

To

final Button sampleImageButton = (Button) findViewById(R.id.btn_sample);
0

It looks like that you haven't set the activity view using setContentView before using findViewById

Hope this helps.

Mustansar Saeed
  • 2,730
  • 2
  • 22
  • 46
0

probably this must be the correction :

final ImageButton sampleImageButton = (ImageButton) dd_dialog.findViewById(R.id.btn_sample);

add this below of

final Dialog dd_dialog = new Dialog(MainActivity.this);
dd_dialog.setContentView(R.layout.Sample_layout);
rahul
  • 1,095
  • 8
  • 22