방금 볼 수없는 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);
모든 팁에 발생?
편집 : 여기 스택 추적입니다 :
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 도우미 : 여기
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 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);
}
}
예외 스택 추적을 추가하십시오. – kichik
스택 추적을 제공 할 수 있습니까? – FabianCook
라인 341이란 무엇입니까? – SK9