NoobNinja
NoobNinja

Reputation: 57

NullPointerException when setting OnClickListener

For some unknown reason I'm getting FATAL EXCEPTION: main - java.lang.NullPointerException on the line:

x_button.setOnClickListener(new View.OnClickListener() {

but I'm not sure why this is happening. The button is there - so I'm not sure why a null pointer would occur at this point - but it is.

Any input is greatly appreciated.

SOURCE SNIPPET:

@Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub

            super.onPostExecute(result);
            // Show updated screen if table was successfully updated
            // Or alert indicating settings are not updated
            if (result.equals("success")) {

                setContentView(R.layout.completion);

            } else
                setContentView(R.layout.error);

            Button x_button = (Button) findViewById(R.id.x_button);
            x_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finishAll(v);
                }
            });
        }

LOGCAT:

08-26 15:49:42.419: W/System.err(5998): java.io.IOException: Stream is closed
08-26 15:49:42.429: W/System.err(5998):     at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:161)
08-26 15:49:42.429: W/System.err(5998):     at java.io.DataInputStream.read(DataInputStream.java:95)
08-26 15:49:42.429: W/System.err(5998):     at java.io.InputStreamReader.read(InputStreamReader.java:255)
08-26 15:49:42.429: W/System.err(5998):     at java.io.BufferedReader.fillBuf(BufferedReader.java:128)
08-26 15:49:42.429: W/System.err(5998):     at java.io.BufferedReader.readLine(BufferedReader.java:357)
08-26 15:49:42.429: W/System.err(5998):     at com.tracfone.straighttalk.networktasklibrary.NetworkTask.doInBackground(NetworkTask.java:37)
08-26 15:49:42.429: W/System.err(5998):     at com.tracfone.straighttalk.networktasklibrary.NetworkTask.doInBackground(NetworkTask.java:1)
08-26 15:49:42.429: W/System.err(5998):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-26 15:49:42.429: W/System.err(5998):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-26 15:49:42.429: W/System.err(5998):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-26 15:49:42.429: W/System.err(5998):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-26 15:49:42.429: W/System.err(5998):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-26 15:49:42.429: W/System.err(5998):     at java.lang.Thread.run(Thread.java:1019)
08-26 15:49:46.299: W/dalvikvm(5998): threadid=1: thread exiting with uncaught exception (group=0x4016e560)
08-26 15:49:46.299: E/AndroidRuntime(5998): FATAL EXCEPTION: main
08-26 15:49:46.299: E/AndroidRuntime(5998): java.lang.NullPointerException
08-26 15:49:46.299: E/AndroidRuntime(5998):     at com.project.new.datasettings.ConfigFinalActivity$TableUpdateRequestTask.onPostExecute(ConfigFinalActivity.java:552)
08-26 15:49:46.299: E/AndroidRuntime(5998):     at com.project.new.datasettings.ConfigFinalActivity$TableUpdateRequestTask.onPostExecute(ConfigFinalActivity.java:1)
08-26 15:49:46.299: E/AndroidRuntime(5998):     at android.os.AsyncTask.finish(AsyncTask.java:417)
08-26 15:49:46.299: E/AndroidRuntime(5998):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
08-26 15:49:46.299: E/AndroidRuntime(5998):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
08-26 15:49:46.299: E/AndroidRuntime(5998):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 15:49:46.299: E/AndroidRuntime(5998):     at android.os.Looper.loop(Looper.java:130)
08-26 15:49:46.299: E/AndroidRuntime(5998):     at android.app.ActivityThread.main(ActivityThread.java:3821)
08-26 15:49:46.299: E/AndroidRuntime(5998):     at java.lang.reflect.Method.invokeNative(Native Method)
08-26 15:49:46.299: E/AndroidRuntime(5998):     at java.lang.reflect.Method.invoke(Method.java:507)
08-26 15:49:46.299: E/AndroidRuntime(5998):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
08-26 15:49:46.299: E/AndroidRuntime(5998):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
08-26 15:49:46.299: E/AndroidRuntime(5998):     at dalvik.system.NativeStart.main(Native Method)

FULL SOURCE:

https://docs.google.com/document/d/1ap5qknNcn9RaJhewc9CarQja2uS4Xjgtzw4eNzXzLKY/edit?usp=sharing

Upvotes: 2

Views: 5405

Answers (1)

Philipp Jahoda
Philipp Jahoda

Reputation: 51411

Your NullpointerException occurs because the Button "x_button" is null, which means that findViewById(...) could not find a Button with the id you provided.

I see 2 possible reasons for that:

  • The "R.id.x_button" is in neither of your two layout files "completion" and "error" but in some other layout file instead. This will cause no compiler error to come up, but will cause the Nullpointer upon running the code.
  • the "R.id.x_button" is just in one of them, but you are using the other one in setContentView(...), depending on your "success".

What I would recommend:

Check your layout files "error" and "completion" and see if the both contain a Button with the id "R.id.x_button", if not, change your code or add the Button to the layout file.

If your Button for example only exists in "completion", you need to change your code to this:

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub

        super.onPostExecute(result);
        // Show updated screen if table was successfully updated
        // Or alert indicating settings are not updated
        if (result.equals("success")) {

            setContentView(R.layout.completion);

            Button x_button = (Button) findViewById(R.id.x_button);
            x_button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finishAll(v);
                }
             });
        } else
            setContentView(R.layout.error);
    }

This will avoid the NullPointer, because the Button is only initialized when the layout that actually contains the Button is loaded.

Upvotes: 2

Related Questions