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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:57:56+00:00 2026-06-13T17:57:56+00:00

I am trying to write my first Android app, and I’m having trouble. I

  • 0

I am trying to write my first Android app, and I’m having trouble. I wrote a simple Java program to find the factors of a number provided by the user, and I’d like to port it over to Java. I have an XML file for the interface, which seemed to run OK until I added the first Java class. Now it won’t run, and log.cat says that there’s a null pointer exception. So far, I only have stubs, but it seems like it should run OK with what I’ve done to this point. I’m including the log.cat text, but I’m too new it this to make much sense out of it.

There may be more than one problem. After adding the Java file I began to get run-time errors right away, but I don’t think they were null pointer exceptions. I think that started when I made changes to fix what was already causing problems.

Any help is appreciated.

Here is the Java file:

import android.app.Activity;  <br>
import android.os.Bundle;  <br>
import android.widget.Button;   <br>
import android.widget.EditText;  <br>
import android.widget.TextView;  <br>

public class AndroidFactoringActivity extends Activity {

    // Instance Variables
    EditText userNumber;
    Button factorButton;
    TextView resultsField;
    int factorResults;

    /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    factorButton = (Button) findViewById(R.id.factorButton);
    userNumber = (EditText) findViewById(R.id.userNumber);
    factorResults = 1;
    resultsField.setText(String.valueOf(factorResults));
}   
}

Here is main.xml:

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

    <TextView
        android:id="@+id/askField"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/ask"
        android:textSize="24dp" />

    <EditText
        android:id="@+id/userNumber"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/factorButton"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/click" />

    <TextView
        android:id="@+id/resultsField"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/tell"
        android:textSize="24dp" />
</LinearLayout>

Here are the log.cat results:

 03-31 23:58:53.579: D/AndroidRuntime(2804): Shutting down VM

    03-31 23:58:53.589: W/dalvikvm(2804): threadid=3: thread exiting with uncaught exception (group=0x4001b188)

    03-31 23:58:53.589: E/AndroidRuntime(2804): Uncaught handler: thread main exiting due to uncaught exception

    03-31 23:58:53.650: E/AndroidRuntime(2804): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.dave_b.factoring/net.dave_b.factoring.AndroidFactoringActivity}: java.lang.NullPointerException

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at android.os.Handler.dispatchMessage(Handler.java:99)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at android.os.Looper.loop(Looper.java:123)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at android.app.ActivityThread.main(ActivityThread.java:4363)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at java.lang.reflect.Method.invokeNative(Native Method)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at java.lang.reflect.Method.invoke(Method.java:521)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at dalvik.system.NativeStart.main(Native Method)

    03-31 23:58:53.650: E/AndroidRuntime(2804): Caused by: java.lang.NullPointerException

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at net.dave_b.factoring.AndroidFactoringActivity.onCreate(AndroidFactoringActivity.java:26)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)

    03-31 23:58:53.650: E/AndroidRuntime(2804):     ... 11 more

    03-31 23:58:53.679: I/dalvikvm(2804): threadid=7: reacting to signal 3

    03-31 23:58:53.679: E/dalvikvm(2804): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

    03-31 23:59:57.629: I/Process(2804): Sending signal. PID: 2804 SIG: 9

    04-01 00:07:36.129: D/AndroidRuntime(3040): Shutting down VM

    04-01 00:07:36.129: W/dalvikvm(3040): threadid=3: thread exiting with uncaught exception (group=0x4001b188)

    04-01 00:07:36.139: E/AndroidRuntime(3040): Uncaught handler: thread main exiting due to uncaught exception

    04-01 00:07:36.159: E/AndroidRuntime(3040): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.dave_b.factoring/net.dave_b.factoring.AndroidFactoringActivity}: java.lang.NullPointerException

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at android.os.Handler.dispatchMessage(Handler.java:99)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at android.os.Looper.loop(Looper.java:123)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at android.app.ActivityThread.main(ActivityThread.java:4363)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at java.lang.reflect.Method.invokeNative(Native Method)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at java.lang.reflect.Method.invoke(Method.java:521)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at dalvik.system.NativeStart.main(Native Method)

    04-01 00:07:36.159: E/AndroidRuntime(3040): Caused by: java.lang.NullPointerException

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at net.dave_b.factoring.AndroidFactoringActivity.onCreate(AndroidFactoringActivity.java:26)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)

    04-01 00:07:36.159: E/AndroidRuntime(3040):     ... 11 more

    04-01 00:07:36.199: I/dalvikvm(3040): threadid=7: reacting to signal 3

    04-01 00:07:36.199: E/dalvikvm(3040): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

    04-01 00:07:40.329: I/Process(3040): Sending signal. PID: 3040 SIG: 9
  • 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-13T17:57:57+00:00Added an answer on June 13, 2026 at 5:57 pm

    You got a null pointer because you didnt intialise the variable resultsField (you just declared it) .

    As you used

    userNumber = (EditText) findViewById(R.id.userNumber);

    you must even initialise the reference resultsField.
    This can be done using

    resultsField= (TextView ) findViewById(R.id.resultsField);

    in the onCreate() after the statement setContentView()

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

Sidebar

Related Questions

I'm trying to write my first Android app. It will take a number input
I'm trying to write my first simple mvc app. I have a Main View
Trying to write my first app on android. But the app crashes(FC) on the
I am trying to write my first app- a simple application where the user
I'm trying to implement my first android Program. It should write calendar entries (I
I'm trying to write a very simple app for Android. It should do 2
Just trying to write my first ever WP7 app. I have several hundreds of
I'm trying to write my first iPhone app, and I'm using a date picker
I am trying to write an Android app to connect to an existing web
I'm having to think hard about optimisation in an Android app for the first

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.