Home > Back-end >  how to get a single data of a room database
how to get a single data of a room database

Time:09-16

I have multiple Acitivitys and in one of them I want to get a single column (poiLinks) of a single row (id -> Primary Key) of my room database. Pulling a whole row with another activity works, but with the new command i get a "null object reference" error and I cant figure out why. I want to pass an int to the function for the correct id-row. The Log says that the line in the Activity produces the error. I found similar questions here but i couldnt understand or implement the answers. Im new to Room so please be gentle :S.

(Entity -> DAO -> Repository -> ViewModel -> Activity)

My Entity:

@Entity(tableName = "route_table")
public class Route {

    @PrimaryKey (autoGenerate = true)
    int id = 0;

    @NonNull

    private String routeName;
    private double startLatitude, startLongitude, endLatitude, endLongitude;
    private String routeTime;
    private String poiLinks;

...

    public String getPoiLinks() {
        return this.poiLinks;
    }
}

My DAO:

@Dao
public interface RouteDao {

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    void insert(Route route);

...

    @Query("SELECT poiLinks FROM route_table WHERE id = :position")
    String getPoiLinks(int position);

}

My Repository:

public class RouteRepository {

    private RouteDao mRouteDao;
    private LiveData<List<Route>> mAllRoutes;

    RouteRepository(Application application){
        RouteRoomDatabase db = RouteRoomDatabase.getDatabase(application);
        mRouteDao = db.routeDao();
        mAllRoutes = mRouteDao.getRoutes();
    }

...

 String getPoiString(int position){
        String tempString = mRouteDao.getPoiLinks(position);
        return tempString;
    }

}

My ViewModel:

public class RouteViewModel extends AndroidViewModel {

    private RouteRepository mRepository;
    private final LiveData<List<Route>> mAllRoutes;

    public RouteViewModel(Application application){
        super(application);
        mRepository = new RouteRepository(application);
        mAllRoutes = mRepository.getAllRouts();
    }

...

public String getPoiStrings(int position){
        String tempString = mRepository.getPoiString(position);
        return tempString;
    }

}

My Activity:

public class POIActivity extends AppCompatActivity{

    ImageView iv_imageView1, iv_imageView2;
    int clickedPosition;
    String poiLinkString = "";

    RouteViewModel routeViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_poiactivity);

        clickedPosition = getIntent().getIntExtra("clickedPosition", 0);

        iv_imageView1 = findViewById(R.id.tv_imageView1);
        iv_imageView2 = findViewById(R.id.iv_imageView2);

        poiLinkString = routeViewModel.getPoiStrings(clickedPosition); //This gives the Error

    }
}

CodePudding user response:

Your routeViewModel is the one throwing the exception not the result. It is not initialized before calling .getPoiStrings(clickedPosition) inside onCreate

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        routeViewModel = new RouteViewModel(this.getApplication());

        ....
        poiLinkString = routeViewModel.getPoiStrings(clickedPosition); 

    }

This should fix it

  • Related