LoneWolfPR
LoneWolfPR

Reputation: 4090

Android: Unable to instantiate application NullPointerException

I've got a class that I'm using to extend application. This is the code for it:

package com.example.myapp

public class MainApplication extends Application {
    public ArrayList<Level> remoteLevels = new ArrayList<Level>();
    public ArrayList<Level> localLevels = new ArrayList<Level>();
    public TCMSQLiteHelper sqliteHelper = new TCMSQLiteHelper(this.getApplicationContext());

    @Override
    public void onCreate() {
        super.onCreate();
        this.loadRemoteLevels();
        localLevels = sqliteHelper.getAllLevels();
        if(localLevels.size()>0){
            Log.d("DEBUG","Explore Data Found");
        } else {
            Log.d("DEBUG","No Explore Data Found");
        }
    }
}

The manifest has the following entry

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/TCMTheme"
    android:name="MainApplication">

I'm getting the error java.lang.RuntimeException: Unable to instantiate application com.example.myapp.MainApplication: java.lang.NullPointerException

What am I missing?

Upvotes: 0

Views: 159

Answers (1)

Maxim Shoustin
Maxim Shoustin

Reputation: 77910

From your code, you define TCMSQLiteHelper before initiated the class

public TCMSQLiteHelper sqliteHelper = new TCMSQLiteHelper(this.getApplicationContext());
                                                           ^^^

Try this way:

public TCMSQLiteHelper sqliteHelper;

...

 @Override
public void onCreate() {

...
sqliteHelper = new TCMSQLiteHelper(this.getApplicationContext());

Upvotes: 1

Related Questions