java lang nullpointerexception error
Jeg begyndte imorges med at lære java med henblik på at lave nogle små android apps. Det gik også meget godt indtil jeg stødte ind i en java lang nullpointerexception error og den har jeg så nedstirret den sidste times tid eller 2. Dette er hvad LogCat giver mig:04-04 19:57:18.352: E/Trace(787): error opening trace file: No such file or directory (2)
04-04 19:57:18.792: I/dalvikvm-heap(787): Grow heap (frag case) to 9.325MB for 1536016-byte allocation
04-04 19:57:24.171: W/dalvikvm(787): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
04-04 19:57:24.201: E/AndroidRuntime(787): FATAL EXCEPTION: main
04-04 19:57:24.201: E/AndroidRuntime(787): java.lang.NullPointerException
04-04 19:57:24.201: E/AndroidRuntime(787): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.widget.AbsListView.obtainView(AbsListView.java:2267)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.widget.ListView.makeAndAddView(ListView.java:1769)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.widget.ListView.fillDown(ListView.java:672)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.widget.ListView.fillFromTop(ListView.java:733)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.widget.ListView.layoutChildren(ListView.java:1622)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.widget.AbsListView.onLayout(AbsListView.java:2102)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.view.View.layout(View.java:13754)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.view.ViewGroup.layout(ViewGroup.java:4362)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.view.View.layout(View.java:13754)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.view.ViewGroup.layout(ViewGroup.java:4362)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.view.View.layout(View.java:13754)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.view.ViewGroup.layout(ViewGroup.java:4362)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.view.View.layout(View.java:13754)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.view.ViewGroup.layout(ViewGroup.java:4362)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1866)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1687)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.view.Choreographer.doFrame(Choreographer.java:525)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.os.Handler.handleCallback(Handler.java:615)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.os.Handler.dispatchMessage(Handler.java:92)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.os.Looper.loop(Looper.java:137)
04-04 19:57:24.201: E/AndroidRuntime(787): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-04 19:57:24.201: E/AndroidRuntime(787): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 19:57:24.201: E/AndroidRuntime(787): at java.lang.reflect.Method.invoke(Method.java:511)
04-04 19:57:24.201: E/AndroidRuntime(787): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-04 19:57:24.201: E/AndroidRuntime(787): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-04 19:57:24.201: E/AndroidRuntime(787): at dalvik.system.NativeStart.main(Native Method)
Her er min menu.java:
package com.example.crystalbal;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class Menu extends ListActivity {
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Class listClass;
String[][] data = getSQL();
try {
listClass = Class.forName("com.example.crystalbal." + data[1][position]);
Intent listIntent = new Intent(Menu.this, listClass);
startActivity(listIntent);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public String[][] getSQL(){
boolean succes = true;
String[][] data = null;
try{
Database entry = new Database(Menu.this);
entry.open();
data = entry.getData();
entry.close();
} catch(Exception e ){
succes = false;
String error = e.toString();
Dialog d = new Dialog(Menu.this);
d.setTitle(error);
TextView tv = new TextView(Menu.this);
d.setContentView(tv);
d.show();
}finally{
}
return data;
}
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
String[][] data = getSQL();
setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, data[1]));
}
}
Og her er min database.java:
package com.example.crystalbal;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class Database {
public static final String KEY_ROWID = "_id";
public static final String KEY_TITEL = "titel";
public static final String KEY_INDHOLD = "indhold";
private static final String DATABASE_NAME = "HuskeDeddelDB";
private static final String DATABASE_TABLE = "Huskesedler";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL( "CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_TITEL + " TEXT NOT NULL, " + KEY_INDHOLD + " TEXT NOT NULL);");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public Database(Context c){
ourContext = c;
}
public Database open(){
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long insert(String titel, String indhold) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_TITEL, titel);
cv.put(KEY_INDHOLD, indhold);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String[][] getData() {
// TODO Auto-generated method stub
String[] resultater = new String[]{ KEY_ROWID, KEY_TITEL, KEY_INDHOLD};
Cursor c = ourDatabase.query(DATABASE_TABLE, resultater, null, null, null, null, null);
String[][] resultat = new String[3][c.getColumnCount()+1];
int iRow = c.getColumnIndex(KEY_ROWID);
int iIndhold = c.getColumnIndex(KEY_INDHOLD);
int iTtitel = c.getColumnIndex(KEY_TITEL);
int i = 1;
resultat[1][0] = new String("Opret ny huskeseddel");
resultat[0][0] = new String("-1");
resultat[2][0] = new String("false");
if(c.getColumnCount() <= 0){
}else{
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
resultat[0][i] = new String(c.getString(iRow));
resultat[1][i] = new String(c.getString(iTtitel));
resultat[2][i] = new String(c.getString(iIndhold));
i++;
}
}
return resultat;
}
}
Jeg kan ikke lige se at fejlen skulle ligge andre steder end i de 2 filer.