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 9076221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:03:51+00:00 2026-06-16T19:03:51+00:00

I am new to programming and I have spent two days trying to figure

  • 0

I am new to programming and I have spent two days trying to figure out what is wrong with my program. I have learnt a lot trying to fix this but I am not getting anywhere.

I couldn’t figure out how to copy the error in logcat in eclipse so I will type out the error:

 Caused by: java.lang.NullPointerException at com.deweyx.MainActivity.OnCreate(MainActivity.java:27)

Here is my code:

public class MainActivity extends Activity {

    int counter;
    Button add, sub;
    TextView display;

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

        counter = 0;
        add = (Button) findViewById(R.id.bAdd);
        sub = (Button) findViewById(R.id.bSub);
        display = (TextView) findViewById(R.id.xDisplay);

        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter++;
                display.setText("Your total is " + counter);            
            }
        });

        sub.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter--;
                display.setText("Your total is " + counter);
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

This is the xml folder:

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



<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/display" 
    android:layout_gravity="center"
    android:textSize="40sp"
    android:id="@+id/xDisplay"
/>


<Button
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:text="@string/padd"
    android:layout_gravity="center"
    android:textSize="20sp"
    android:id="@+id/bAdd"
 />


<Button
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:text="@string/psub"
    android:layout_gravity="center"
    android:textSize="20sp" 
    android:id="@+id/bSub"
/>

</LinearLayout>

logcat:

   12-19 02:11:36.423: D/AndroidRuntime(531): Shutting down VM
12-19 02:11:36.423: W/dalvikvm(531): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
12-19 02:11:36.444: E/AndroidRuntime(531): FATAL EXCEPTION: main
12-19 02:11:36.444: E/AndroidRuntime(531): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.deweyx/com.deweyx.MainActivity}: java.lang.NullPointerException
12-19 02:11:36.444: E/AndroidRuntime(531):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
12-19 02:11:36.444: E/AndroidRuntime(531):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-19 02:11:36.444: E/AndroidRuntime(531):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-19 02:11:36.444: E/AndroidRuntime(531):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-19 02:11:36.444: E/AndroidRuntime(531):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 02:11:36.444: E/AndroidRuntime(531):  at android.os.Looper.loop(Looper.java:137)
12-19 02:11:36.444: E/AndroidRuntime(531):  at android.app.ActivityThread.main(ActivityThread.java:4424)
12-19 02:11:36.444: E/AndroidRuntime(531):  at java.lang.reflect.Method.invokeNative(Native Method)
12-19 02:11:36.444: E/AndroidRuntime(531):  at java.lang.reflect.Method.invoke(Method.java:511)
12-19 02:11:36.444: E/AndroidRuntime(531):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-19 02:11:36.444: E/AndroidRuntime(531):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-19 02:11:36.444: E/AndroidRuntime(531):  at dalvik.system.NativeStart.main(Native Method)
12-19 02:11:36.444: E/AndroidRuntime(531): Caused by: java.lang.NullPointerException
12-19 02:11:36.444: E/AndroidRuntime(531):  at com.deweyx.MainActivity.onCreate(MainActivity.java:27)
12-19 02:11:36.444: E/AndroidRuntime(531):  at android.app.Activity.performCreate(Activity.java:4465)
12-19 02:11:36.444: E/AndroidRuntime(531):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-19 02:11:36.444: E/AndroidRuntime(531):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
12-19 02:11:36.444: E/AndroidRuntime(531):  ... 11 more
12-19 02:11:36.653: I/dalvikvm(531): threadid=3: reacting to signal 3
12-19 02:11:36.673: I/dalvikvm(531): Wrote stack traces to '/data/anr/traces.txt'
12-19 02:11:36.983: I/dalvikvm(531): threadid=3: reacting to signal 3
12-19 02:11:36.993: I/dalvikvm(531): Wrote stack traces to '/data/anr/traces.txt'
12-19 02:11:52.452: I/dalvikvm(551): threadid=3: reacting to signal 3
12-19 02:11:52.502: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
12-19 02:11:52.783: D/AndroidRuntime(551): Shutting down VM
12-19 02:11:52.783: W/dalvikvm(551): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
12-19 02:11:52.793: E/AndroidRuntime(551): FATAL EXCEPTION: main
12-19 02:11:52.793: E/AndroidRuntime(551): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.deweyx/com.deweyx.MainActivity}: java.lang.NullPointerException
12-19 02:11:52.793: E/AndroidRuntime(551):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
12-19 02:11:52.793: E/AndroidRuntime(551):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-19 02:11:52.793: E/AndroidRuntime(551):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-19 02:11:52.793: E/AndroidRuntime(551):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-19 02:11:52.793: E/AndroidRuntime(551):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 02:11:52.793: E/AndroidRuntime(551):  at android.os.Looper.loop(Looper.java:137)
12-19 02:11:52.793: E/AndroidRuntime(551):  at android.app.ActivityThread.main(ActivityThread.java:4424)
12-19 02:11:52.793: E/AndroidRuntime(551):  at java.lang.reflect.Method.invokeNative(Native Method)
12-19 02:11:52.793: E/AndroidRuntime(551):  at java.lang.reflect.Method.invoke(Method.java:511)
12-19 02:11:52.793: E/AndroidRuntime(551):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-19 02:11:52.793: E/AndroidRuntime(551):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-19 02:11:52.793: E/AndroidRuntime(551):  at dalvik.system.NativeStart.main(Native Method)
12-19 02:11:52.793: E/AndroidRuntime(551): Caused by: java.lang.NullPointerException
12-19 02:11:52.793: E/AndroidRuntime(551):  at com.deweyx.MainActivity.onCreate(MainActivity.java:27)
12-19 02:11:52.793: E/AndroidRuntime(551):  at android.app.Activity.performCreate(Activity.java:4465)
12-19 02:11:52.793: E/AndroidRuntime(551):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-19 02:11:52.793: E/AndroidRuntime(551):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
12-19 02:11:52.793: E/AndroidRuntime(551):  ... 11 more
12-19 02:11:52.963: I/dalvikvm(551): threadid=3: reacting to signal 3
12-19 02:11:52.963: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
12-19 02:11:53.343: I/dalvikvm(551): threadid=3: reacting to signal 3
12-19 02:11:53.353: I/dalvikvm(551): Wrote stack traces to '/data/anr/traces.txt'
  • 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-06-16T19:03:52+00:00Added an answer on June 16, 2026 at 7:03 pm

    Because it crashes on the findViewById when looking for the id bAdd, this is probably because that id is not defined yet. Are you sure you are using the correct xml? What I mean is, are you sure you’re using activity_main.xml as the XML layout? Because maybe you are looking in the wrong XML file, it has happened to me 🙂

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

Sidebar

Related Questions

i am new kernel programming.i have been trying to load this driver program for
I'm pretty new in iphone programming and have stumbled upon this issue which I
I am new to programming. I have been trying to write a function in
I'm new to programming and have spent the past few weeks studying python on
I've spent over a week trying to figure out a way to do a
I'm very much new to programming and have been doing fairly well so far.
I am new to web programming...I have been asked to create a simple Internet
I'm new to PHP programming and have poor knowledge about it, but I want
I am very new to python programming and have yet to buy a textbook
I am just new to programming in Unix and have a small issue that

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.