Note that there are some explanatory texts on larger screens.

plurals
  1. PONullPointeException on StartActivity from button
    text
    copied!<p>I'm trying to launch an actvivity via onClickListener of a button, and always I get Null pointer exception. I've searched and tried a lot of different things, and I cannot find where I have made a mistake. Here is the related code/classes</p> <p>The crashing activity:</p> <pre><code> public class DisplayData extends Activity{ TextView displayData; DataObjectsProvider provider; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.displaydb); provider = new DataObjectsProvider(this); displayData = (TextView) findViewById(R.id.tvDisplayData); displayData.setText(provider.getAlldobjects().toString()); } } </code></pre> <p>(I've tried commended out the provider and setText code)</p> <p>The onClickListener from the working main activity:</p> <pre><code>bDisplay.setOnClickListener(new OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MainActivity.this, DisplayData.class); startActivity(intent); } }); </code></pre> <p>Here is the layout for the crashing activity:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/scrollView1" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;TextView android:id="@+id/tvDisplayData" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="showData" android:textAppearance="?android:attr/textAppearanceMedium" /&gt; &lt;/ScrollView&gt; </code></pre> <p>(I've already tried and with only a linear layout and textview)</p> <p>and here is the Android manifest code:</p> <pre><code>&lt;activity android:name=".MainActivity" android:label="@string/title_activity_main" &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=".DisplayData" android:label="" &gt; &lt;/activity&gt; </code></pre> <p>Am I doing something wrong? Is there any mistake which I don't see or I'm missing something?</p> <p><strong>EDIT:</strong> log:</p> <blockquote> <p>02-09 23:52:35.002: W/dalvikvm(24849): threadid=1: thread exiting with uncaught exception (group=0x40c971f8) 02-09 23:52:35.052: E/AndroidRuntime(24849): FATAL EXCEPTION: main 02-09 23:52:35.052: E/AndroidRuntime(24849): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.d69.dancesongsdb/com.d69.dancesongsdb.DisplayData}: java.lang.NullPointerException 02-09 23:52:35.052: E/AndroidRuntime(24849): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 02-09 23:52:35.052: E/AndroidRuntime(24849): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 02-09 23:52:35.052: E/AndroidRuntime(24849): at android.app.ActivityThread.access$600(ActivityThread.java:123) 02-09 23:52:35.052: E/AndroidRuntime(24849): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 02-09 23:52:35.052: E/AndroidRuntime(24849): at android.os.Handler.dispatchMessage(Handler.java:99) 02-09 23:52:35.052: E/AndroidRuntime(24849): at android.os.Looper.loop(Looper.java:137) 02-09 23:52:35.052: E/AndroidRuntime(24849): at android.app.ActivityThread.main(ActivityThread.java:4424) 02-09 23:52:35.052: E/AndroidRuntime(24849): at java.lang.reflect.Method.invokeNative(Native Method) 02-09 23:52:35.052: E/AndroidRuntime(24849): at java.lang.reflect.Method.invoke(Method.java:511) 02-09 23:52:35.052: E/AndroidRuntime(24849): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787) 02-09 23:52:35.052: E/AndroidRuntime(24849): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554) 02-09 23:52:35.052: E/AndroidRuntime(24849): at dalvik.system.NativeStart.main(Native Method) 02-09 23:52:35.052: E/AndroidRuntime(24849): Caused by: java.lang.NullPointerException 02-09 23:52:35.052: E/AndroidRuntime(24849): at com.d69.dancesongsdb.DataObjectsProvider.getAlldobjects(DataObjectsProvider.java:58) 02-09 23:52:35.052: E/AndroidRuntime(24849): at com.d69.dancesongsdb.DisplayData.onCreate(DisplayData.java:19) 02-09 23:52:35.052: E/AndroidRuntime(24849): at android.app.Activity.performCreate(Activity.java:4465) 02-09 23:52:35.052: E/AndroidRuntime(24849): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 02-09 23:52:35.052: E/AndroidRuntime(24849): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 02-09 23:52:35.052: E/AndroidRuntime(24849): ... 11 more</p> </blockquote> <p><strong>EDIT2</strong> I've abandoned db4o and recreated my classes with sqlite instead. But this error still happens on the activity. I've edited the code of crashing activity also. The latest line(which use the provider) is the one which causes the crash. I've tested it on the main activity by changing the the code of the button which start the activity with the below code:</p> <pre><code>public void onClick(View v) { Toast.makeText(MainActivity.this, provider.getAlldobjects().toString(), Toast.LENGTH_LONG).show(); </code></pre> <p>and the database' s items displaying correct on toast. On the main activity(from which is the above code) I've declared the DataObjectsProvider class with exactly the same way as on the crashing activity. I cannot understand why on the one activity works and not on the other.</p> <p>DataObjectsProvider:</p> <pre><code>public class DataObjectsProvider { // Database fields private SQLiteDatabase database; private DbHelper dbHelper; private String[] allColumns = { DbHelper.COLUMN_ID, DbHelper.COLUMN_SONGNAME, DbHelper.COLUMN_SONGARTIST, DbHelper.COLUMN_SONGGENRE }; public DataObjectsProvider (Context context) { dbHelper = new DbHelper(context); } public void open() throws SQLException { database = dbHelper.getWritableDatabase(); } public void close() { dbHelper.close(); } public DataObjects createDataObjects (String songName, String songArtist, String songGenre){ ContentValues values = new ContentValues(); values.put(DbHelper.COLUMN_SONGNAME, songName + ", "); values.put(DbHelper.COLUMN_SONGARTIST, songArtist + ", "); values.put(DbHelper.COLUMN_SONGGENRE, songGenre); long insertId = database.insert(DbHelper.TABLE_SONGS, null, values); Cursor cursor = database.query(DbHelper.TABLE_SONGS, allColumns, DbHelper.COLUMN_ID + " = " + insertId, null, null, null, null); cursor.moveToFirst(); DataObjects newDobject = cursorToDataObjects(cursor); cursor.close(); return newDobject; } public void deleteDobjects(DataObjects dobjects) { long id = dobjects.getId(); System.out.println("Comment deleted with id: " + id); database.delete(DbHelper.TABLE_SONGS, DbHelper.COLUMN_ID + " = " + id, null); } public List&lt;DataObjects&gt; getAlldobjects() { List&lt;DataObjects&gt; dobjects = new ArrayList&lt;DataObjects&gt;(); Cursor cursor = database.query(DbHelper.TABLE_SONGS, allColumns, null, null, null, null, null); cursor.moveToFirst(); while (!cursor.isAfterLast()) { DataObjects dobject = cursorToDataObjects(cursor); dobjects.add(dobject); cursor.moveToNext(); } // Make sure to close the cursor cursor.close(); return dobjects; } private DataObjects cursorToDataObjects(Cursor cursor) { DataObjects dobject = new DataObjects(); dobject.setId(cursor.getLong(0)); dobject.setSongName(cursor.getString(1)); dobject.setSongArtist(cursor.getString(2)); dobject.setSongGenre(cursor.getString(3)); return dobject; } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload