title: Android Studio - Display data from database to TextView
body: <p>I'm trying to display the data I've inserted into my database to TextView through onMarkerClick. I've also checked the solution for <a href="https://stackoverflow.com/questions/14179148/display-data-from-database-into-textview">this question</a> and other questions and tutorial videos but I would always get this error:</p>
  
  <pre><code>01-27 10:33:49.747    8287-8287/com.example.diana.ippv2 D/DatabaseHandler﹕ Database created
  01-27 10:33:49.747    8287-8287/com.example.diana.ippv2 D/AndroidRuntime﹕ Shutting down VM
  01-27 10:33:49.747    8287-8287/com.example.diana.ippv2 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41d2d2a0)
  01-27 10:33:49.757    8287-8287/com.example.diana.ippv2 E/AndroidRuntime﹕ FATAL EXCEPTION: main
      java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.diana.ippv2/com.example.diana.ippv2.MainActivity}: java.lang.NullPointerException
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2016)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
              at android.app.ActivityThread.access$700(ActivityThread.java:134)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
              at android.os.Handler.dispatchMessage(Handler.java:99)
              at android.os.Looper.loop(Looper.java:137)
              at android.app.ActivityThread.main(ActivityThread.java:4867)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:511)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
              at dalvik.system.NativeStart.main(Native Method)
       Caused by: java.lang.NullPointerException
              at android.app.Activity.findViewById(Activity.java:1859)
              at com.example.diana.ippv2.MapFragment.&lt;init&gt;(MapFragment.java:20)
              at com.example.diana.ippv2.MainActivity.&lt;init&gt;(MainActivity.java:10)
              at java.lang.Class.newInstanceImpl(Native Method)
              at java.lang.Class.newInstance(Class.java:1319)
              at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2007)
              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
              at android.app.ActivityThread.access$700(ActivityThread.java:134)
              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
              at android.os.Handler.dispatchMessage(Handler.java:99)
              at android.os.Looper.loop(Looper.java:137)
              at android.app.ActivityThread.main(ActivityThread.java:4867)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:511)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
              at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Here's my LotData class</p>
  
  <pre><code>public class LotData
  {
      private String _address, _area, _utilities, _accessibility, _price;
      private int _id;
  
      public LotData(int id, String address, String area, String utilities, String accessibility, String price)
      {
          _id = id;
          _address = address;
          _area = area;
          _utilities = utilities;
          _accessibility = accessibility;
          _price = price;
      }
  
      public int getId() { return _id; }
      public String getAddress() { return _address; }
      public String getArea() { return _area; }
      public String getUtilities() { return _utilities; }
      public String getAccessibility() { return _accessibility; }
      public String getPrice() { return _price; }
  }
  </code></pre>
  
  <p>DatabaseHandler class:</p>
  
  <pre><code>public class DatabaseHandler extends SQLiteOpenHelper
  {
      public static final int DATABASE_VERSION = 1;
      public static final String DATABASE_NAME = "lotManager", TABLE_NAME = "lot_data", LOT_ID = "lot_id", LOT_ADDRESS = "lot_address", LOT_AREA = "lot_area", LOT_UTILITIES = "lot_utilities", LOT_ACCESSIBILITY = "lot_accessibility", LOT_PRICE = "lot_price";
  
      public DatabaseHandler(Context context)
      {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
  
          Log.d("DatabaseHandler", "Database created");
      }
  
      //Creating tables
      @Override
      public void onCreate(SQLiteDatabase db)
      {
          db.execSQL("CREATE TABLE" + TABLE_NAME + "(" + LOT_ID + "INTEGER PRIMARY KEY AUTOINCREMENT," + LOT_ADDRESS + "TEXT," + LOT_AREA + "TEXT," + LOT_UTILITIES + "TEXT," + LOT_ACCESSIBILITY + "TEXT," + LOT_PRICE + "TEXT");
  
          Log.d("DatabaseHandler", "Table created");
      }
  
      //Upgrading database
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
      {
          db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
  
          onCreate(db);
      }
  
      //Adding new data
      public void addData(LotData lotData)
      {
          SQLiteDatabase db = this.getWritableDatabase();
          ContentValues values = new ContentValues();
  
          values.put(LOT_ADDRESS, lotData.getAddress());
          values.put(LOT_AREA, lotData.getArea());
          values.put(LOT_UTILITIES, lotData.getUtilities());
          values.put(LOT_ACCESSIBILITY, lotData.getAccessibility());
          values.put(LOT_PRICE, lotData.getPrice());
  
          db.insert(TABLE_NAME, null, values);
          db.close();
      }
  
      //Retrieving data
      public LotData getData(int id)
      {
          SQLiteDatabase db = this.getReadableDatabase();
          Cursor cursor = db.query(TABLE_NAME, new String[] { LOT_ID, LOT_ADDRESS, LOT_AREA, LOT_UTILITIES, LOT_ACCESSIBILITY, LOT_PRICE }, LOT_ID + "=?", new String[]{ String.valueOf(id) }, null, null, null, null);
  
          if (cursor != null)
              cursor.moveToFirst();
  
          LotData data = new LotData(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5));
  
          db.close();
          cursor.close();
          return data;
      }
  
      //Deleting single data
      public void deleteData (LotData lotData)
      {
          SQLiteDatabase db = getReadableDatabase();
          db.delete(TABLE_NAME, LOT_ID + "=?", new String[] { String.valueOf(lotData.getId()) });
          db.close();
      }
  
      //Updating single data from database
      public int updateData(LotData lotData)
      {
          SQLiteDatabase db = getReadableDatabase();
          ContentValues values = new ContentValues();
  
          values.put(LOT_ADDRESS, lotData.getAddress());
          values.put(LOT_AREA, lotData.getArea());
          values.put(LOT_UTILITIES, lotData.getUtilities());
          values.put(LOT_ACCESSIBILITY, lotData.getAccessibility());
          values.put(LOT_PRICE, lotData.getPrice());
  
          return db.update(TABLE_NAME, values, LOT_ID + "=?", new String[] { String.valueOf(lotData.getId()) });
      }
  
      public int getDataCount()
      {
          SQLiteDatabase db = getReadableDatabase();
          Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
          int count = cursor.getCount();
          db.close();
          cursor.close();
  
          return count;
      }
  
      public List&lt;LotData&gt; getAllDatas()
      {
          List&lt;LotData&gt; dataList = new ArrayList&lt;LotData&gt;();
  
          SQLiteDatabase db = getWritableDatabase();
          Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
  
          if (cursor.moveToFirst())
          {
              do
              {
                  LotData data = new LotData(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5)); dataList.add(data);
              }
  
              while (cursor.moveToNext());
          }
          return dataList;
      }
  }
  </code></pre>
  
  <p>MapFragment class:</p>
  
  <pre><code>public class MapFragment extends FragmentActivity implements OnMapReadyCallback, OnMarkerClickListener
  {
      List&lt;LotData&gt; dataList;
      DatabaseHandler dbHandler = new DatabaseHandler(this);
      TextView lotAddress = (TextView) findViewById(R.id.addressTV);
      TextView lotArea = (TextView) findViewById(R.id.areaTV);
      TextView lotUtilities = (TextView) findViewById(R.id.utilTV);
      TextView lotAccessibility = (TextView) findViewById(R.id.accsTV);
  
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.map_layout);
  
          SupportMapFragment sMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
          sMapFragment.getMapAsync(this);
  
          Log.d("Insert: ", "Inserting data..");
          dbHandler.addData(new LotData(1, "Jaro Iloilo", "24", "Water and Electrical Supply", "Market, Hospital, Police Station," + "School", "Unknown"));
  
          Log.d("Reading: ", "Reading all lot data..");
          dataList = dbHandler.getAllDatas();
  
          for (LotData lotData : dataList)
          {
              String log = "ID: " + lotData.getId() + "Address: " + lotData.getAddress() + "Area: " + lotData.getArea() + "Utilities: " + lotData.getUtilities() + "Accessibility: " + lotData.getAccessibility() + "Price: " + lotData.getPrice();
  
              //Writing to log
              Log.d("Address: ", log);
          }
      }
  
      @Override
      public void onMapReady(GoogleMap mMap)
      {
          LatLng one = new LatLng(10.7187530, 122.5611620);
          LatLng ILOILO = new LatLng(10.730278, 122.548889);
  
          mMap.setBuildingsEnabled(true);
          mMap.setMyLocationEnabled(true);
          mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
          mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ILOILO, 12));
  
          Marker oneJRD = mMap.addMarker(new MarkerOptions()
                              .title("Lot ID: 0115-0001JRD")
                              .snippet("Click for more info")
                              .position(one)
                              .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
  
          oneJRD.showInfoWindow();
          mMap.setOnMarkerClickListener(this);
      }
  
      @Override
      public boolean onMarkerClick (Marker oneJRD)
      {
          setContentView(R.layout.info_layout);
  
          LotData lotData = dbHandler.getData(0);
  
          lotAddress.setText("Address: " + lotData.getAddress());
          lotArea.setText("Area: " + lotData.getArea());
          lotUtilities.setText("Utilities: " + lotData.getUtilities());
          lotAccessibility.setText("Accessibility: " + lotData.getAccessibility());
  
          return false;
      }
  }
  </code></pre>