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!

FeliceM
  • 4,163
  • 9
  • 48
  • 75
DevoidSylph
  • 55
  • 1
  • 2
  • 7
  • 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. – DevoidSylph Nov 13 '13 at 22:50
  • 1
    Here is [a question](http://stackoverflow.com/q/218384/778118) that discusses NullPointerExceptions. Here is [a question](http://stackoverflow.com/q/3988788/7781180) 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. – jahroy Nov 14 '13 at 05:36

1 Answers1

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");
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • 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! – DevoidSylph Nov 13 '13 at 22:51
  • @BhaveshNai before trying to access the variable. Such as in `onCreate()` right before `startMonitor.setClassName()` – codeMagic Dec 18 '14 at 15:41