• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps NullPointerException on ClickListener

Hi guys,
I've been researching this, and I've seen a number of cases with the same issue, but none of their solutions have fit my problem.

Code:
@Override
    protected void onCreate(Bundle savedInstanceState)
    {
....
ConnectButton = (Button)findViewById(R.id.Connect);
        SettingsButton = (Button)findViewById(R.id.Settings);
        CreditsButton = (Button)findViewById(R.id.Credits);
        ExitButton = (Button)findViewById(R.id.Exit);
        BackButton = (Button)findViewById(R.id.Back);
        quality = (RadioGroup) findViewById(R.id.Quality);
        
        ConnectButton.setOnClickListener(new OnClickListener()
        {
        	public void onClick(View v)
        	{
        		scanIP();
        		try
                {
                	
                }
                catch(RuntimeException r)
                {
                	print(r.toString());
                }
        	}
        });
        
        SettingsButton.setOnClickListener(new OnClickListener()
        {
        	public void onClick(View v)
        	{
        		setContentView(R.layout.settings);
        	}
        });
        
/*Line 86*/        BackButton.setOnClickListener(new OnClickListener()
        {
        	public void onClick(View v)
        	{
        		String selected = findViewById(quality.getCheckedRadioButtonId()).toString();
        		setQuality(selected);
        		setContentView(R.layout.activity_main);
        	}
        });
        
        CreditsButton.setOnClickListener(new OnClickListener()
        {
        	public void onClick(View v)
        	{
        	}
        });
        
        ExitButton.setOnClickListener(new OnClickListener()
        {
        	public void onClick(View v)
        	{
        		try
        		{
            		frontCam.release();
            		backCam.release();
        		}
        		catch(NullPointerException e){}
        		finish();
        	}
        });
    }
....
}

All of my other buttons work perfectly, the one button that throws the exception is BackButton. I've got no idea why. I found 'Back' in R.id, it's been referenced correctly, there are no errors in the java file at all.
Here is my logcat report:
Code:
09-11 21:53:35.732: E/AndroidRuntime(857): FATAL EXCEPTION: main
09-11 21:53:35.732: E/AndroidRuntime(857): java.lang.RuntimeException: Unable to start activity ComponentInfo{droidcam.assets/droidcam.assets.MainActivity}: java.lang.NullPointerException
09-11 21:53:35.732: E/AndroidRuntime(857): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-11 21:53:35.732: E/AndroidRuntime(857): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-11 21:53:35.732: E/AndroidRuntime(857): 	at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-11 21:53:35.732: E/AndroidRuntime(857): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-11 21:53:35.732: E/AndroidRuntime(857): 	at android.os.Handler.dispatchMessage(Handler.java:99)
09-11 21:53:35.732: E/AndroidRuntime(857): 	at android.os.Looper.loop(Looper.java:123)
09-11 21:53:35.732: E/AndroidRuntime(857): 	at android.app.ActivityThread.main(ActivityThread.java:4627)
09-11 21:53:35.732: E/AndroidRuntime(857): 	at java.lang.reflect.Method.invokeNative(Native Method)
09-11 21:53:35.732: E/AndroidRuntime(857): 	at java.lang.reflect.Method.invoke(Method.java:521)
09-11 21:53:35.732: E/AndroidRuntime(857): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-11 21:53:35.732: E/AndroidRuntime(857): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-11 21:53:35.732: E/AndroidRuntime(857): 	at dalvik.system.NativeStart.main(Native Method)
09-11 21:53:35.732: E/AndroidRuntime(857): Caused by: java.lang.NullPointerException
09-11 21:53:35.732: E/AndroidRuntime(857): 	at droidcam.assets.MainActivity.onCreate(MainActivity.java:86)
09-11 21:53:35.732: E/AndroidRuntime(857): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-11 21:53:35.732: E/AndroidRuntime(857): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-11 21:53:35.732: E/AndroidRuntime(857): 	... 11 more

Here is my XML
activity_main:
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" >

    
<AbsoluteLayout
	android:id="@+id/frame"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	xmlns:android="http://schemas.android.com/apk/res/android">
<TableRow
	android:id="@+id/frontCamera"
	android:layout_width="147dp"
	android:layout_height="130dp"
	android:visibility="invisible"
	android:orientation="horizontal"
	android:layout_x="0dp"
	android:layout_y="1dp">
</TableRow>
<TableRow
	android:id="@+id/backCamera"
	android:layout_width="147dp"
	android:layout_height="130dp"
	android:visibility="invisible"
	android:orientation="horizontal"
	android:layout_x="173dp"
	android:layout_y="1dp">
</TableRow>
<Button
	android:id="@+id/Connect"
	android:layout_width="200dp"
	android:layout_height="wrap_content"
	android:text="Connect to a Computer"
	android:layout_x="60dp"
	android:layout_y="169dp" />
<Button
	android:id="@+id/Settings"
	android:layout_width="128dp"
	android:layout_height="wrap_content"
	android:text="Settings"
	android:layout_x="95dp"
	android:layout_y="222dp" />
<Button
	android:id="@+id/Credits"
	android:layout_width="128dp"
	android:layout_height="wrap_content"
	android:text="Credits"
	android:layout_x="96dp"
	android:layout_y="275dp" />
<Button
	android:id="@+id/Exit"
	android:layout_width="148dp"
	android:layout_height="wrap_content"
	android:text="Exit"
	android:layout_x="84dp"
	android:layout_y="330dp" />
</AbsoluteLayout>

</RelativeLayout>

Settings:
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"
    xmlns:android1="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <AbsoluteLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/frame"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TableRow
            android:id="@+id/frontCamera"
            android:layout_width="147dp"
            android:layout_height="130dp"
            android:layout_x="0dp"
            android:layout_y="1dp"
            android:orientation="horizontal"
            android:visibility="invisible" >

        </TableRow>

<TableRow
	android:id="@+id/backCamera"
	android:layout_width="147dp"
	android:layout_height="130dp"
	android:visibility="invisible"
	android:orientation="horizontal"
	android:layout_x="173dp"
	android:layout_y="1dp">
</TableRow>

<Button
    android:id="@+id/Back"
    android:layout_width="148dp"
    android:layout_height="wrap_content"
    android:layout_x="84dp"
    android:layout_y="330dp"
    android:text="Main Menu" />

<RadioGroup
    android:id="@+id/Quality"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="108dp"
    android:layout_y="96dp" >

    <RadioButton
        android:id="@+id/VHigh"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="Very High" />

    <RadioButton
        android:id="@+id/High"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="High" />

    <RadioButton
        android:id="@+id/Balanced"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Balanced" />

    <RadioButton
        android:id="@+id/Low"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Low" />
</RadioGroup>

<EditText
    android:id="@+id/editText1"
    android:layout_width="159dp"
    android:layout_height="wrap_content"
    android:layout_x="80dp"
    android:layout_y="42dp"
    android:clickable="false"
    android:ems="10"
    android:inputType="none"
    android:text="Camera Quality:" >

    <requestFocus />
</EditText>

</AbsoluteLayout>

</RelativeLayout>
What are your thoughts?

Also, yes I know there are some empty spaces, I just haven't gotten around to writing those parts yet.
 
I'mmmmmmmmm still not getting it.
I tried adding that in to my manifest, but it complained saying that the attribute name was already specified.
Just curious though, why would that throw a NullPointerException? The exception is thrown only on line 86. I've tried commenting out all other stuff in there, so I know that the exception is thrown on initialisation. Not only that, but the only difference between the button throwing the exception and the other buttons is that this one is from settings.xml, but it is still referenced correctly under R.id. Without that button, the main activity loads fully without any exceptions being thrown :S
 
Upvote 0
I think I see the problem,

when you call BackButton = (Button)findViewById(R.id.Back);
It probably won't find the Button called "Back" - because per default it only checks in the activity_main xml file.

This will set BackButton to null. And when you try to add listener to a null value it throws out the error!

if you want to access the Back button you need to inflate the other document.
http://developerlife.com/tutorials/?p=303
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones