I have an activity which has a ListView
on it ive been trying to populate the list view with data from a String ArrayList
this is the activity code
package com.example.arun.db;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class List_view extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<String> throwlist = new ArrayList<String>();
ListView Trainlist;
Trainlist=(ListView)findViewById(R.id.station_list);
SQLiteDatabase db = openOrCreateDatabase("Station11.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
setContentView(R.layout.activity_list_view);
try{
Cursor data_fetch = db.rawQuery("Select Station_name From Station", null);
String[] station_array = new String[data_fetch.getCount()];
int i= 0;
while (data_fetch.moveToNext()) {
String name = data_fetch.getString(data_fetch.getColumnIndex("Station_name"));
station_array[i] = name;
Log.d("STATION_GET","Retrieved station " + station_array[i]);
//Toast.makeText(List_view.this, "Retrieved station " + station_array[i] , Toast.LENGTH_SHORT).show();
throwlist.add(name);
i++;
}
data_fetch.close();
}catch(Exception e)
{
Toast.makeText(List_view.this, "An Error occured Retrieving your data" , Toast.LENGTH_LONG).show();
}
try {
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, throwlist);
Trainlist.setAdapter(arrayAdapter);
}catch (Exception e){
Log.d("Error in listview ",e.toString());
Toast.makeText(List_view.this, "Listview Error" , Toast.LENGTH_LONG).show();
}
}
}
Ive identified That the Section `
try {
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, throwlist);
Trainlist.setAdapter(arrayAdapter);
}catch (Exception e){
Log.d("Error in listview ",e.toString());
Toast.makeText(List_view.this, "Listview Error" , Toast.LENGTH_LONG).show();
}`
throws the error
10-28 18:03:42.004 11549-11549/com.example.arun.db D/Error in listview: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
upon Further Investigation of this error i knew that this error could be caused by the list view not being declared in the xml file for the particular activity as told in here
But the ListView is clearly defined in the xml heres the xml code:
<ListView
android:id="@+id/station_list"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="1.0" />
Any Ideas As to Why this is happening