Android: NullPointerException when retrieving info from sql database
<p>I have a problem that i'm unable to solve for a couple of days now.
I've created methods to write and retrieve data to and from the db, but theres a problem that I can't find. 
Here is the code:</p>

<pre><code>    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;
    }
</code></pre>

<p>Here is the method to write data to the db:</p>

<pre><code> 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();
}
</code></pre>

<p>And here is how I retrieve it:</p>

<pre><code> public List&lt;String&gt; getReportInfo(String rowId){
    List&lt;String&gt; result = new ArrayList&lt;String&gt;();
    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;
}
</code></pre>

<p>Later on, I use data from db in BaseAdapter in getView method to populate the ListView like this:</p>

<pre><code>  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&lt;String&gt; 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&lt;String&gt; 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&lt;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;
    }
</code></pre>

<p>I expect to get one row from db at a time for each ListView item, but instead I get this logcat:</p>

<pre><code>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)
</code></pre>

<p>Don't know where's the problem. Please help.</p>
