0

It's apparently a question that has been asked multiple times, but only has solutions on a case-by-case basis. I'm designing an application called Trackr, and all it does is opening a service when a button is pressed. I keep getting this error:

11-13 16:44:06.546: D/ActivityThread(4656): setTargetHeapUtilization:0.25
11-13 16:44:06.546: D/ActivityThread(4656): setTargetHeapIdealFree:8388608
11-13 16:44:06.546: D/ActivityThread(4656): setTargetHeapConcurrentStart:2097152
11-13 16:44:06.926: W/dalvikvm(4656): threadid=1: thread exiting with uncaught exception (group=0x41225438)
11-13 16:44:06.936: E/AndroidRuntime(4656): FATAL EXCEPTION: main
11-13 16:44:06.936: E/AndroidRuntime(4656): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.example.trackr/com.example.trackr.StartScreen}: java.lang.NullPointerException
11-13 16:44:06.936: E/AndroidRuntime(4656): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
11-13 16:44:06.936: E/AndroidRuntime(4656): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
11-13 16:44:06.936: E/AndroidRuntime(4656): at android.app.ActivityThread.access$700(ActivityThread.java:143)
11-13 16:44:06.936: E/AndroidRuntime(4656): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
11-13 16:44:06.936: E/AndroidRuntime(4656): at android.os.Handler.dispatchMessage(Handler.java:99)
11-13 16:44:06.936: E/AndroidRuntime(4656): at android.os.Looper.loop(Looper.java:137)
11-13 16:44:06.936: E/AndroidRuntime(4656): at android.app.ActivityThread.main(ActivityThread.java:4962)
11-13 16:44:06.936: E/AndroidRuntime(4656): at java.lang.reflect.Method.invokeNative(Native Metood)
11-13 16:44:06.936: E/AndroidRuntime(4656): at java.lang.reflect.Method.invoke(Method.java:511)
11-13 16:44:06.936: E/AndroidRuntime(4656): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
11-13 16:44:06.936: E/AndroidRuntime(4656): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
11-13 16:44:06.936: E/AndroidRuntime(4656): at dalvik.system.NativeStart.main(Native Method)
11-13 16:44:06.936: E/AndroidRuntime(4656): Caused by: java.lang.NullPointerException
11-13 16:44:06.936: E/AndroidRuntime(4656): at com.example.trackr.StartScreen.onCreate(StartScreen.java:17)
11-13 16:44:06.936: E/AndroidRuntime(4656): at android.app.Activity.performCreate(Activity.java:5160)
11-13 16:44:06.936: E/AndroidRuntime(4656): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
11-13 16:44:06.936: E/AndroidRuntime(4656): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
11-13 16:44:06.936: E/AndroidRuntime(4656): ... 11 more

I have 2 classes, StartScreen(an activity) and StartService(a service). I also have a Manifest for the app and layout files for each one.

package com.example.trackr;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class StartScreen extends Activity {

    Intent startMonitor;
    Button newBut;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_screen);
        startMonitor.setClassName("com.example.trackr","StartService");

    }

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

    public void callService(View view){
        startService(startMonitor);
    }
}

This is the layout file:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".StartScreen" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="90dp"
        android:onClick="callService"
        android:text="Enable" />

</RelativeLayout>

And then this is the manifest file:

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.trackr.StartScreen"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name="com.example.trackr.StartService"></service>

</application>

</manifest>

I know it may seem like an easy question, but it's got me stumped. I would really appreciate some help. Thanks!

2
  • In the future, suggesting help is a lot better. Saying you need to know this is unhelpful if I've already said that I do not know this. Commented Nov 13, 2013 at 22:50
  • 1
    Here is a question that discusses NullPointerExceptions. Here is a question that discusses how to read a stack trace. When you get a NullPointerException, the stack trace tells you the exact line where the Exception was thrown. If you look at the line in question, the cause is usually pretty easy to identify. Commented Nov 14, 2013 at 5:36

1 Answer 1

6

You are getting NPE here

startMonitor.setClassName("com.example.trackr","StartService");

because startMonitor has not been initialized. Change it to

startMonitor = new Intent();  // initialize your variable before trying to use it
startMonitor.setClassName("com.example.trackr","StartService");
Sign up to request clarification or add additional context in comments.

2 Comments

I knew it. I know how to do initialize variables normally, and I was afraid that the problem with this code would be something I already knew how to do. Thank you very much for your help!
@BhaveshNai before trying to access the variable. Such as in onCreate() right before startMonitor.setClassName()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.