Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7511427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:32:38+00:00 2026-05-29T23:32:38+00:00

I sometimes get a null pointer exception with my code. Oddly enough, this does

  • 0

I sometimes get a null pointer exception with my code. Oddly enough, this does not happen every time, and I’m not sure why. Help?

package net.obviam.droidz;
public class HomeView extends Activity {
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);



     setContentView(R.layout.menu);



    Button page1 = (Button) findViewById(R.id.Button01);
         page1.setOnClickListener(new View.OnClickListener() {
                 public void onClick(View view) {
//                   Intent i = getIntent();
                     Intent myIntent = new Intent(view.getContext(), DroidzActivity.class);
                     startActivityForResult(myIntent, 0);
                 }

     });


}



protected void onDestroy() {
//      Log.d(TAG, "Destroying..");
        super.onDestroy();
    }
    @Override
    protected void onStop() {
//      Log.d(TAG, "Stopping...");
        super.onStop();
    }

}

The logcat says that the problem is with line 19, the setOnClickListener. I’m not sure what’s wrong with it though. It doesn’t appear to be referencing anything.

The Logcat reads like this:

02-17 16:52:13.816: E/AndroidRuntime(23756): FATAL EXCEPTION: main
02-17 16:52:13.816: E/AndroidRuntime(23756): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.obviam.droidz/net.obviam.droidz.HomeView}: java.lang.NullPointerException
02-17 16:52:13.816: E/AndroidRuntime(23756):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at android.os.Looper.loop(Looper.java:130)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at android.app.ActivityThread.main(ActivityThread.java:3687)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at java.lang.reflect.Method.invokeNative(Native Method)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at java.lang.reflect.Method.invoke(Method.java:507)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at dalvik.system.NativeStart.main(Native Method)
02-17 16:52:13.816: E/AndroidRuntime(23756): Caused by: java.lang.NullPointerException
02-17 16:52:13.816: E/AndroidRuntime(23756):    at net.obviam.droidz.HomeView.onCreate(HomeView.java:19)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-17 16:52:13.816: E/AndroidRuntime(23756):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
02-17 16:52:13.816: E/AndroidRuntime(23756):    ... 11 more

And here is the manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.obviam.droidz"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />

<application
    android:icon="@drawable/images"
    android:label="@string/app_name" android:debuggable="true">
    <activity android:name=".HomeView" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".DroidzActivity"></activity>

</application>

And here is the xml file that the button is found in:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

 <Button android:text="Start Game!"
        android:id="@+id/Button01"
        android:layout_width="250dp"
        android:textSize="18dp"
        android:layout_height="55dp">
    </Button>



</LinearLayout>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:32:40+00:00Added an answer on May 29, 2026 at 11:32 pm

    After reading through the stack trace provided, I think the problem is when you start the new intent, you to pass the Activity’s context, NOT the View’s:

    Button page1 = (Button) findViewById(R.id.Button01);
    page1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent myIntent = new Intent(getContext(), DroidzActivity.class); // fix View.getContext() to getContext()
            startActivity(myIntent);    // change to startActivity
        }
    });
    

    As to why this is an intermitent problem, I have no idea.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sometimes I get a broken background in Chrome. I do not get this error
Hit refresh several times and see sometimes I get null. This script loops through
Sometimes when i run this code I get a process and id and sometimes
I'm using GLUTess to tesselate polygons. Sometimes it crashes with a null pointer issue
Some times when running my app I get a null pointer when retrieving a
Sometimes I get an error or an unhandled exception in a line in the
Sometimes I get a dash in output but I was told it's not a
Sometimes we put some debug prints in our code this way printf(successfully reached at
When running my application I sometimes get an error about too many files open
We have some applications that sometimes get into a bad state, but only in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.