-1

I' m a complete newcome to android and java and I'm writing an android app with android studio that should show some points on a map each 5 seconds (it's just a test). When I run the app on the emulator I get this on the logcat:

03-13 13:22:28.373    2964-2964/com.example.francesca.geoapp      

E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.francesca.geoapp, PID: 2964
java.lang.RuntimeException: Unable to instantiate service   com.example.francesca.geoapp.DataService: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:2716)
        at android.app.ActivityThread.access$1800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1361)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
        at  android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:105)
        at  android.support.v4.content.LocalBroadcastManager.getInstance(LocalBroadcastManag er.java:102) at com.example.francesca.geoapp.DataService.<init> (DataService.java:56)
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.Class.newInstance(Class.java:1572)
        at   android.app.ActivityThread.handleCreateService(ActivityThread.java:2713)
            at android.app.ActivityThread.access$1800(ActivityThread.java:144)
            at      android.app.ActivityThread$H.handleMessage(ActivityThread.java:1361)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

the intentService DataService code is

public class DataService extends IntentService {

private LocalBroadcastManager BroadcastNotifier ;
private Timer timer = new Timer();
private List dataList  = new ArrayList<testData>();
int i = 0;

//constructor
public DataService() {
    super("DataService");

    dataList.add(new testData(new LatLng(52.519804, 13.404988), new LatLng(52.519125, 13.406061), 50, 30));
    dataList.add(new testData(new LatLng(52.520692, 13.403722), new LatLng(52.519804, 13.404988), 60, 40));
    dataList.add(new testData(new LatLng(52.522023, 13.404730), new LatLng(52.520692, 13.403722), 50, 30));
    dataList.add(new testData(new LatLng(52.523388, 13.407143), new LatLng(52.522023, 13.404730), 40, 20));
    dataList.add(new testData(new LatLng(52.522911, 13.408807), new LatLng(52.523388, 13.407143), 80, 30));
    dataList.add(new testData(new LatLng(52.523982, 13.409623), new LatLng(52.522911, 13.408807), 50, 40));
    dataList.add(new testData(new LatLng(52.524713, 13.407434), new LatLng(52.523982, 13.409623), 30, 50));

    BroadcastNotifier = LocalBroadcastManager.getInstance(this);
    timer.scheduleAtFixedRate(task, 0, 5000 );
}

protected void onHandleIntent(Intent i) {

}

//timerTask to send request to the webservice
TimerTask task = new TimerTask() {
    @Override
    public void run() {
        if(i <7 ) {
            testData test = (testData) dataList.get(i);
            data.setCurrentPosition(test.currentPosition);
            data.setLatestPosition(test.latestPosition);
            data.setSpeed(test.speed);
            data.setMediumSpeed(test.mediumSpeed);
            i++;
        }
    }
};


//method to tell subscribers that a newData has arrived
private void OnNewDataSeen() {
    Intent localIntent = new Intent();
    localIntent.setAction("com.example.android.threadsample.BROADCAST");

    localIntent.addCategory(Intent.CATEGORY_DEFAULT);

    // Broadcasts the Intent
    BroadcastNotifier.sendBroadcast(localIntent);
}

}

And the MenuActivity that should start the intentService is

public class MenuActivity extends ActionBarActivity {

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

    Intent mServiceIntent = new Intent(this, DataService.class);
    this.startService(mServiceIntent);
}

public void goToMap(View view){
    Intent intent = new Intent(this, MapActivity.class);
    startActivity(intent);
}
public void goToData(View view){
    Intent intent = new Intent(this, ShowDataActivity.class);
    startActivity(intent);
}

}

What have I done wrong?

  • asked many times, you are trying to use context before it is fully created (in constructor) – Selvin Mar 13 '15 at 14:26

2 Answers2

4

You can't have this

BroadcastNotifier = LocalBroadcastManager.getInstance(this);

in you constructor. Put it into the onHandleIntent()

this isn't fully initialised in your constructor and a Context cannot be obtained, which it why it is null and you get that NPE.

ci_
  • 8,594
  • 10
  • 39
  • 63
0

You should leave constructor with super call only and use onCreate lifecycle callback method.

Mohsenme
  • 1,012
  • 10
  • 20