Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

In the code I have listed below, the method getPieceTextFile() should return the InputStream to read data from the specified file. However, the method always falls back on the catch and so returns null, leading to a NPE.

I have listed the LogCat output and the code for the class below. Any identification of the potential source of the problem would be appreciated.

LogCat:

01-03 12:17:51.331: I/dalvikvm(589): threadid=3: reacting to signal 3
01-03 12:17:51.590: I/dalvikvm(589): Wrote stack traces to '/data/anr/traces.txt'
01-03 12:17:51.831: I/dalvikvm(589): threadid=3: reacting to signal 3
01-03 12:17:51.871: I/dalvikvm(589): Wrote stack traces to '/data/anr/traces.txt'
01-03 12:17:55.081: I/System.out(589): Asset Manager/IS not work.
01-03 12:17:55.081: W/System.err(589): java.lang.NullPointerException
01-03 12:17:55.091: W/System.err(589):  at com.lawson.englishlitrevision.ManipulateDatabase.getPieceTextFile(ManipulateDatabase.java:387)
01-03 12:17:55.091: W/System.err(589):  at com.lawson.englishlitrevision.ManipulateDatabase.<init>(ManipulateDatabase.java:398)
01-03 12:17:55.091: W/System.err(589):  at com.lawson.englishlitrevision.Novel.onCreate(Novel.java:22)
01-03 12:17:55.091: W/System.err(589):  at android.app.Activity.performCreate(Activity.java:4465)
01-03 12:17:55.091: W/System.err(589):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-03 12:17:55.091: W/System.err(589):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
01-03 12:17:55.091: W/System.err(589):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-03 12:17:55.091: W/System.err(589):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-03 12:17:55.091: W/System.err(589):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-03 12:17:55.091: W/System.err(589):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 12:17:55.091: W/System.err(589):  at android.os.Looper.loop(Looper.java:137)
01-03 12:17:55.101: W/System.err(589):  at android.app.ActivityThread.main(ActivityThread.java:4424)
01-03 12:17:55.101: W/System.err(589):  at java.lang.reflect.Method.invokeNative(Native Method)
01-03 12:17:55.101: W/System.err(589):  at java.lang.reflect.Method.invoke(Method.java:511)
01-03 12:17:55.101: W/System.err(589):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-03 12:17:55.101: W/System.err(589):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-03 12:17:55.101: W/System.err(589):  at dalvik.system.NativeStart.main(Native Method)
01-03 12:17:55.101: W/dalvikvm(589): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
01-03 12:17:55.121: E/AndroidRuntime(589): FATAL EXCEPTION: main
01-03 12:17:55.121: E/AndroidRuntime(589): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lawson.englishlitrevision/com.lawson.englishlitrevision.Novel}: java.lang.NullPointerException
01-03 12:17:55.121: E/AndroidRuntime(589):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
01-03 12:17:55.121: E/AndroidRuntime(589):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
01-03 12:17:55.121: E/AndroidRuntime(589):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
01-03 12:17:55.121: E/AndroidRuntime(589):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
01-03 12:17:55.121: E/AndroidRuntime(589):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 12:17:55.121: E/AndroidRuntime(589):  at android.os.Looper.loop(Looper.java:137)
01-03 12:17:55.121: E/AndroidRuntime(589):  at android.app.ActivityThread.main(ActivityThread.java:4424)
01-03 12:17:55.121: E/AndroidRuntime(589):  at java.lang.reflect.Method.invokeNative(Native Method)
01-03 12:17:55.121: E/AndroidRuntime(589):  at java.lang.reflect.Method.invoke(Method.java:511)
01-03 12:17:55.121: E/AndroidRuntime(589):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-03 12:17:55.121: E/AndroidRuntime(589):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-03 12:17:55.121: E/AndroidRuntime(589):  at dalvik.system.NativeStart.main(Native Method)
01-03 12:17:55.121: E/AndroidRuntime(589): Caused by: java.lang.NullPointerException
01-03 12:17:55.121: E/AndroidRuntime(589):  at java.io.Reader.<init>(Reader.java:64)
01-03 12:17:55.121: E/AndroidRuntime(589):  at java.io.InputStreamReader.<init>(InputStreamReader.java:79)
01-03 12:17:55.121: E/AndroidRuntime(589):  at com.lawson.englishlitrevision.ManipulateDatabase.<init>(ManipulateDatabase.java:398)
01-03 12:17:55.121: E/AndroidRuntime(589):  at com.lawson.englishlitrevision.Novel.onCreate(Novel.java:22)
01-03 12:17:55.121: E/AndroidRuntime(589):  at android.app.Activity.performCreate(Activity.java:4465)
01-03 12:17:55.121: E/AndroidRuntime(589):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-03 12:17:55.121: E/AndroidRuntime(589):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
01-03 12:17:55.121: E/AndroidRuntime(589):  ... 11 more
01-03 12:17:55.572: I/dalvikvm(589): threadid=3: reacting to signal 3
01-03 12:17:55.580: I/dalvikvm(589): Wrote stack traces to '/data/anr/traces.txt'
01-03 12:17:55.681: I/dalvikvm(589): threadid=3: reacting to signal 3
01-03 12:17:55.690: I/dalvikvm(589): Wrote stack traces to '/data/anr/traces.txt'

Class Code:

/**
 * Contains SQL statements and manages connections
 * 
 * @author Daniel Lawson
 * @version 29/12/13
 */
package com.lawson.englishlitrevision;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.res.AssetManager;

import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class ManipulateDatabase {

    /** String representing the name of the database */
    private static final String databaseName = "EnglishDatabase.db";

    /** Int representing the current version of the database */
    private static final int databaseVersion = 1;

    // ----------------------------------------------------------------------------------

    /**
     * String representing the table that stores the name of the piece and
     * whether it is a play or not.
     */
    private static final String pieceTable = "pieceTable";

    /**
     * String representing the table that stores the name of the point made,
     * which piece it belongs to, and which element it pertains to.
     */

    private static final String specificTable = "specificTable";

    /**
     * String representing the table that stores the element, the piece it
     * belongs to, and whether or not it is a character.
     */

    private static final String elementTable = "elementTable";

    /**
     * String representing the table that stores the quote and the point it
     * links to.
     */

    private static final String quoteTable = "quoteTable";

    /**
     * String representing the table that stores the piece and the
     * correct/incorrect responses on various dates
     */

    private static final String statsTable = "statsTable";

    // ----------------------------------------------------------------------------------

    /** String representing the piece field of the piece table */

    private static final String pieceField = "piece";

    /** String representing the type field of the piece table */

    private static final String typeField = "isPlay";

    /** String representing the point field of the specific table */

    private static final String pointField = "point";

    /** String representing the element field of the element table */

    private static final String elementField = "element";

    /** String representing the elType field of the element table */

    private static final String elTypeField = "isCharacter";

    /** String representing the quote field of the quote table */

    private static final String quoteField = "quote";

    /** String representing the ID field of the stats table */

    private static final String idField = "statId";

    /** String representing the correct field of the stats table */

    private static final String correctField = "correct";

    /** String representing the incorrect field of the stats table */

    private static final String incorrectField = "incorrect";

    /** String representing the date field of the stats table */

    private static final String dateField = "date";

    // ----------------------------------------------------------------------------------

    /** String array representing all the fields of the piece table */

    private static final String[] pieceTableFields = { pieceField, typeField };

    /** String array representing all the fields of the specific table */

    private static final String[] specificTableFields = { pointField,
            pieceField, elementField };

    /** String array representing all the fields of the quote table */

    private static final String[] quoteTableFields = { quoteField, pointField };

    /** String array representing all the fields of the element table */

    private static final String[] elementTableFields = { elementField,
            pieceField, elTypeField };

    /** String array representing all the fields of the stats table */

    private static final String[] statsTableFields = { idField, pieceField,
            correctField, incorrectField, dateField };

    // ----------------------------------------------------------------------------------

    /** SQL statement to create the piece table. */

    private static final String createPieceTable = "CREATE TABLE " + pieceTable
            + " (" + pieceField + " TEXT NOT NULL PRIMARY KEY, " + typeField
            + " INTEGER);";

    /** SQL statement to create the specific table. */

    private static final String createSpecificTable = "CREATE TABLE "
            + specificTable + "(" + pointField + " TEXT NOT NULL PRIMARY KEY,"
            + pieceField + " TEXT NOT NULL," + elementField + " TEXT NOT NULL)";

    /** SQL statement to create the quotes table. */

    private static final String createQuoteTable = "CREATE TABLE " + quoteTable
            + "(" + quoteField + " TEXT NOT NULL PRIMARY KEY," + pointField
            + " TEXT NOT NULL PRIMARY KEY)";

    /** SQL statement to create the element table. */

    private static final String createElementTable = "CREATE TABLE "
            + elementTable + "(" + elementField + " TEXT NOT NULL PRIMARY KEY,"
            + pieceField + " TEXT NOT NULL," + elTypeField + " INTEGER)";

    /** SQL statement to create the stats table. */

    private static final String createStatsTable = "CREATE TABLE " + statsTable
            + "(" + idField + " INTEGER PRIMARY KEY," + pieceField
            + " TEXT NOT NULL," + correctField + " INTEGER," + incorrectField
            + " INTEGER," + dateField + " DATE)";

    // ----------------------------------------------------------------------------------

    /**
     * SQL statement to search the piece table - replace [columnName] and
     * [value] with field to be s

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
108 views
Welcome To Ask or Share your Answers For Others

1 Answer

private BufferedReader pieceScan = new BufferedReader(
        new InputStreamReader(getPieceTextFile(this.context), "UTF-8"));

This initializer code runs when your class is instantiated. this.context is null. This is shown in the logcat in

01-03 12:17:55.091: W/System.err(589):  at com.lawson.englishlitrevision.ManipulateDatabase.getPieceTextFile(ManipulateDatabase.java:387)
01-03 12:17:55.091: W/System.err(589):  at com.lawson.englishlitrevision.ManipulateDatabase.<init>(ManipulateDatabase.java:398)

where <init> refers to constructor and member variable initialization. You initialize context in constructor, but constructor is only run after member variable initialization.

Just move the pieceScan initialization to the constructor.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...