Sometimes I'm receiving crash reports from google caused by random NullPointerExceptions
(see above). I tried to reproduce those errors but I'm not able to catch them.
Examples of NPE I get :
Caused by: java.lang.NullPointerException
at com.gamequiz.databasemanager.CategoryManager.getAllCategories(CategoryManager.java:28)
Caused by: java.lang.NullPointerException
at com.gamequiz.databasemanager.QuestionManager.getQuestionsFromLevel(QuestionManager.java:30)
at com.gamequiz.databasemanager.QuestionManager.getNumberOfQuestionAnsweredFromLevel(QuestionManager.java:148)
I though that my
dbHelper
variable is null
sometimes but I can't figure out why.
Since I don't know how to resolve that, I post the all steps of my code :
First of all I initialize all the managers in the LaunchActivity
:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_launch);
initializeAllManagers();
//some stuff
}
public void initializeAllManagers(){
InitializeAllManagers.init(getApplicationContext());
}
In my InitializeAllManagers
class, I set all the managers I need for the lifecycle of the app :
public class InitializeAllManagers {
public static void init(Context context){
DatabaseManager.init(context);
CategoryManager.init(DatabaseManager.getInstance().getHelper());
//and others initializations
}
}
DatabaseManager
class (initialization of the dbManager and dbHelper) :
public class DatabaseManager {
private static DatabaseManager instance;
private DatabaseHelper helper;
public static void init(Context ctx) {
if (instance==null) {
instance = new DatabaseManager(ctx);
}
}
public static DatabaseManager getInstance() {
return instance;
}
private DatabaseManager(Context ctx) {
helper = new DatabaseHelper(ctx);
}
public DatabaseHelper getHelper() {
return helper;
}
}
Finally there is an example of one manager :
public class CategoryManager {
private static DatabaseHelper dbHelper;
public static void init(DatabaseHelper dbHelperInstance) {
dbHelper = dbHelperInstance;
}
public static ArrayList <Category> getAllCategories(){
ArrayList <Category> cList = null;
try {
cList = (ArrayList<Category>) dbHelper.getCategoryDao().queryForAll();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return cList;
}
}
So I suspect that my dbHelper
variable is null
sometimes. Does anyone have an idea about how I can solve this issue ?
EDIT :
NPE mainly refers to this lines :
cList = (ArrayList<Category>) dbHelper.getCategoryDao().queryForAll();
Dao <Question, Long> questionDao = dbHelper.getQuestionDao();
That's why I suspect that dbHelper
is null
sometimes, and apparently crashes occurs when the app is sleeping for a moment (see feedback above).
Feedback of one user :
So, mainly if I leave the app without exiting it, the app will often crash when I try to go back to it. Sometimes I just get sent back to the menu, but mostly just all the way out of the app and I have to restart it to continue.