In my Android project, I have a Service
:
public class MyService extends Service{
//I defined a explicite contructor
public MyService(){
//NullPointerException here, why?
File dir = getDir("my_dir", Context.MODE_PRIVATE);
}
@Override
public void onCreate(){
super.onCreate();
...
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
...
}
}
I know normally, I shouldn't explicitly define a constructor for Android Service
. But in my case, I want to use the constructor to create a directory in my internal storage.
When I try it, I got NullPointerException, which I don't understand why? Could anyone explain to me the reason why I got NullPoineterException when call getDir(...)
in my explicite constructor ?