Android:从sql数据库中检索信息时出现NullPointerException

时间:2012-09-27 07:56:05

标签: android

我有一个问题,我现在几天都无法解决。 我已经创建了从数据库写入和检索数据的方法,但这是一个我无法找到的问题。 这是代码:

    public void writeEntry(String name, long period, long frequency, long recievingTime, String stats){
        ContentValues cv = new ContentValues();
        cv.put(KEY_NAME, name);
        cv.put(KEY_PERIOD, period);
        cv.put(KEY_FREQUENCY, frequency);
        cv.put(KEY_RECIEVINGTIME, recievingTime);
        cv.put(KEY_STATS, stats);
        myDatabase.insert(DATABASE_TABLE, null, cv);
    }

    public Cursor query(String[] columns, String selection, String[] selectionArgs, String groupBy) {
        // TODO Auto-generated method stub
        Cursor c = myDatabase.query(DATABASE_TABLE, columns, selection, selectionArgs, groupBy, null, null);
        return c;
    }

以下是将数据写入db的方法:

 public void save(){
    sharedPrefs = context.getSharedPreferences(SPNAME, 0);
    Log.v(" info", sharedPrefs.getAll().toString());
    Database database = new Database(context);
    database.open();
    database.writeEntry(
            sharedPrefs.getString(NAME, "No data was found"), 
            sharedPrefs.getLong(PERIOD, 0),
            sharedPrefs.getLong(FREQUENCY, 0), 
            sharedPrefs.getLong(RECIEVINGTIME, 0), 
            sharedPrefs.getString(STATS, "No data was found")
            );
    database.close();
}

以下是我检索它的方法:

 public List<String> getReportInfo(String rowId){
    List<String> result = new ArrayList<String>();
    database = new Database(context);
    database.open();
    cursor = database.query(new String[]{
            Database.KEY_NAME,
            Database.KEY_PERIOD,
            Database.KEY_FREQUENCY,
            Database.KEY_RECIEVINGTIME,
            Database.KEY_STATS},
            Database.KEY_ROWID +" = "+rowId, null, null);
    for(cursor.moveToFirst(); cursor.isAfterLast(); cursor.moveToNext()){
        result.add(cursor.getString(cursor.getPosition()));
    }
    cursor.close(); 
    database.close();   
    return result;
}

稍后,我在getView方法的BaseAdapter中使用db中的数据来填充ListView,如下所示:

  public class MyArrayAdapter extends BaseAdapter{  

    private Activity activity;
    private LayoutInflater inflater;
    TextView tvMyReportsPeriod, tvMyReportsFrequency, tvMyReportsReceivingTime, tvMyReportTitle;
    LinearLayout llMyReportsStats;
    Database database;
    Report report;
    Cursor cursor;

    public MyArrayAdapter(Activity a) {
        super();
        // TODO Auto-generated constructor stub
        activity = a;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        report = new Report(activity);
    }

    public int getCount() {
        // TODO Auto-generated method stub
        int count = 2;
        try{
        cursor = database.query(new String[]{Database.KEY_NAME}, null, null, null);
        count= cursor.getCount();
        cursor.close();
        }catch(Exception e){}
        return count;
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        List<String> list = report.getReportInfo(String.valueOf(position));
        return list;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if(convertView==null){
            convertView = inflater.inflate(R.layout.my_reports_view_holder, null, false);

            tvMyReportsPeriod = (TextView)convertView.findViewById(R.id.tvMyReportsPeriod);
            tvMyReportsFrequency = (TextView)convertView.findViewById(R.id.tvMyReportsFrequency);
            tvMyReportsReceivingTime = (TextView)convertView.findViewById(R.id.tvMyReportsReceivingTime);
            tvMyReportTitle = (TextView)convertView.findViewById(R.id.tvMyReportTitle);
            llMyReportsStats = (LinearLayout)convertView.findViewById(R.id.llMyReportsStats);

                List<String> data = report.getReportInfo(String.valueOf(position));
                if(data!=null){
                    tvMyReportTitle.setText(data.get(0));
                    tvMyReportsPeriod.setText(report.convertMillis(Long.parseLong(data.get(1))));
                    tvMyReportsFrequency.setText(report.convertMillis(Long.parseLong(data.get(2))));
                    tvMyReportsReceivingTime.setText(report.convertMillis(Long.parseLong(data.get(3))));
                    String[] stats = data.get(4).toString().split(" ");             
                    for(int x=0; x<stats.length; x++){
                        TextView textView = new TextView(activity);
                        for(Constants dc: Constants.values()){
                            if(dc.getId()==Integer.parseInt(stats[x])){
                                textView.setText(dc.getText());
                            }
                        }
                        llMyReportsStats.addView(textView); 
                    }
                }else{System.out.println("ERROR: data=null");}
        }
        return convertView;
    }

我希望每次为每个ListView项目从db获取一行,但我得到这个logcat:

09-27 11:03:03.173: E/AndroidRuntime(25768): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at com.dailyreports.ainius.Report.getReportInfo(Report.java:107)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at com.dailyreports.ainius.MyReports$MyArrayAdapter.getView(MyReports.java:88)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.widget.AbsListView.obtainView(AbsListView.java:1430)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.widget.ListView.onMeasure(ListView.java:1127)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.view.View.measure(View.java:8313)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.view.View.measure(View.java:8313)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.view.View.measure(View.java:8313)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.view.View.measure(View.java:8313)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.view.View.measure(View.java:8313)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.view.ViewRoot.performTraversals(ViewRoot.java:841)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1861)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.os.Looper.loop(Looper.java:123)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at android.app.ActivityThread.main(ActivityThread.java:3729)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at java.lang.reflect.Method.invokeNative(Native Method)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at java.lang.reflect.Method.invoke(Method.java:507)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:632)
09-27 11:03:03.173: E/AndroidRuntime(25768):    at dalvik.system.NativeStart.main(Native Method)

不知道问题出在哪里。请帮忙。

4 个答案:

答案 0 :(得分:0)

您应该检查cursor.moveToFirst()是否返回true。

如果您正在进行的查询没有任何结果,您将获得CursorIndexOutOfBoundsException例外...

另外,正如阿普里安所指出的那样。 cursor.getString将列索引作为参数,而不是光标的位置。

result.add(cursor.getString(cursor.getPosition()));替换为result.add(cursor.getString(Database.KEY_STATS));

答案 1 :(得分:0)

cursor.getString(cursor.getPosition())不会导致您发布的错误,但稍后会导致其他错误。如果您阅读文档,那么期望的参数是列索引,而不是行索引。

您正在观察的错误可能是由于moveToFirst返回False但您没有测试它。你的数据库是空的。

修改

再次阅读之后,你的for循环(虽然非常好看)完全关闭。您将isAfterLast测试为循环条件,而最多应该是退出条件。您没有检查移动到第一个的结果。你不检查moveToNext的结果。

替换你的for循环
while(cursor.moveToNext()) {
    // Do stuff
}

答案 2 :(得分:0)

问题似乎也在这里:

result.add(cursor.getString(cursor.getPosition()));

因为如果获取的记录数超过查询中请求的列数,则会抛出错误。这不是您应该从游标中读取值的方式,实际上您应该确定应该读取哪些列值。例如:

result.add(cursor.getString(cursor.getColumnIndex(Database.KEY_NAME)));

或者可能具有固定的索引值,例如:

result.add(cursor.getString(0));

答案 3 :(得分:0)

编辑:看起来像你想从数据库中检索一行。

您应该使用MyObject代替List<String>来实现您想要的目标。

以下是MyObject的示例,请参阅POJO

public class MyObject {
    String name = "", stats = "";
    Long period = 0, frequency = 0, receivingtime = 0;

    // empty constructor
    public MyObject() {
    }

    // simple getter method
    public String getName() {
        return name;
    }

    // simple setter method
    public void setName(String name) {
        this.name = name;
    }

    ...
    // You may want to override toString() method when implementing searching or using ArrayAdapter
    @Override
    public String toString() {
        return name + " " + stats;
    }
}

将您的getReportInfo更改为:

public MyObject getReportInfo(String rowId){
    MyObject result = new MyObject();
    database = new Database(context);
    database.open();
    cursor = database.query(new String[]{
            Database.KEY_NAME,
            Database.KEY_PERIOD,
            Database.KEY_FREQUENCY,
            Database.KEY_RECIEVINGTIME,
            Database.KEY_STATS},
            Database.KEY_ROWID +" = "+rowId, null, null);
    // this will prevent CursorIndexOutofBoundsException
    if (cursor.moveToFirst()) {
        result.setName(cursor.getString(0));
        result.setPeriod(cursor.getLong(1));
        result.setFrequency(cursor.getLong(2));
        result.setReceivingTime(cursor.getLong(3));
        result.setStats(cursor.getString(4));
    }
    cursor.close(); 
    database.close();   
    return result;
}