Autore Topic: NullPointerException con getWritableDatabase  (Letto 751 volte)

Offline racoonlab

  • Utente junior
  • **
  • Post: 98
  • Respect: +1
    • Mostra profilo
  • Dispositivo Android:
    HTC Desire
NullPointerException con getWritableDatabase
« il: 31 Maggio 2011, 13:46:51 CEST »
0
Ciao a tutti,

Ho un dubbio. Questo errore (vedi sotto) di puntatore nullo e' perche' getWritableDatabase non viene visto, giusto?

Nella classe ho

Codice: [Seleziona]
...
private class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL(DATABASE_CREATE);
           
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS "+ DATABASE_TABLE );
            onCreate(db);
            load(new InputStreamReader(this.getClass().getResourceAsStream("data.csv")));
        }
    }

  public GeoDbAdapter(Context ctx) {
        this.mCtx = ctx;
    }

public GeoDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }
...
...
public void load(InputStreamReader in) {
                BufferedReader reader = new BufferedReader(in);
                try {
                        String line = null;
                        while ( (line = reader.readLine()) != null ) {
                                mDb.insert(DATABASE_TABLE, null, getContentValues(line));
                        }
                        reader.close();
                } catch (IOException e) {
                        e.printStackTrace();
                } finally {
                        if (in != null) {
                                try {
                                        in.close();
                                } catch (IOException e) {
                                }
                        }
                }
        }

Codice: [Seleziona]
05-31 11:37:14.584: ERROR/AndroidRuntime(3443): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.racoonlab.geo.map/com.racoonlab.geo.map.GeoIta01}: java.lang.NullPointerException
05-31 11:37:14.584: ERROR/AndroidRuntime(3443):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
05-31 11:37:14.584: ERROR/AndroidRuntime(3443):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-31 11:37:14.584: ERROR/AndroidRuntime(3443):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-31 11:37:14.584: ERROR/AndroidRuntime(3443):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-31 11:37:14.584: ERROR/AndroidRuntime(3443):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 11:37:14.584: ERROR/AndroidRuntime(3443):     at android.os.Looper.loop(Looper.java:123)
05-31 11:37:14.584: ERROR/AndroidRuntime(3443):     at android.app.ActivityThread.main(ActivityThread.java:4363)
05-31 11:37:14.584: ERROR/AndroidRuntime(3443):     at java.lang.reflect.Method.invokeNative(Native Method)
05-31 11:37:14.584: ERROR/AndroidRuntime(3443):     at java.lang.reflect.Method.invoke(Method.java:521)
05-31 11:37:14.584: ERROR/AndroidRuntime(3443):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-31 11:37:14.584: ERROR/AndroidRuntime(3443):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-31 11:37:14.584: ERROR/AndroidRuntime(3443):     at dalvik.system.NativeStart.main(Native Method)
05-31 11:37:14.584: ERROR/AndroidRuntime(3443): Caused by: java.lang.NullPointerException
05-31 11:37:14.584: ERROR/AndroidRuntime(3443):     at com.racoonlab.geo.map.GeoDbAdapter.load(GeoDbAdapter.java:317)
05-31 11:37:14.584: ERROR/AndroidRuntime(3443):     at com.racoonlab.geo.map.GeoDbAdapter$DatabaseHelper.onUpgrade(GeoDbAdapter.java:81)
05-31 11:37:14.584: ERROR/AndroidRuntime(3443):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:108)
05-31 11:37:14.584: ERROR/AndroidRuntime(3443):     at com.racoonlab.geo.map.GeoDbAdapter.open(GeoDbAdapter.java:106)
05-31 11:37:14.584: ERROR/AndroidRuntime(3443):     at com.racoonlab.geo.map.GeoIta01.onCreate(GeoIta01.java:55)
05-31 11:37:14.584: ERROR/AndroidRuntime(3443):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-31 11:37:14.584: ERROR/AndroidRuntime(3443):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
05-31 11:37:14.584: ERROR/AndroidRuntime(3443):     ... 11 more


Offline racoonlab

  • Utente junior
  • **
  • Post: 98
  • Respect: +1
    • Mostra profilo
  • Dispositivo Android:
    HTC Desire
Re:NullPointerException con getWritableDatabase
« Risposta #1 il: 31 Maggio 2011, 19:11:35 CEST »
0
Risolto seguendo la sezione "Gestione dei dati" in tutorial.