Reputation: 31
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ferryfinal/com.example.ferryfinal.Timings}: java.lang.NullPointerException
M getting null pointer exception in calling i function ie (in Timings.java calling display_all) i guess.
//CommentsDataSource.java
public List<Comment> display_all()
{
// String args[]={ref_id};
Comment comment=new Comment();
//String q = "SELECT * FROM timings where ref_id = ?";
String q= " select * from timings ";
Cursor cursor=database.rawQuery(q, null);
List<Comment> list = new ArrayList<Comment>();
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
comment = cursorToComment(cursor);
list.add(comment);
cursor.moveToNext();
}
// make sure to close the cursor
// cursor.close();
Log.w(MySQLiteHelper.class.getName(), "in display fn"+comment.get_Ref_Id());
return list;
}
//Route1.java
ferry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String ref_id="1";
Intent intent = new Intent(Route1.this, Timings.class);
intent.putExtra(EXTRA_MESSAGE, ref_id);
Log.w(MySQLiteHelper.class.getName(),intent.getStringExtra(EXTRA_MESSAGE));
startActivity(intent);
}
});
}
//Timings.java
public class Timings extends Activity {
final CommentsDataSource dt=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timings);
Intent intent = getIntent();
String message = intent.getStringExtra(Route1.EXTRA_MESSAGE);
Log.w(MySQLiteHelper.class.getName(),message+"hello");
//String message = "1";
List<Comment> values = dt.display_all();
Log.w(MySQLiteHelper.class.getName(),values.toString()+"hello1");
StringBuilder builder = new StringBuilder();
Log.w(MySQLiteHelper.class.getName(),builder+"hello2");
for (Comment c: values)
{
Log.w(MySQLiteHelper.class.getName(),message+"hello3");
builder.append(c.get_Ref_Id()).append(";")
.append(c.get_Starting_From()).append(";")
.append(c.get_Departure_Time()).append(";")
.append(c.get_Destination()).append("_");
Log.w(MySQLiteHelper.class.getName(),builder+"hello4");
}
//tv.setText(builder.toString());
......
Upvotes: 0
Views: 94
Reputation: 14590
Based on your code you are getting the one of these lines in Timings.java class..
List<Comment> values = dt.display_all();
you are not initialized dt
..initialize it first
Upvotes: 1
Reputation: 23638
In your Timings.java you have not initialized the final CommentsDataSource dt=null;
.
Declare dt
in your onCreate()
as below:
dt=new CommentsDataSource();
Upvotes: 0