Autore Topic: Errore database nullpointerexception  (Letto 638 volte)

Offline TheRedFox

  • Nuovo arrivato
  • *
  • Post: 30
  • Respect: +1
    • Mostra profilo
  • Dispositivo Android:
    Nexus one
  • Play Store ID:
    RedFox Labs
  • Sistema operativo:
    Debian lenny 5.0, Windows XP
Errore database nullpointerexception
« il: 22 Aprile 2011, 16:23:35 CEST »
0
Salve a tutti, è la prima volta che chiedo aiuto, ora vi spiego il mio problema.
Vorrei scrivere una semplice applicazione per iniziare ad imparare qualcosa sui database.
L' applicazione crea un db e inserisce un solo record, che è la frase "hello database by redfox" e poi va a rileggerla per scriverla in una textview.

ecco il codice:

act.java che è l' activity vera e propria.
Codice (Java): [Seleziona]
package red.fox.hellodatabase;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.TextView;
import red.fox.hellodatabase.hellodb;

public class act extends Activity {
        String key_testo = "testo";
        String testo = "Hello Database By RedFox";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        hellodb db = new hellodb(getApplicationContext());
        db.open();
        db.hinsert(testo);
        TextView tv = (TextView) findViewById(R.id.tv);
        Cursor cursor = db.alldb();
        startManagingCursor(cursor);
        int textcol = cursor.getColumnIndex(key_testo);
        final String testo = cursor.getString(textcol);
        tv.setText(testo);
        db.close();
    }
}

hellodb.java la classe per la gestione del db:
Codice (Java): [Seleziona]
package red.fox.hellodatabase;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;



public class hellodb {
        public static final String key_testo = "text";
        public static final String table = "hellos";
        private static final String db_name = "hello";
        private static final int db_version = 1;
        private final Context context;
        private SQLiteDatabase db;
        private hellohelper hp;
       
        private static class hellohelper extends SQLiteOpenHelper {
                hellohelper (Context context) {
                        super (context, db_name, null, db_version);
                }
                @Override
                public void onCreate(SQLiteDatabase db) {
                        db.execSQL("create table hellos (id integer primary key autoincrement, testo text not null);");
                       
                }
                @Override
                public void onUpgrade(SQLiteDatabase db, int oldv, int newv){
                       
                        db.execSQL("drop table hellos");
                        onCreate(db);
                }
        }
               
        public void open() {
                db = hp.getWritableDatabase();
        }
        public void close() {
                hp.close();
        }
        public void hinsert(String testo){
                ContentValues cv = new ContentValues();
                cv.put(key_testo,testo);
                db.insert(table,null,cv);
        }
        public Cursor alldb(){
                return db.query(table,null,null,null,null,null,null);
        }
        public hellodb(Context ctx) {
                this.context = ctx;
        }
}

ecco gli errori del logcat:
Codice: [Seleziona]
04-22 16:12:09.186: INFO/PackageManager(59): Removing non-system package:red.fox.hellodatabase
04-22 16:12:09.196: INFO/ActivityManager(59): Force stopping package red.fox.hellodatabase uid=10041
04-22 16:12:09.206: INFO/Process(59): Sending signal. PID: 1036 SIG: 9
04-22 16:12:09.256: WARN/InputManagerService(59): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44ee34d8
04-22 16:12:10.405: DEBUG/PackageManager(59): Scanning package red.fox.hellodatabase
04-22 16:12:10.405: INFO/PackageManager(59): Package red.fox.hellodatabase codePath changed from /data/app/red.fox.hellodatabase-1.apk to /data/app/red.fox.hellodatabase-2.apk; Retaining data and using new
04-22 16:12:10.415: INFO/PackageManager(59): /data/app/red.fox.hellodatabase-2.apk changed; unpacking
04-22 16:12:10.455: DEBUG/installd(35): DexInv: --- BEGIN '/data/app/red.fox.hellodatabase-2.apk' ---
04-22 16:12:10.816: DEBUG/dalvikvm(1156): DexOpt: load 52ms, verify 65ms, opt 3ms
04-22 16:12:10.826: DEBUG/installd(35): DexInv: --- END '/data/app/red.fox.hellodatabase-2.apk' (success) ---
04-22 16:12:10.836: WARN/PackageManager(59): Code path for pkg : red.fox.hellodatabase changing from /data/app/red.fox.hellodatabase-1.apk to /data/app/red.fox.hellodatabase-2.apk
04-22 16:12:10.836: WARN/PackageManager(59): Resource path for pkg : red.fox.hellodatabase changing from /data/app/red.fox.hellodatabase-1.apk to /data/app/red.fox.hellodatabase-2.apk
04-22 16:12:10.836: DEBUG/PackageManager(59):   Activities: red.fox.hellodatabase.act
04-22 16:12:10.866: INFO/ActivityManager(59): Force stopping package red.fox.hellodatabase uid=10041
04-22 16:12:11.066: INFO/installd(35): move /data/dalvik-cache/data@app@red.fox.hellodatabase-2.apk@classes.dex -> /data/dalvik-cache/data@app@red.fox.hellodatabase-2.apk@classes.dex
04-22 16:12:11.066: DEBUG/PackageManager(59): New package installed in /data/app/red.fox.hellodatabase-2.apk
04-22 16:12:11.355: INFO/ActivityManager(59): Force stopping package red.fox.hellodatabase uid=10041
04-22 16:12:11.616: DEBUG/dalvikvm(59): GC_EXPLICIT freed 8234 objects / 522728 bytes in 214ms
04-22 16:12:11.796: DEBUG/dalvikvm(131): GC_EXPLICIT freed 880 objects / 47008 bytes in 212ms
04-22 16:12:12.425: DEBUG/dalvikvm(170): GC_EXPLICIT freed 1961 objects / 98472 bytes in 518ms
04-22 16:12:12.706: DEBUG/dalvikvm(59): GC_EXPLICIT freed 3451 objects / 169752 bytes in 240ms
04-22 16:12:12.756: WARN/RecognitionManagerService(59): no available voice recognition services found
04-22 16:12:12.886: INFO/installd(35): unlink /data/dalvik-cache/data@app@red.fox.hellodatabase-1.apk@classes.dex
04-22 16:12:12.946: DEBUG/AndroidRuntime(1147): Shutting down VM
04-22 16:12:12.986: DEBUG/dalvikvm(1147): Debugger has detached; object registry had 1 entries
04-22 16:12:13.046: INFO/AndroidRuntime(1147): NOTE: attach of thread 'Binder Thread #3' failed
04-22 16:12:14.216: DEBUG/AndroidRuntime(1162): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
04-22 16:12:14.226: DEBUG/AndroidRuntime(1162): CheckJNI is ON
04-22 16:12:14.566: DEBUG/AndroidRuntime(1162): --- registering native functions ---
04-22 16:12:16.335: INFO/ActivityManager(59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=red.fox.hellodatabase/.act }
04-22 16:12:16.676: DEBUG/AndroidRuntime(1162): Shutting down VM
04-22 16:12:16.826: DEBUG/jdwp(1162): Got wake-up signal, bailing out of select
04-22 16:12:16.826: DEBUG/dalvikvm(1162): Debugger has detached; object registry had 1 entries
04-22 16:12:16.886: INFO/ActivityManager(59): Start proc red.fox.hellodatabase for activity red.fox.hellodatabase/.act: pid=1170 uid=10041 gids={}
04-22 16:12:16.886: INFO/AndroidRuntime(1162): NOTE: attach of thread 'Binder Thread #3' failed
04-22 16:12:18.846: DEBUG/AndroidRuntime(1170): Shutting down VM
04-22 16:12:18.846: WARN/dalvikvm(1170): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-22 16:12:18.906: ERROR/AndroidRuntime(1170): FATAL EXCEPTION: main
04-22 16:12:18.906: ERROR/AndroidRuntime(1170): java.lang.RuntimeException: Unable to start activity ComponentInfo{red.fox.hellodatabase/red.fox.hellodatabase.act}: java.lang.NullPointerException
04-22 16:12:18.906: ERROR/AndroidRuntime(1170):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-22 16:12:18.906: ERROR/AndroidRuntime(1170):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-22 16:12:18.906: ERROR/AndroidRuntime(1170):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-22 16:12:18.906: ERROR/AndroidRuntime(1170):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-22 16:12:18.906: ERROR/AndroidRuntime(1170):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 16:12:18.906: ERROR/AndroidRuntime(1170):     at android.os.Looper.loop(Looper.java:123)
04-22 16:12:18.906: ERROR/AndroidRuntime(1170):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-22 16:12:18.906: ERROR/AndroidRuntime(1170):     at java.lang.reflect.Method.invokeNative(Native Method)
04-22 16:12:18.906: ERROR/AndroidRuntime(1170):     at java.lang.reflect.Method.invoke(Method.java:521)
04-22 16:12:18.906: ERROR/AndroidRuntime(1170):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-22 16:12:18.906: ERROR/AndroidRuntime(1170):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-22 16:12:18.906: ERROR/AndroidRuntime(1170):     at dalvik.system.NativeStart.main(Native Method)
04-22 16:12:18.906: ERROR/AndroidRuntime(1170): Caused by: java.lang.NullPointerException
04-22 16:12:18.906: ERROR/AndroidRuntime(1170):     at red.fox.hellodatabase.hellodb.open(hellodb.java:38)
04-22 16:12:18.906: ERROR/AndroidRuntime(1170):     at red.fox.hellodatabase.act.onCreate(act.java:19)
04-22 16:12:18.906: ERROR/AndroidRuntime(1170):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-22 16:12:18.906: ERROR/AndroidRuntime(1170):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-22 16:12:18.906: ERROR/AndroidRuntime(1170):     ... 11 more
04-22 16:12:18.952: WARN/ActivityManager(59):   Force finishing activity red.fox.hellodatabase/.act
04-22 16:12:19.466: WARN/ActivityManager(59): Activity pause timeout for HistoryRecord{44ff1c58 red.fox.hellodatabase/.act}
04-22 16:12:26.255: DEBUG/dalvikvm(193): GC_EXPLICIT freed 149 objects / 11072 bytes in 106ms
04-22 16:12:31.013: WARN/ActivityManager(59): Activity destroy timeout for HistoryRecord{44ff1c58 red.fox.hellodatabase/.act}
04-22 16:12:31.355: DEBUG/dalvikvm(280): GC_EXPLICIT freed 31 objects / 1496 bytes in 160ms
04-22 16:17:08.371: DEBUG/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol
04-22 16:17:19.025: INFO/Process(1170): Sending signal. PID: 1170 SIG: 9
04-22 16:17:19.065: INFO/ActivityManager(59): Process red.fox.hellodatabase (pid 1170) has died.
04-22 16:17:19.095: WARN/InputManagerService(59): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44f99fa0
04-22 16:22:08.376: DEBUG/SntpClient(59): request time failed: java.net.SocketException: Address family not supported by protocol

Non riesco a capire dove sta l' errore.

Offline Ricky`

  • Amministratore
  • Utente storico
  • *****
  • Post: 3489
  • Respect: +507
    • Github
    • Google+
    • rciovati
    • Mostra profilo
Re: Errore database nullpointerexception
« Risposta #1 il: 22 Aprile 2011, 23:17:27 CEST »
0
Non instanzi l'oggetto hp.

Sent from my HTC Desire

Offline TheRedFox

  • Nuovo arrivato
  • *
  • Post: 30
  • Respect: +1
    • Mostra profilo
  • Dispositivo Android:
    Nexus one
  • Play Store ID:
    RedFox Labs
  • Sistema operativo:
    Debian lenny 5.0, Windows XP
Re:Errore database nullpointerexception
« Risposta #2 il: 25 Aprile 2011, 16:06:07 CEST »
0
Nel file hellodb.java lo istanzio cosi:
Codice (Java): [Seleziona]
 private hellohelper hp;

Offline Ricky`

  • Amministratore
  • Utente storico
  • *****
  • Post: 3489
  • Respect: +507
    • Github
    • Google+
    • rciovati
    • Mostra profilo
Re: Errore database nullpointerexception
« Risposta #3 il: 25 Aprile 2011, 16:22:27 CEST »
0
Quella è la dichiarazione. Instanziare significa fare una cosa tipo:

variabile = new Classe()

Sent from my HTC Desire
« Ultima modifica: 25 Aprile 2011, 16:43:27 CEST da Ricky` »

Offline TheRedFox

  • Nuovo arrivato
  • *
  • Post: 30
  • Respect: +1
    • Mostra profilo
  • Dispositivo Android:
    Nexus one
  • Play Store ID:
    RedFox Labs
  • Sistema operativo:
    Debian lenny 5.0, Windows XP
Re:Errore database nullpointerexception
« Risposta #4 il: 26 Aprile 2011, 14:43:48 CEST »
0
quindi dovrei fare una cosa come:
hellohelper hp = new hellohelper();

Problema risolto, ma sembra che ora ci sia un problema con il cursor.
Ecco il log:
04-26 16:25:26.293: ERROR/AndroidRuntime(693): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 11

Tuttto risolto, mi dimenticavo di spostare il cursore al primo elemento :-)
« Ultima modifica: 26 Aprile 2011, 17:07:25 CEST da TheRedFox »