title: Android - Caused by: java.lang.NullPointerException: storage == null
body: <p>"I am trying to retrieve data from database and add into my listAdapter. Everything work fine while the listAdapter is not empty. However, I am getting runtime exception when the listAdapter is empty. I been tried for several hours but can't get a solution. The solution I had tried such as setEmptyView, setEmptyText, throw Exception, etc. Sorry I'm new to programming."</p>
  
  <p>Here is my ListFragment (LecMonFragmentTab.java):</p>
  
  <pre><code>public class LecMonFragmentTab extends ListFragment{
  DBAdapter dbAdapter;
  
   public LecMonFragmentTab() {
  
   }
  
   @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    dbAdapter = new DBAdapter(getActivity());
    dbAdapter.open();
    String myData[] = dbAdapter.getScheduleRecord("1");  
    ListAdapter listAdapter = new ArrayAdapter&lt;String&gt;(getActivity(), android.R.layout.simple_list_item_1, myData);
    setListAdapter(listAdapter);
  
    } 
  
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.lec_mon_fragment_tab, container, false);
   }
  
   @Override
   public void onListItemClick(ListView list, View v, int position, long id) {
  
    Toast.makeText(getActivity(), getListView().getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
   }
  }
  </code></pre>
  
  <p>lec_mon_fragment_tab.xml</p>
  
  <pre><code>&lt;LinearLayout 
  android:layout_height="match_parent" 
  android:layout_width="match_parent" 
  android:orientation="vertical" 
  android:paddingLeft="8dp" 
  android:paddingRight="8dp" 
  xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools"
  tools:context="com.example.fyp2.LecMonFragmentTab" &gt;
  
  &lt;ListView 
      android:id="@id/android:list" 
      android:layout_height="match_parent"
      android:layout_width="match_parent"&gt;
  
   &lt;/ListView&gt;
  </code></pre>
  
  <p></p>
  
  <p>DBAdapter</p>
  
  <pre><code>public String[] getScheduleRecord(String lecid) {
      // TODO Auto-generated method stub
  
      // Get calendar set to current date and time
      Calendar c = Calendar.getInstance();
      c.add(Calendar.MINUTE, -15);
  
      // Print dates of the current week starting on Sunday
      SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
      //sdf.format(c.getTime());
      SimpleDateFormat sdfTime = new SimpleDateFormat("hh:mm:ss a");
  
       try {
          String arrData[] = null;
  
          String strSQL = "SELECT * FROM " + TABLE_SCHEDULE + " WHERE lecturer_id=? AND schedule_day=? AND schedule_endtime &gt; ?"; 
           Cursor cursor = mDb.rawQuery(strSQL, new String[]{lecid, sdf.format(c.getTime()), sdfTime.format(c.getTime())});
  
              if(cursor != null)
              {
                  if (cursor.moveToFirst()) {
                      arrData = new String[cursor.getCount()];
  
                      int i= 0;
                      do {                
                          arrData[i] = cursor.getString(1) 
                                   + " - " + cursor.getString(2);
                          i++;
                      } while (cursor.moveToNext());                      
  
                  }
              }
  
              cursor.close();
  
              return arrData;
  
       } catch (Exception e) {
          return null;
       }
  }
  </code></pre>
  
  <p>LogCat Output</p>
  
  <pre><code>08-07 04:48:19.271: E/AndroidRuntime(25461): FATAL EXCEPTION: main
  08-07 04:48:19.271: E/AndroidRuntime(25461): Process: com.example.fyp2, PID: 25461
  08-07 04:48:19.271: E/AndroidRuntime(25461): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fyp2/com.example.fyp2.LecTakeAtt}: java.lang.NullPointerException: storage == null
  08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
  08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
  08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
  08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
  08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.os.Handler.dispatchMessage(Handler.java:102)
  08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.os.Looper.loop(Looper.java:136)
  08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.app.ActivityThread.main(ActivityThread.java:5017)
  08-07 04:48:19.271: E/AndroidRuntime(25461):    at java.lang.reflect.Method.invokeNative(Native Method)
  08-07 04:48:19.271: E/AndroidRuntime(25461):    at java.lang.reflect.Method.invoke(Method.java:515)
  08-07 04:48:19.271: E/AndroidRuntime(25461):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
  08-07 04:48:19.271: E/AndroidRuntime(25461):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  08-07 04:48:19.271: E/AndroidRuntime(25461):    at dalvik.system.NativeStart.main(Native Method)
  08-07 04:48:19.271: E/AndroidRuntime(25461): Caused by: java.lang.NullPointerException: storage == null
  08-07 04:48:19.271: E/AndroidRuntime(25461):    at java.util.Arrays$ArrayList.&lt;init&gt;(Arrays.java:38)
  08-07 04:48:19.271: E/AndroidRuntime(25461):    at java.util.Arrays.asList(Arrays.java:155)
  08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.widget.ArrayAdapter.&lt;init&gt;(ArrayAdapter.java:128)
  08-07 04:48:19.271: E/AndroidRuntime(25461):    at com.example.fyp2.LecMonFragmentTab.onCreate(LecMonFragmentTab.java:43)
  08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.support.v4.app.Fragment.performCreate(Fragment.java:1763)
  08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:913)
  08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
  08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
  08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
  08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
  08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
  08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.app.Activity.performStart(Activity.java:5241)
  08-07 04:48:19.271: E/AndroidRuntime(25461):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
  08-07 04:48:19.271: E/AndroidRuntime(25461):    ... 11 more
  </code></pre>
  
  <p>Hope that you all can give some tips or provide some solution. Appreciate it.</p>