Get data from Cursor: null pointer exception <p>I've this two classes:</p> <pre><code>public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DatabaseHelper helper = new DatabaseHelper(this); String[] temp = new String[] { "40", "35", "28" }; String[] hum = new String[] { "50%", "30%", "80%" }; for (int i = 0; i &lt; temp.length; i++) { helper.insertRecord(temp[i], hum[i]); } Cursor c = helper.getData(); String res = ""; if(c.moveToFirst()) { while(!c.isAfterLast()) { res+=c.getString(0)+" "+c.getString(1)+"\n"; c.moveToNext(); } c.close(); } TextView tv = (TextView) findViewById(R.id.tv); tv.setText(res); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } public class DatabaseHelper extends SQLiteOpenHelper { private static final String DB_NAME = "database_name"; private static final String TABLE_NAME = "my_table"; private static final String COL_TEMPERATURA = "temperatura"; private static final String COL_UMIDITA = "umidita"; public DatabaseHelper(Context context) { super(context, DB_NAME, null, 33); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + COL_TEMPERATURA + " TEXT, " + COL_UMIDITA + " TEXT)"); } @Override public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) { // TODO Auto-generated method stub } public void insertRecord(String temperatura, String umidita) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put(COL_TEMPERATURA, temperatura); cv.put(COL_UMIDITA, umidita); db.insert(TABLE_NAME, null, cv); db.close(); } public Cursor getData() { SQLiteDatabase db = this.getReadableDatabase(); String[] cols = new String[]{COL_TEMPERATURA, COL_UMIDITA}; Cursor res = db.query(TABLE_NAME, cols, null, null, null, null, null); return res; } } </code></pre> <p>but whew i execute my code, i have a nullpointer exception on MainActivity when i call tv.setText(res);</p> <p>why?</p> <p>Log:</p> <pre><code>09-08 19:25:16.249: E/AndroidRuntime(5911): FATAL EXCEPTION: main 09-08 19:25:16.249: E/AndroidRuntime(5911): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.database/com.example.database.MainActivity}: java.lang.NullPointerException 09-08 19:25:16.249: E/AndroidRuntime(5911): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 09-08 19:25:16.249: E/AndroidRuntime(5911): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 09-08 19:25:16.249: E/AndroidRuntime(5911): at android.app.ActivityThread.access$600(ActivityThread.java:141) 09-08 19:25:16.249: E/AndroidRuntime(5911): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 09-08 19:25:16.249: E/AndroidRuntime(5911): at android.os.Handler.dispatchMessage(Handler.java:99) 09-08 19:25:16.249: E/AndroidRuntime(5911): at android.os.Looper.loop(Looper.java:137) 09-08 19:25:16.249: E/AndroidRuntime(5911): at android.app.ActivityThread.main(ActivityThread.java:5103) 09-08 19:25:16.249: E/AndroidRuntime(5911): at java.lang.reflect.Method.invokeNative(Native Method) 09-08 19:25:16.249: E/AndroidRuntime(5911): at java.lang.reflect.Method.invoke(Method.java:525) 09-08 19:25:16.249: E/AndroidRuntime(5911): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 09-08 19:25:16.249: E/AndroidRuntime(5911): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 09-08 19:25:16.249: E/AndroidRuntime(5911): at dalvik.system.NativeStart.main(Native Method) 09-08 19:25:16.249: E/AndroidRuntime(5911): Caused by: java.lang.NullPointerException 09-08 19:25:16.249: E/AndroidRuntime(5911): at com.example.database.MainActivity.onCreate(MainActivity.java:31) 09-08 19:25:16.249: E/AndroidRuntime(5911): at android.app.Activity.performCreate(Activity.java:5133) 09-08 19:25:16.249: E/AndroidRuntime(5911): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 09-08 19:25:16.249: E/AndroidRuntime(5911): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 09-08 19:25:16.249: E/AndroidRuntime(5911): ... 11 more </code></pre>