给出NPE时,cursor为null

时间:2014-03-31 15:32:36

标签: java sqlite android-layout exception android-cursor

我正在尝试通过游标从数据库中检索数据我得到了一个N​​PE。我很确定数据正在插入到数据库中,因为我已经将这个相同的数据库用于另一个Android应用程序而没有任何问题。我试过寻找类似问题的答案,但没有一个能解决我的问题。这是我的代码。

package com.example.testapp;
import mylibs.testshop;
import android.app.Activity;
import android.database.Cursor;

import android.os.Bundle;
import android.util.Log;
import android.widget.*;

public class shopping_list extends Activity {
   testshop shop;
   String item, type;
   int IdCounter = 0;
   LinearLayout itemLayout;

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.shopping_list);
       shop = new testshop(this); // now Initialized shop
        showShoppinglist();
        }

   public void showShoppinglist() {
       itemLayout = (LinearLayout) findViewById(R.id.item_list);

       // Here's is the cursor that gives the NPE
       String[] columns = new String[] { testshop.TABLE_ROW_ID,
                                         testshop.TABLE_ROW_ONE, 
                                         testshop.TABLE_ROW_TWO };
       Cursor cursor = shop.db.rawQuery(testshop.TABLE_NAME, columns);

       cursor.moveToFirst();
       if (!cursor.isAfterLast()) {
          do {
              addItemToList(cursor);
          } 
          while (cursor.moveToNext());
       }
       cursor.close(); // closing of the cursor
       shop.db.close(); // closing the database
   }

   public void addItemToList(Cursor items) {
       RelativeLayout itemListlayout = new RelativeLayout(this);
       try {
           addItemToitemListLayout(itemListlayout,
                                   items.getString(1),
                                   items.getString(1));
       } 
       catch (Exception e) {
           Log.e("additemtolistLAYOUT ERROR", e.toString());
       }
       itemListlayout.setId(IdCounter++);
       itemLayout.addView(itemListlayout);
   }

   private void addItemToitemListLayout(RelativeLayout itemListlayout,
                                        String string,
                                        String string2) {
       TextView textView = new TextView(this);
       textView.setText(item);
       RelativeLayout.LayoutParams layoutDesc = new RelativeLayout.LayoutParams(
                            RelativeLayout.LayoutParams.WRAP_CONTENT,
                            RelativeLayout.LayoutParams.WRAP_CONTENT);
       layoutDesc.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
       textView.setId(IdCounter++);
       TextView textView2 = new TextView(this);
       textView2.setText(type);
       layoutDesc.addRule(RelativeLayout.RIGHT_OF, textView2.getId());
       textView2.setId(IdCounter++);
       itemListlayout.addView(textView, layoutDesc);
       itemListlayout.addView(textView2, layoutDesc);
   }
}

这是我的logcat:

03-31 17:23:36.464: E/AndroidRuntime(400): FATAL EXCEPTION: main
03-31 17:23:36.464: E/AndroidRuntime(400): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testapp/com.example.testapp.shopping_list}: java.lang.StringIndexOutOfBoundsException
03-31 17:23:36.464: E/AndroidRuntime(400):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-31 17:23:36.464: E/AndroidRuntime(400):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-31 17:23:36.464: E/AndroidRuntime(400):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-31 17:23:36.464: E/AndroidRuntime(400):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-31 17:23:36.464: E/AndroidRuntime(400):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-31 17:23:36.464: E/AndroidRuntime(400):  at android.os.Looper.loop(Looper.java:123)
03-31 17:23:36.464: E/AndroidRuntime(400):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-31 17:23:36.464: E/AndroidRuntime(400):  at java.lang.reflect.Method.invokeNative(Native Method)
03-31 17:23:36.464: E/AndroidRuntime(400):  at java.lang.reflect.Method.invoke(Method.java:521)
03-31 17:23:36.464: E/AndroidRuntime(400):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-31 17:23:36.464: E/AndroidRuntime(400):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-31 17:23:36.464: E/AndroidRuntime(400):  at dalvik.system.NativeStart.main(Native Method)
03-31 17:23:36.464: E/AndroidRuntime(400): Caused by: java.lang.StringIndexOutOfBoundsException
03-31 17:23:36.464: E/AndroidRuntime(400):  at java.lang.String.substring(String.java:1579)
03-31 17:23:36.464: E/AndroidRuntime(400):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:66)
03-31 17:23:36.464: E/AndroidRuntime(400):  at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
03-31 17:23:36.464: E/AndroidRuntime(400):  at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
03-31 17:23:36.464: E/AndroidRuntime(400):  at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
03-31 17:23:36.464: E/AndroidRuntime(400):  at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1315)
03-31 17:23:36.464: E/AndroidRuntime(400):  at com.example.testapp.shopping_list.showShoppinglist(shopping_list.java:38)
03-31 17:23:36.464: E/AndroidRuntime(400):  at com.example.testapp.shopping_list.onCreate(shopping_list.java:30)
03-31 17:23:36.464: E/AndroidRuntime(400):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-31 17:23:36.464: E/AndroidRuntime(400):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-31 17:23:36.464: E/AndroidRuntime(400):  ... 11 more

1 个答案:

答案 0 :(得分:1)

您的shopnull。你还没有初始化它。