-2

Whenever i try to run my code i get a null pointer exception if i try to dynamically insert data into the database.

However, if i run the method insertTestData() the data will be placed into the database.

I want to have a button save the listview item's data to the database.

I am setting the onClickListener in a custom adapter for my listview.

I am then passing the data back to the activities.java class and then to the database adapter but when click the button on the emulator, i get a NullPointerException error in the open() method on the database adapter.

I believe i am initializing the database. I am new to Android development and any help would be appreciated.

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);
}

Error Log

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)
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

You are crashing because database is null. Moreover, database is not necessary. Remove that field, and replace it with getReadableDatabase() or getWritableDatabase() as appropriate.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
-1

Database is null. You should specify the db.

Mwangi Njuguna
  • 75
  • 2
  • 10