Android - getapplicationinfo() nullpointerexception

6.4k views Asked by At

My app should just access and display a sql database. When i try to run it on the emulator the app stopps und the logcat says the following:

12-14 17:15:14.330: D/AndroidRuntime(3558): Shutting down VM
12-14 17:15:14.389: E/AndroidRuntime(3558): FATAL EXCEPTION: main
12-14 17:15:14.389: E/AndroidRuntime(3558): Process: com.wiemann.databaseapp, PID: 3558
12-14 17:15:14.389: E/AndroidRuntime(3558): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.wiemann.databaseapp/com.wiemann.databaseapp.DatabaseListActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
12-14 17:15:14.389: E/AndroidRuntime(3558):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
12-14 17:15:14.389: E/AndroidRuntime(3558):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
12-14 17:15:14.389: E/AndroidRuntime(3558):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
12-14 17:15:14.389: E/AndroidRuntime(3558):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
12-14 17:15:14.389: E/AndroidRuntime(3558):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-14 17:15:14.389: E/AndroidRuntime(3558):     at android.os.Looper.loop(Looper.java:135)
12-14 17:15:14.389: E/AndroidRuntime(3558):     at android.app.ActivityThread.main(ActivityThread.java:5221)
12-14 17:15:14.389: E/AndroidRuntime(3558):     at java.lang.reflect.Method.invoke(Native Method)
12-14 17:15:14.389: E/AndroidRuntime(3558):     at java.lang.reflect.Method.invoke(Method.java:372)
12-14 17:15:14.389: E/AndroidRuntime(3558):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-14 17:15:14.389: E/AndroidRuntime(3558):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-14 17:15:14.389: E/AndroidRuntime(3558): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference

Ok, so this .getapplicationinfo method seems trying to access a null object. I thought it must be something with my manifest xml, but i can´t figure out where the problem could be. My problem is that normally the stacktrace refers to a specific line, but not here. I´ve been sitting here for hours and i´m not getting what it could be.

Here is my manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.wiemann.databaseapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".DatabaseListActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

And here is the code of my main Activity, which keeps on crashing:

package com.wiemann.databaseapp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;

public class DatabaseListActivity extends ListActivity {
    DatabaseHelper dbHelper = new DatabaseHelper(this);
    SQLiteDatabase database = null;
    Cursor dbCursor;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_database_list);
        queryDataFromDatabase();
    }

    public void queryDataFromDatabase() {
        try {
            dbHelper.createDataBase();
        } catch (IOException ioe) {
        }
        List<String> list_values = new ArrayList<String>();
        try {
            database = dbHelper.getDataBase();
            dbCursor = database.rawQuery("SELECT university FROM db_table;",
                    null);

            dbCursor.moveToFirst();
            int index = dbCursor.getColumnIndex("university");
            while (!dbCursor.isAfterLast()) {
                String record = dbCursor.getString(index);
                list_values.add(record);
                dbCursor.moveToNext();
            }
        } finally {
            if (database != null) {
                dbHelper.close();
            }
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.list_item, list_values);
        setListAdapter(adapter);
    }

}

Here the code of the database helping class:

package com.wiemann.databaseapp;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

public class DatabaseHelper extends SQLiteAssetHelper {
    private static String DB_PATH;
    private static String DB_PATH_PREFIX = "/data/data/";
    private static String DB_PATH_SUFFIX = "/databases/";
    private static String DB_NAME = "exercise_database.db";
    private SQLiteDatabase myDataBase;
    private final Context myContext;

    public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, 1);
        this.myContext = context;
    }

    public void createDataBase() throws IOException {
        DB_PATH = DB_PATH_PREFIX + myContext.getPackageName() + DB_PATH_SUFFIX
                + DB_NAME;
        boolean dbExist = checkDataBase();
        SQLiteDatabase db_Read = null;
        if (dbExist) {
        } else {
            db_Read = this.getReadableDatabase();
            db_Read.close();
        }
    }

    private boolean checkDataBase() {
        SQLiteDatabase checkDB = null;
        try {
            checkDB = SQLiteDatabase.openDatabase(DB_PATH, null,
                    SQLiteDatabase.NO_LOCALIZED_COLLATORS);
            checkDB.close();
            return true;
        } catch (SQLiteException e) {
            return false;
        }
    }

    private void copyDataBase() throws IOException {
        InputStream assetsDB = myContext.getAssets().open(DB_NAME);
        File directory = new File(DB_PATH);
        if (directory.exists() == false) {
            directory.mkdir();
        }
        OutputStream dbOut = new FileOutputStream(DB_PATH);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = assetsDB.read(buffer)) > 0) {
            dbOut.write(buffer, 0, length);
        }
        dbOut.flush();
        dbOut.close();
    }

    public void openDataBase() throws SQLException {
        myDataBase = SQLiteDatabase.openDatabase(DB_PATH, null,
                SQLiteDatabase.NO_LOCALIZED_COLLATORS);
    }

    public SQLiteDatabase getDataBase() throws SQLException {
        myDataBase = SQLiteDatabase.openDatabase(DB_PATH, null,
                SQLiteDatabase.NO_LOCALIZED_COLLATORS);
        return myDataBase;
    }

    @Override
    public synchronized void close() {
        if (myDataBase != null) {
            myDataBase.close();
        }
        super.close();
    }

    //@Override
    //public void onCreate(SQLiteDatabase db) {
    //}

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}
0

There are 0 answers