title: How to solve NullPointerExpression in Android?
body: <p>I have a problem while using a database. When I run my SQLView.java, I get a fatal exception. I'll post all my database-related code.</p>
  
  <p>See my commenting lines on Database class (line 71), and SQLView class (Line 14). Maybe there is something wrong? I've tried many things like put in: this, of null. </p>
  
  <p>Also, I've read all related nullpointerexception-question on this website. </p>
  
  <p>To begin: my Database class:</p>
  
  <pre><code>    package com.jacob.eindproject;
  
  import android.content.ContentValues;
  import android.content.Context;
  import android.database.Cursor;
  import android.database.SQLException;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  import android.database.sqlite.SQLiteDatabase.CursorFactory;
  
  import java.sql.*;
  
  public class Database {
  
      public static final String KEY_ROWID = "_id";
      public static final String KEY_NAME = "persons_name";
      public static final String KEY_HOTNESS = "persons_hotness";
  
      private static final String DATABASE_NAME = "Databasedb";
      private static final String DATABASE_TABLE = "peopleTable";
      private static final int DATABASE_VERSION = 1;
  
      private DbHelper ourHelper;
      private final Context ourContext;
      private SQLiteDatabase ourDatabase;
  
      private static class DbHelper extends SQLiteOpenHelper{
  
          public DbHelper(Context context) {
              super(context, DATABASE_NAME, null, DATABASE_VERSION);
              // TODO Auto-generated constructor stub
          }
  
          @Override
          public void onCreate(SQLiteDatabase db) {
              db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                      KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                      KEY_NAME + " TEXT NOT NULL, " +
                      KEY_HOTNESS + " TEXT NOT NULL);"                    
          );
  
  
          }
  
          @Override
          public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
              db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
              onCreate(db);
          }
  
          public void close(Database database) {
              // TODO Auto-generated method stub
  
          }
      }
  
  
  
      public Database(Context c){
          ourContext = c;
      }
  
      public Database open() throws SQLException{
          ourHelper = new DbHelper(ourContext);
          ourDatabase = ourHelper.getWritableDatabase();
          return this;        
      }
  
      public void close() {
  
      ourHelper.close(); //This is line 71, where also an error is given, maybe there should me text between the brackets ()?
  }
  
  
  
      public long createEntry(String name, String hotness) {
          ContentValues cv = new ContentValues();
          cv.put(KEY_NAME, name);
          cv.put(KEY_HOTNESS, hotness);
          return ourDatabase.insert(DATABASE_TABLE, null, cv);
  
      }
  
      public String getData() {
          // TODO Auto-generated method stub
          String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_HOTNESS};
          Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
          String result = "";
  
          int iRow = c.getColumnIndex(KEY_ROWID);
          int iName = c.getColumnIndex(DATABASE_NAME);
          int iHotness = c.getColumnIndex(KEY_HOTNESS);
  
          for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
              result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iHotness) + "\n";
  
  
          }
  
          return result;
      }
  }
  </code></pre>
  
  <p>The SQLite class, to fix the input:</p>
  
  <pre><code>package com.jacob.eindproject;
  
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  import android.app.Activity;
  import android.app.Dialog;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.view.View.OnClickListener;
  
  
  public class SQLite extends Activity implements View.OnClickListener {
  
      Button sqlUpdate, sqlView;
      EditText sqlName, sqlHotness;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.sqllite);
          sqlUpdate = (Button) findViewById(R.id.bSQLUpdate);
          sqlName = (EditText) findViewById(R.id.etSQLName);
          sqlHotness = (EditText) findViewById(R.id.etSQLHotness);
  
          sqlView = (Button) findViewById(R.id.bSQLopenView);
          sqlView.setOnClickListener((android.view.View.OnClickListener) this);
          sqlUpdate.setOnClickListener((android.view.View.OnClickListener) this);
      }
  
      public void onClick(View arg0) {
          switch (arg0.getId()) {
          case R.id.bSQLUpdate:
  
  
              boolean didItWork = true;
              try{            
              String name = sqlName.getText().toString();
              String hotness = sqlHotness.getText().toString();
  
              Database entry = new Database(SQLite.this);
              entry.open();
              entry.createEntry(name, hotness);
              entry.close();
  
              }catch (Exception e ){
                  didItWork = false;
  
              }finally{
                  if (didItWork){
                      Dialog d = new Dialog(this);
                      d.setTitle("Heak Yeay");
                      TextView tv = new TextView(this);
                      tv.setText("Succes");
                      d.setContentView(tv);
                      d.show();
              }
          }
  
              break;
          case R.id.bSQLopenView:
              Intent i = new Intent("com.jacob.eindproject.SQLVIEW");
              startActivity(i);
  
  
          }
          }
  
      public void onClick(DialogInterface arg0, int arg1) {
          // TODO Auto-generated method stub
  
      }
  
      }
  </code></pre>
  
  <p>After that, my SQLView class, to view the input from the SQLite class:</p>
  
  <pre><code>package com.jacob.eindproject;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.widget.TextView;
  
  public class SQLView extends Activity{
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.sqlview);
          TextView tv = (TextView) findViewById(R.id.tvSQLinfo);
          Database info = new Database(this); //as you can see in the logcat, this is line 14, where the error is. Maybe the "this" isn't right?
  
          info.open();
          String data = info.getData();
          info.close();
          tv.setText(data);
  
  
  
  
  
      }
  
  }
  </code></pre>
  
  <p>Now, my xml files, maybe I haven't configured a button not well, or something?</p>
  
  <p>SQLView.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"&gt;
  
  
      &lt;TableLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tableLayout1"&gt;
  
          &lt;TableRow&gt;
  
                  &lt;TextView android:text="@string/Names" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"/&gt;
  
                  &lt;TextView android:text="@string/Hotness" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" /&gt;
  
          &lt;/TableRow&gt;
  
  
      &lt;/TableLayout&gt;
  
          &lt;TextView android:id="@+id/tvSQLinfo" android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="@string/info"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>The sqllite.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
      &lt;LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"&gt;
  
  
      &lt;TextView
          android:id="@+id/Naam" 
          android:text="@string/Naam"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceSmall" /&gt;
  
      &lt;EditText
          android:id="@+id/etSQLName"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"&gt;
      &lt;/EditText&gt;
  
      &lt;TextView
          android:id="@+id/hotness" 
          android:text="@string/hotnessscale"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
      /&gt; 
  
      &lt;EditText
          android:id="@+id/etSQLHotness"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"&gt;
      &lt;/EditText&gt;    
  
      &lt;Button
          android:id="@+id/bSQLUpdate"
          android:text="@string/Update"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" &gt;&lt;/Button&gt;
  
  
  
      &lt;Button
          android:id="@+id/bSQLopenView"
          android:text="@string/View"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"    
      &gt;&lt;/Button&gt;    
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>The Menu class, to fix the menu, with an OnListItemClick (there are some other activities in there, such as Overgewicht. Don't mind these, I guess..?):</p>
  
  <pre><code>package com.jacob.eindproject;
  
  import android.app.ListActivity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.AdapterView.OnItemClickListener;
  import android.widget.ArrayAdapter;
  import android.widget.ListView;
  
  public class Menu extends ListActivity implements OnItemClickListener { 
  
  
      String classes[] = { "BMI- Calculator", "Ondergewicht", "Gezond Gewicht", "Overgewicht", "Database", "Bekijk Database"};
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
          setListAdapter(new ArrayAdapter&lt;String&gt;(Menu.this, android.R.layout.simple_list_item_1, classes));
      }
  
      @Override
      protected void onListItemClick(ListView l, View v, int position, long id) {
      //Positie 0 is het eerste item (dus de BMI-Calculator.)
      super.onListItemClick(l, v, position, id);
  
      switch(position)
      {
      case 0: 
      Intent openStartingPoint = new Intent(getApplicationContext(),MainActivity.class);
      startActivity(openStartingPoint);   
      break;
  
      case 1: 
      Intent openOndergewicht = new Intent(getApplicationContext(),Ondergewicht.class);
      startActivity(openOndergewicht);
      break;
  
      case 2: 
      Intent openGezondgewicht = new Intent(getApplicationContext(),Gezond_gewicht.class);
      startActivity(openGezondgewicht);   
      break;
  
      case 3: 
      Intent openOvergewicht = new Intent(getApplicationContext(),Overgewicht.class);
      startActivity(openOvergewicht); 
  
      break;
  
  
      case 4: 
      Intent openDatabase = new Intent(getApplicationContext(),SQLite.class);
      startActivity(openDatabase);    
  
      break;
  
      case 5: 
      Intent openViewdatabase = new Intent(getApplicationContext(),SQLView.class);
      startActivity(openViewdatabase);    
  
      break;
  
      }
  
  
      }
  
      @Override
      public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) { 
          // TODO Auto-generated method stub
  
      }
  }
  </code></pre>
  
  <p>My Android Manifest file: (again, I've declared some other things, don't mind these!)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.jacob.eindproject"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="18" /&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
  
  
          &lt;activity
              android:name="com.jacob.eindproject.Menu"
              android:label="@string/app_name" &gt;
          &lt;/activity&gt;
  
          &lt;activity
              android:name="com.jacob.eindproject.Inleiding"
              android:label="@string/app_name" &gt;
                      &lt;intent-filter&gt;
                              &lt;action android:name="android.intent.action.MAIN" /&gt;
                              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
                      &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
          &lt;activity
              android:name="com.jacob.eindproject.MainActivity"
              android:label="@string/app_name" &gt;
                       &lt;intent-filter&gt;
                              &lt;action android:name="android.intent.action.MAIN" /&gt;
                              &lt;category android:name="android.intent.category.DEFAULT" /&gt;
                      &lt;/intent-filter&gt;
          &lt;/activity&gt;        
  
          &lt;activity
              android:name="com.jacob.eindproject.Ondergewicht"
              android:label="@string/app_name" &gt;
          &lt;/activity&gt;        
  
          &lt;activity
              android:name="com.jacob.eindproject.Gezond_gewicht"
              android:label="@string/app_name" &gt;
          &lt;/activity&gt;   
  
          &lt;activity
              android:name="com.jacob.eindproject.Overgewicht"
              android:label="@string/app_name" &gt;
          &lt;/activity&gt;
  
          &lt;activity
              android:name="com.jacob.eindproject.Database"
              android:label="@string/app_name" &gt;
                      &lt;intent-filter&gt;
                              &lt;action android:name="com.jacob.eindproject.DATABASE" /&gt;
                              &lt;category android:name="android.intent.category.DEFAULT" /&gt;
                      &lt;/intent-filter&gt;
          &lt;/activity&gt;                 
  
          &lt;activity
              android:name=".SQLView"
              android:label="@string/app_name" &gt;
                      &lt;intent-filter&gt;
                              &lt;action android:name="com.jacob.eindproject.SQLVIEW" /&gt;
                              &lt;category android:name="android.intent.category.DEFAULT" /&gt;
                      &lt;/intent-filter&gt;                     
  
          &lt;/activity&gt;           
  
  
          &lt;activity
              android:name="com.jacob.eindproject.SQLite"
              android:label="@string/app_name" &gt;
                      &lt;intent-filter&gt;
                              &lt;action android:name="com.jacob.eindproject.SQLITE" /&gt;
                              &lt;category android:name="android.intent.category.DEFAULT" /&gt;
                      &lt;/intent-filter&gt;
          &lt;/activity&gt;  
  
  
  
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>At last, my LogCat, to show you the error:</p>
  
  <pre><code>12-14 11:04:18.227: E/AndroidRuntime(1577): FATAL EXCEPTION: main
  12-14 11:04:18.227: E/AndroidRuntime(1577): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jacob.eindproject/com.jacob.eindproject.SQLView}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
  12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
  12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
  12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
  12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
  12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.os.Handler.dispatchMessage(Handler.java:99)
  12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.os.Looper.loop(Looper.java:137)
  12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.app.ActivityThread.main(ActivityThread.java:5103)
  12-14 11:04:18.227: E/AndroidRuntime(1577):     at java.lang.reflect.Method.invokeNative(Native Method)
  12-14 11:04:18.227: E/AndroidRuntime(1577):     at java.lang.reflect.Method.invoke(Method.java:525)
  12-14 11:04:18.227: E/AndroidRuntime(1577):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
  12-14 11:04:18.227: E/AndroidRuntime(1577):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  12-14 11:04:18.227: E/AndroidRuntime(1577):     at dalvik.system.NativeStart.main(Native Method)
  12-14 11:04:18.227: E/AndroidRuntime(1577): Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
  12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.database.CursorWindow.nativeGetString(Native Method)
  12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.database.CursorWindow.getString(CursorWindow.java:434)
  12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
  12-14 11:04:18.227: E/AndroidRuntime(1577):     at com.jacob.eindproject.Database.getData(Database.java:95)
  12-14 11:04:18.227: E/AndroidRuntime(1577):     at com.jacob.eindproject.SQLView.onCreate(SQLView.java:16)
  12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.app.Activity.performCreate(Activity.java:5133)
  12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
  12-14 11:04:18.227: E/AndroidRuntime(1577):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
  12-14 11:04:18.227: E/AndroidRuntime(1577):     ... 11 more
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>I have a new error now, please take a look at my LogCat. I think it's because I have a wrong column set or something? Thanks in advance.</p>