title: Very weird Android NullPointerException
body: <p>I've been working on populating a <code>ListView</code> with data I've retrieved from my sqlite database,
  the code from my database file <code>DBMainAdapter</code> works perfectly (snippet to save space) I know this because when I call my <code>getAll()</code> inside a separate activity I'm using (not included) i get my <code>ArrayList</code> and I can use it perfectly fine to print out the contents inside however I've noticed that when ever I try to do the same thing inside my <code>ProfileListActivity</code> I keep getting a <code>NullPointerException</code> thrown. What am I doing wrong?!</p>
  
  <p>Error Log:</p>
  
  <pre><code>04-21 06:42:31.669: W/dalvikvm(6107): threadid=1: thread exiting with uncaught exception (group=0xb3a8db90)
  04-21 06:42:31.749: E/AndroidRuntime(6107): FATAL EXCEPTION: main
  04-21 06:42:31.749: E/AndroidRuntime(6107): Process: com.test.profilekeeper, PID: 6107
  04-21 06:42:31.749: E/AndroidRuntime(6107): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.profilekeeper/com.test.profilekeeper.ProfileListActivity}: java.lang.NullPointerException
  04-21 06:42:31.749: E/AndroidRuntime(6107):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
  04-21 06:42:31.749: E/AndroidRuntime(6107):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
  04-21 06:42:31.749: E/AndroidRuntime(6107):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
  04-21 06:42:31.749: E/AndroidRuntime(6107):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
  04-21 06:42:31.749: E/AndroidRuntime(6107):     at android.os.Handler.dispatchMessage(Handler.java:102)
  04-21 06:42:31.749: E/AndroidRuntime(6107):     at android.os.Looper.loop(Looper.java:137)
  04-21 06:42:31.749: E/AndroidRuntime(6107):     at android.app.ActivityThread.main(ActivityThread.java:4998)
  04-21 06:42:31.749: E/AndroidRuntime(6107):     at java.lang.reflect.Method.invokeNative(Native Method)
  04-21 06:42:31.749: E/AndroidRuntime(6107):     at java.lang.reflect.Method.invoke(Method.java:515)
  04-21 06:42:31.749: E/AndroidRuntime(6107):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
  04-21 06:42:31.749: E/AndroidRuntime(6107):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
  04-21 06:42:31.749: E/AndroidRuntime(6107):     at dalvik.system.NativeStart.main(Native Method)
  04-21 06:42:31.749: E/AndroidRuntime(6107): Caused by: java.lang.NullPointerException
  04-21 06:42:31.749: E/AndroidRuntime(6107):     at com.test.profilekeeper.LoadedData.&lt;init&gt;(LoadedData.java:17)
  04-21 06:42:31.749: E/AndroidRuntime(6107):     at com.test.profilekeeper.ProfileListActivity.onCreate(ProfileListActivity.java:23)
  04-21 06:42:31.749: E/AndroidRuntime(6107):     at android.app.Activity.performCreate(Activity.java:5243)
  04-21 06:42:31.749: E/AndroidRuntime(6107):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
  04-21 06:42:31.749: E/AndroidRuntime(6107):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
  04-21 06:42:31.749: E/AndroidRuntime(6107):     ... 11 more
  </code></pre>
  
  <p>Where at com.test.profilekeeper.LoadedData.(LoadedData.java:17) : ArrayList listdata = dbhelper.getAll(); //constructor</p>
  
  <p>Where at com.test.profilekeeper.ProfileListActivity.onCreate(ProfileListActivity.java:23) : LoadedData mydata = new LoadedData();</p>
  
  <pre><code>public class DBMainAdapter{
      DBMain dbhelper;
  
      public DBMainAdapter(Context context){
          dbhelper = new DBMain(context);
      }
  
      public long insertprofiledata(String fname, String lname, String age, String gender, String username, String password){
          SQLiteDatabase db = dbhelper.getWritableDatabase();
          ContentValues cv = new ContentValues();
  
          cv.put(DBMain.FIRSTNAME, fname);
          cv.put(DBMain.LASTNAME, lname);
          cv.put(DBMain.AGE, age);
          cv.put(DBMain.GENDER, gender);
          cv.put(DBMain.USERNAME, username);
          cv.put(DBMain.PASSWORD, password);
  
          long id = db.insert(DBMain.TABLE_NAME, null, cv);
          return id;
      }
  
      public ArrayList&lt;String[]&gt; getAll(){
          SQLiteDatabase db = dbhelper.getWritableDatabase();
          String[] columns = {DBMain.UID, DBMain.FIRSTNAME, DBMain.LASTNAME, DBMain.AGE, DBMain.GENDER};
          Cursor curs = db.query(DBMain.TABLE_NAME, columns, null, null, null, null, null);
  
          int index1 = curs.getColumnIndex(DBMain.UID);
          int index2 = curs.getColumnIndex(DBMain.FIRSTNAME);
          int index3 = curs.getColumnIndex(DBMain.LASTNAME);
          int index4 = curs.getColumnIndex(DBMain.AGE);
          int index5 = curs.getColumnIndex(DBMain.GENDER);
  
          StringBuffer buffer = new StringBuffer();
  
          while(curs.moveToNext()){
              String uid = curs.getString(index1);
              String fname = curs.getString(index2);
              String lname = curs.getString(index3);
              String age = curs.getString(index4);
              String gender = curs.getString(index5);
  
              buffer.append(uid+","+fname+","+lname+","+age+","+gender+" ");
          }
  
          String[] row = buffer.toString().split(" ");
          String profilebuilder[][] = new String[row.length][row[0].length()];
  
          for(int i = 0; i &lt; row.length; i++){
              String[] profile = row[i].split(",");
              for(int j = 0; j &lt; profile.length; j++){
                  profilebuilder[i][j] = profile[j];
              }
          }
  
          String[] ids = new String[profilebuilder.length];
          String[] firstname = new String[profilebuilder.length];
          String[] ages = new String[profilebuilder.length];
          String[] genders = new String[profilebuilder.length];
  
          for(int k = 0; k &lt; profilebuilder.length; k++){
              for(int l = 0; l &lt; profilebuilder[0].length; l++){
                  ids[k] = profilebuilder[k][0];
                  firstname[k] = profilebuilder[k][1];
                  ages[k] = profilebuilder[k][3];
                  genders[k] = profilebuilder[k][4];
              }
          }
  
          ArrayList&lt;String[]&gt; alldata = new ArrayList&lt;String[]&gt;();
          alldata.add(ids);
          alldata.add(firstname);
          alldata.add(ages);
          alldata.add(genders);       
  
          return alldata;
      }
  }
  
      public class LoadedData {
          DBMainAdapter dbhelper;
  
          String[] ids;
          String[] firstnames;
          String[] ages;
          String[] genders;
  
          public LoadedData(){
              ArrayList&lt;String[]&gt; listdata = dbhelper.getAll();
  
              ids =  new String[listdata.get(0).length];
              firstnames = new String[listdata.get(0).length];
              ages = new String[listdata.get(0).length];
              genders  = new String[listdata.get(0).length];
  
              ids = listdata.get(0);
              firstnames = listdata.get(1);
              ages = listdata.get(2);
              genders = listdata.get(3);
          }
  
          public String[] getIds() {
              return ids;
          }
  
          public String[] getFirstnames() {
              return firstnames;
          }
  
          public String[] getAges() {
              return ages;
          }
  
          public String[] getGenders() {
              return genders;
          }
      }
  
  
  public class ProfileListActivity extends Activity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          LoadedData mydata = new LoadedData();
          populateListView(mydata);   
      }
  
      private void populateListView(LoadedData mydata) {
          ArrayAdapter&lt;LoadedData&gt; adapter = new MyListAdapter(mydata);
          ListView list = (ListView) findViewById(R.id.listView);
          list.setAdapter(adapter);
      }
  
      private class MyListAdapter extends ArrayAdapter&lt;LoadedData&gt;{
          LoadedData mdata;
          public MyListAdapter(LoadedData data){
              super(ProfileListActivity.this, R.layout.row);
              this.mdata = data;
          }
  
          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
              View itemView = convertView;
              if(itemView == null){
                  itemView = getLayoutInflater().inflate(R.layout.row, parent, false);
              }
  
              TextView first_name = (TextView) itemView.findViewById(R.id.nameListTextView);
              TextView id = (TextView) itemView.findViewById(R.id.idListTextView);
              TextView age = (TextView) itemView.findViewById(R.id.ageListTextView);
              ImageView img = (ImageView) itemView.findViewById(R.id.imageViewList);
  
              id.setText(mdata.getIds()[position]);
              first_name.setText(mdata.getFirstnames()[position]);
              age.setText(mdata.getAges()[position]);
  
              if(mdata.getGenders()[position].equalsIgnoreCase("Male")){
                  img.setImageResource(R.raw.man_thumb);
              }else{
                  img.setImageResource(R.raw.woman_thumb);
              }   
  
              return itemView;
          }
      }
  }
  </code></pre>