Reputation: 3218
I am trying to insert data to a database using button click as:
In my MainActivity, I am using:
package com.example.trial;
public CityViewModel mcityViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
ImageButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String cityName = cityNameWeakReference.getText().toString();
City cityItem = new City(cityName, 5.0,4.0);
Snackbar.make(view, cityName+cityItem, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
mcityViewModel.insert(cityItem);
}
});
For easier look at the database related code, I have defined all database related files in com.example.trial.db
(except the DbActivity
which shows the recyclerview
).
My Entity
package com.example.trial.db
Entity(tableName = "city_table")
public class City {
@NonNull
@PrimaryKey
@ColumnInfo(name = "city")
private String city = "Atlantis";
@NonNull
@ColumnInfo(name = "latitude")
private Double latitude = 0.0;
@NonNull
@ColumnInfo(name = "Longitude")
private Double longitude = 0.0;
public City(@NonNull String city, @NonNull Double latitude, @NonNull Double longitude) {
this.city = city;
this.latitude = latitude;
this.longitude = longitude;
}
... getter and setter for city, latitude and longitude ...
... not showing for brevity. If you think that is necessary, please ask...
the Dao
package com.example.trial.db
@Dao
public interface CityDao{
@Insert(onConflict = OnConflictStrategy.IGNORE)
void insert(City city);
@Query("DELETE FROM city_table")
void deleteAll();
@Delete
void deleteCity(City city);
@Query("SELECT * from city_table LIMIT 1")
City[] getAnyCity();
@Query("SELECT * from city_table ORDER BY city ASC")
LiveData<List<City>> getAllCity();
}
and the ViewModel is
package com.example.trial.db
public class CityViewModel extends AndroidViewModel {
private CityRepository mRepository;
private LiveData<List<City>> mAllCity;
public CityViewModel(Application application) {
super(application);
mRepository = new CityRepository(application);
mAllCity = mRepository.getAllCity();
}
public LiveData<List<City>> getAllCity() {
return mAllCity;
}
public void insert(City city) { mRepository.insert(city); }
public void deleteCity(City city) { mRepository.deleteCity(city); }
public void deleteAll() {
mRepository.deleteAll();
}
}
Now, when I run the code, I am getting an error
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.trial.db.CityViewModel.insert(com.example.trial.db.City)' on a null object reference
The snackbar
is giving some value, which shows, cityItem
is not null either, but still I am getting this error.
I am very new to java and trying to build this from this codelabs
. But I am unable to find out what I am doing wrong here.
Kindly help.
Upvotes: 0
Views: 390
Reputation: 6422
It looks like you are not initialising mcityViewModel
in onCreate(Bundle savedInstanceState)
Please add this line in onCreate
method:
mcityViewModel = new ViewModelProvider(this).get(CityViewModel.class);
@Override
protected void onCreate(Bundle savedInstanceState) {
// -------> Add this line here <-------
mcityViewModel = new ViewModelProvider(this).get(CityViewModel.class);
ImageButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String cityName = cityNameWeakReference.getText().toString();
City cityItem = new City(cityName, 5.0,4.0);
Snackbar.make(view, cityName+cityItem, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
mcityViewModel.insert(cityItem);
}
});
}
Upvotes: 1