I have my BaseApplication lets say which looks like
public class ApplicationBase extends Application {
String someKey;
public String getSomeKey() {
return someKey;
}
public void setSomeKey(String someKey) {
this.someKey = someKey;
}
}
I have a fragment It performs some actions and decides on the basis of
String key = (ApplicationBase) getActivity().getApplication()).getSomeKey();
if(key.equals(anotherString){
Do Some thing
...
}else{
Do Some thing
....
}
It run smoothly but sometime (rare case) It crashes with this error
java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.Application android.support.v4.app.FragmentActivity.getApplication()' on a null object reference
How to solve? (I tried my best to keep this question universal not personal so that another coder relates this question with his problem So please Dont downvote :p)
Or Can I do this to prevent this error?
if((ApplicationBase) getActivity().getApplication() !=null){
String key = (ApplicationBase) getActivity().getApplication()).getSomeKey();
if(key.equals(anotherString){
Do Some thing
...
}else{
Do Some thing
....
}
}