我得到一个我无法看到的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;
}
}
和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);
}
}
答案 0 :(得分:1)
dataSource
为null,stop不能为null或者在调用其方法时会得到一个空指针,我们也可以看到你专门调用new Stops();
答案 1 :(得分:1)
在初始化createData()
之前,您正在调用方法dataSource
。在方法调用之前进行。即,在调用任何可能使用它的函数之前先进行初始化。
dataSource = new StopsDataSource(this);
if (boot == false) {
createData();
}