2013-08-16 19 views
0

我收到了一個我無法看到的NullPointerException。我知道這可能很簡單,我可以忽略。我已經逐行讀入一個文本文件,將它分開,並使用這些數據創建一個自定義類,並將這些數據傳遞給SQLite數據庫。找不到我的NullPointerException

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; 
} 

} 

和DB助手:

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); 
} 
} 
+1

請添加異常堆棧跟蹤。 – kichik

+0

你能提供堆棧跟蹤嗎? – FabianCook

+0

什麼是341行? – SK9

回答

1

要初始化dataSource之前調用該方法createData()。在方法調用之前進行。即在調用可能使用它的任何函數之前首先進行初始化。

dataSource = new StopsDataSource(this); 
if (boot == false) { 
     createData(); 
    } 
+0

這是它謝謝你! –

1

dataSource爲空,止損不能爲空或同時調用其方法,你會得到一個空指針,我們也可以看到你專門致電new Stops();

+0

數據源不爲空,我只是在onCreate部分對其進行了定義。 –

+0

提供堆棧跟蹤,我們將能夠確認。另外,這些聲明在哪裏被調用? – FabianCook