I am trying to get Realtime data from firestore and update a class level object in my android app but when execute the code the app crashes and when I examined it displays the following error:
2021-06-08 16:47:37.569 13632-13632/com.example.railtracker E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.railtracker, PID: 13632
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
at com.example.railtracker.Acitivity.TrainLiveTrackViewActivity$1.onEvent(TrainLiveTrackViewActivity.java:80)
at com.example.railtracker.Acitivity.TrainLiveTrackViewActivity$1.onEvent(TrainLiveTrackViewActivity.java:69)
at com.google.firebase.firestore.DocumentReference.lambda$addSnapshotListenerInternal$2$DocumentReference(DocumentReference.java:504)
at com.google.firebase.firestore.-$$Lambda$DocumentReference$GF131yLOtm0PQYErAZvV1mYKmvw.onEvent(Unknown Source:6)
at com.google.firebase.firestore.core.AsyncEventListener.lambda$onEvent$0$AsyncEventListener(AsyncEventListener.java:42)
at com.google.firebase.firestore.core.-$$Lambda$AsyncEventListener$DNkggu2LY54oguDvcp-QtRg6Sfg.run(Unknown Source:6)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
I checked printing the data to the terminal and it displays correctly but when I try to assign it to a variable the app crashes. Please help me out with this issue. Thanks in advance.
My code:
package com.example.railtracker.Acitivity;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.IntentService;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import com.example.railtracker.Adapter.StopViewAdapter;
import com.example.railtracker.DTO.TrainDTO;
import com.example.railtracker.DTO.TrainInfoDTO;
import com.example.railtracker.R;
import com.example.railtracker.Util.FetchLocationIntentService;
import com.github.vipulasri.timelineview.TimelineView;
import com.google.android.gms.common.logging.Logger;
import com.google.android.material.snackbar.BaseTransientBottomBar;
import com.google.android.material.snackbar.Snackbar;
import com.google.api.Logging;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
public class TrainLiveTrackViewActivity extends AppCompatActivity {
private static final String TAG= TrainLiveTrackViewActivity.class.getSimpleName();
private RecyclerView stopsView;
private Toolbar toolbar;
private TrainDTO train;
private LocationReceiver locationReceiver;
private Location lastLocation;
private String zipCode;
private TrainInfoDTO trainInfoDTO=new TrainInfoDTO();
private final String RESULT_DATA="RESULT_DATA";
private final String COLUMN="trains";
double sample;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_train_live_track_view);
setUpView();
locationReceiver=new LocationReceiver(new Handler());
loadRealTimeData();
}
private void loadRealTimeData()
{
try{
DocumentReference db= FirebaseFirestore.getInstance().collection(COLUMN).document( String.valueOf(train.getTrainId()));
db.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot value, @Nullable FirebaseFirestoreException error) {
if(error != null){
Log.e(TAG,error.getMessage());
error.printStackTrace();
}
if(value != null && value.exists()){
trainInfoDTO.setTrainId((int) value.get(getResources().getString(R.string.train_id)));
trainInfoDTO.setLat((double)value.get(getResources().getString(R.string.latitude)));
trainInfoDTO.setLon((double)value.get(getResources().getString(R.string.longitude)));
trainInfoDTO.setPassengerCount((int)value.get(getResources().getString(R.string.passenger_count)));
trainInfoDTO.setSeatingCapacity(train.getSeatingCapacity());
System.out.println(value.getData());
}
else{
showSnackbar(getResources().getString(R.string.no_train_data));
}
}
});
}catch (Exception e){
Log.e(TAG,e.getMessage());
e.printStackTrace();
}
}
private void showSnackbar(String text){
View container=findViewById(android.R.id.content);
if(container != null){
Snackbar.make(container,text,Snackbar.LENGTH_LONG).show();
}
}
private void setUpView() {
toolbar=findViewById(R.id.ToolBarMain);
stopsView=findViewById(R.id.stops_rv);
Intent intent=getIntent();
train=(TrainDTO)intent.getSerializableExtra("train");
StopViewAdapter adapter=new StopViewAdapter(train.getCurrentShift().getStops(),this);
stopsView.setAdapter(adapter);
stopsView.setLayoutManager(new LinearLayoutManager(this));
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(train.getName());
}
private void startIntentService()
{
Intent intent= new Intent(this, FetchLocationIntentService.class);
intent.putExtra("LocationReceiver",locationReceiver);
intent.putExtra("LastLocation",lastLocation);
startService(intent);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
private class LocationReceiver extends ResultReceiver {
/**
* Create a new ResultReceive to receive results. Your
* {@link #onReceiveResult} method will be called from the thread running
* <var>handler</var> if given, or from an arbitrary thread if null.
*
* @param handler
*/
public LocationReceiver(Handler handler) {
super(handler);
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
zipCode= resultData.getString(RESULT_DATA);
}
}
}
I was able to figure out the problem in the above scenario. It was the document which I accessed in the firestore had a inner Map and within it was its attributes so the null pointer exception was thrown because I directly tried to access the attributes from the document. Work around I did:
Map<String,Object> map=new HashMap<>();
map=(Map)value.get(String.valueOf(train.getTrainId()));
trainInfoDTO.setLat((double) map.get(getResources().getString(R.string.latitude)));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.