0

我收到一个 NullPointerException ,我只是看不到。我知道这可能是我忽略的一些简单的事情。我有一点逐行读取文本文件,将其拆分,并使用数据创建自定义类并将数据传递给 SQLite 数据库。

Public void createData() {
try {
        InputStream in = this.getAssets().open("stops.txt");
        BufferedReader reader = new BufferedReader( new InputStreamReader(in));
        String line;
        line=reader.readLine();
        while ((line = reader.readLine())!=null) {
            String lineValues[] = line.split(",");
            Stops stop = new Stops();
            stop.setLon(lineValues[5]);
            stop.setLat(lineValues[4]);
            stop.setName(lineValues[2]);
            stop.setNumber(lineValues[0]);
            dataSource.create(stop);
        }

    }catch (IOException e) {
        e.printStackTrace();
    }catch(NullPointerException n) {
        n.printStackTrace();
        Log.d(TAG,n.toString());
    }
 }

异常发生在dataSource.create(stop);任何提示上?

编辑:这是堆栈跟踪:

java.lang.RuntimeException: Unable to start activity ComponentInfo{ccalgary.transit.helper/ccalgary.transit.helper.MainActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2356)
    at android.app.ActivityThread.access$600(ActivityThread.java:150)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5195)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
    at ccalgary.transit.helper.MainActivity.createData(MainActivity.java:341)
    at ccalgary.transit.helper.MainActivity.onCreate(MainActivity.java:71)
    at android.app.Activity.performCreate(Activity.java:5104)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2260)

这是 onCreate

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    settings = PreferenceManager.getDefaultSharedPreferences(this);
    sPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    sContext = getApplicationContext();
    boolean boot = settings.getBoolean("launch", false);
    if (boot == false) {
        createData();
    }
    dataSource = new StopsDataSource(this);
    dbhelper = new StopsDBHelper(this,TABLE_STOPS,null,1);
    mDatabase = dbhelper.getWritableDatabase();
    dataSource.open();
    if (mDatabase.isOpen()) {

        Log.d(TAG, "database open");
    } else {
        Log.d(TAG,"database closed");
    }
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
        //Toast.makeText(this, "GPS is Enabled in your devide", Toast.LENGTH_SHORT).show();
    }else{
        showGPSDisabledAlertToUser();
    }
    //MyLocationListener = new MyLocationListener();



    notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
}

数据源:

public class StopsDataSource {

SQLiteOpenHelper dbHelper;
SQLiteDatabase database;

public StopsDataSource(Context context) {
    dbHelper = new StopsDBHelper(context, "stops", null, 1 );

}

public void open() {
    database = dbHelper.getWritableDatabase();
}

public void close() {
    dbHelper.close();
}

public Stops create(Stops stops) {
    ContentValues values = new ContentValues();
    values.put(StopsDBHelper.COLUMN_STOP_LAT, stops.getLat());
    values.put(StopsDBHelper.COLUMN_STOP_LON, stops.getLon());
    values.put(StopsDBHelper.COLUMN_STOP_NAME, stops.getName());
    values.put(StopsDBHelper.COLUMN_STOP_NUMBER,stops.getNumber());
    long instertID = database.insert(StopsDBHelper.TABLE_STOPS, null, values);
    stops.setId(instertID);
    return stops;
}

}

和数据库助手:

public class StopsDBHelper extends SQLiteOpenHelper {

public static final String TABLE_STOPS = "stops";
public static final String COlUMN_ID = "stopID";
public static final String COLUMN_STOP_NAME = "name";
public static final String COLUMN_STOP_LAT = "lat";
public static final String COLUMN_STOP_LON = "lon";
public static final String COLUMN_STOP_NUMBER = "number";
public static final String TABLE_CREATE = "CREATE TABLE IF NOT EXISTS " + TABLE_STOPS + "(" + COlUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +  COLUMN_STOP_LAT + " TEXT, " + COLUMN_STOP_LON + " TEXT, " +
        COLUMN_STOP_NUMBER + " TEXT, " + COLUMN_STOP_NAME + " TEXT" + ")";
public StopsDBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL(TABLE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_STOPS);
    onCreate(sqLiteDatabase);
}
}
4

2 回答 2

1

dataSource为空,stop 不能为空,否则调用它的方法时会得到一个空指针,我们也可以看到你专门调用new Stops();

于 2013-08-16T03:03:54.070 回答
1

createData()在初始化之前调用该方法dataSource。在方法调用之前进行。即,在调用任何可能使用它的函数之前先进行初始化。

    dataSource = new StopsDataSource(this);
 if (boot == false) {
        createData();
    }
于 2013-08-16T03:37:24.997 回答