0

見えない NullPointerException が発生しています。私が見落としているのはおそらく単純なことだと思います。テキストファイルを1行ずつ読み取り、データを使用してカスタムクラスを作成し、データを渡して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);Any tips out there?で例外が発生していますか?

編集:スタックトレースは次のとおりです。

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

2 に答える 2

1

dataSourceがnull、stopがnullにならない、またはそのメソッドを呼び出すときにnullポインターを取得する、また、具体的に呼び出していることがわかりますnew Stops();

于 2013-08-16T03:03:54.070 に答える