java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ferryfinal/com.example.ferryfinal.Timings}: java.lang.NullPointerException
我在调用i函数时得到空指针异常,即(在 Timings.java 中调用 display_all )我想。
// 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());
......
答案 0 :(得分:1)
根据您的代码,您将获得Timings.java类中的其中一行。
List<Comment> values = dt.display_all();
您尚未初始化dt
..首先初始化
答案 1 :(得分:0)
在 Timings.java 中,您尚未初始化final CommentsDataSource dt=null;
。
在dt
中声明onCreate()
,如下所示:
dt=new CommentsDataSource();