每当我尝试运行我的代码时,如果我尝试将数据动态插入数据库,我会得到null pointer exception
。
但是,如果我运行方法insertTestData(),数据将被放入数据库。
我希望button
将listview
项目的数据保存到数据库中。
我在onClickListener
的自定义适配器中设置了listview
。
然后我将数据传递回activities.java
类,然后传递给数据库适配器,但是当点击模拟器上的button
时,NullPointerException
出现open()
错误1}}数据库适配器上的方法。
我相信我正在初始化数据库。我是Android开发的新手,我们将不胜感激。
DatabaseAdapter class
public class LaunchesDatabaseAdapter extends SQLiteOpenHelper{
private SQLiteDatabase database;
public static final String DATABASE_NAME = "SpaceApplicationDatabase.db";
public static final String LAUNCHES_TABLE_NAME = "launches";
public static final String LAUNCHES_COLUMN_ID = "id";
public static final String LAUNCHES_COLUMN_NAME = "name";
public static final String LAUNCHES_COLUMN_DATE = "date";
public static final String LAUNCHES_COLUMN_TBDDATE = "tbd_date";
public static final String LAUNCHES_COLUMN_TBDTIME = "tbd_time";
public boolean insertLaunch(String name, String date, int tbdDate, int tbdTime){
//SQLiteDatabase db = getWritableDatabase();
ContentValues insertValues = new ContentValues();
insertValues.put(LAUNCHES_COLUMN_NAME, name);
insertValues.put(LAUNCHES_COLUMN_DATE, date);
insertValues.put(LAUNCHES_COLUMN_TBDDATE, tbdDate);
insertValues.put(LAUNCHES_COLUMN_TBDTIME, tbdTime);
long result = database.insert(LAUNCHES_TABLE_NAME, null, insertValues);
if (result == -1){
return false;
}else {
Log.i("Enter into: ", name + ", " + date + ", " + tbdDate + ", " + tbdTime);
return true;
}
}
public boolean insertTestData(){
//TODO DELETE THIS METHOD
SQLiteDatabase db = this.getWritableDatabase();
ContentValues insertValues = new ContentValues();
insertValues.put("name", "Atlas V 401 | NROL-79");
insertValues.put("date", "March 1, 2017 14:30:00 UTC");
insertValues.put("tbd_date", 0);
insertValues.put("tbd_time", 0);
db.insert("launches", null, insertValues);
return true;
}
public void open() throws SQLException {
database = this.getWritableDatabase();
}
}
ListView Adapter class
public class LaunchAdapter extends ArrayAdapter<Launches> {
public LaunchAdapter(Activity context, List<Launches> launchesList){
super(context, 0, launchesList);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
final LaunchesDatabaseAdapter databaseAdapter;
final Launches launch = getItem(position);
if (convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
}
/*TODO CHANGE SO THAT ICON CAN BE CHANGED ACCORDING TO THE NAME
OR CHANGE SO THAT BUTTON COULD APPEAR INSTEAD OF ICON, IF NOT CHANGE THEN DISPLAY BUTTON ELSEWHERE*/
final Button saveButton = (Button) convertView.findViewById(R.id.buttonSave);
saveButton.setTag(launch.getId());
saveButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
UpcomingLaunches upcomingLaunches = new UpcomingLaunches();
upcomingLaunches.insertToDatabase(launch.getName(), launch.getDate(),
launch.getTbdDate(), launch.getTbdTime());
}
});
}
}
Activity Class
public class UpcomingLaunches extends AppCompatActivity implements View.OnClickListener {
private LaunchesDatabaseAdapter databaseAdapter;
private final String UPCOMING_LAUNCHES_URL = "https://launchlibrary.net/1.1/launch";
private final String UPCOMING_LAUNCHES_URL_NEXT = "https://launchlibrary.net/1.1/launch/next/10";
HttpHandler httpHandler = new HttpHandler();
private ProgressDialog launchesDialog;
private List<Launches> launchList = new ArrayList<Launches>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upcoming_launches);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
CheckInternet check = new CheckInternet();
check.setActivity(this);
new CheckInternet().execute();
//TODO ADD CHECK
new loadStation().execute(UPCOMING_LAUNCHES_URL);
Button nextButton = (Button) findViewById(R.id.upcoming_launches_next);
nextButton.setOnClickListener(this);
Button saveButton = (Button) findViewById(R.id.buttonSave);
}
public void insertToDatabase(String name, String date, int tbdDate, int tbdTime){
databaseAdapter = new LaunchesDatabaseAdapter(UpcomingLaunches.this);
//databaseAdapter.insertTestData();
Boolean inserted = databaseAdapter.insertLaunch(name, date, tbdDate, tbdTime);
}
错误记录
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.matthew.nethercott.spaceapplication, PID: 3152
java.lang.NullPointerException: Attempt to invoke virtual method 'long android.database.sqlite.SQLiteDatabase.insert(java.lang.String, java.lang.String, android.content.ContentValues)' on a null object reference
at com.matthew.nethercott.spaceapplication.Adapters.LaunchesDatabaseAdapter.insertLaunch(LaunchesDatabaseAdapter.java:56)
at com.matthew.nethercott.spaceapplication.UpcomingLaunches.insertToDatabase(UpcomingLaunches.java:60)
at com.matthew.nethercott.spaceapplication.Adapters.LaunchAdapter$1.onClick(LaunchAdapter.java:43)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
答案 0 :(得分:0)
您正在崩溃,因为database
是null
。而且,database
不是必需的。删除该字段,并根据需要将其替换为getReadableDatabase()
或getWritableDatabase()
。
答案 1 :(得分:-1)
数据库为空。您应该指定数据库。