The actual error I receive when running the program is:
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.database.DatabaseReference com.google.firebase.database.DatabaseReference.child(java.lang.String)' on a null object reference
This is my firebase database class:
import android.location.Location; import android.util.Log; import androidx.annotation.NonNull; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseError; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.database.ValueEventListener; import java.util.ArrayList; import java.util.List; public class FirebaseDatabaseHelper { public static FirebaseDatabase mDatabase ; public static DatabaseReference mReferenceCrossings; // public static DataSnapshot dataSnapshot; public FirebaseDatabaseHelper(FirebaseDatabase mDatabase) { mDatabase = FirebaseDatabase.getInstance(); mReferenceCrossings = mDatabase.getReference(); } public static ArrayList<Location> sortedQuery(String reference, String orderKey) { ArrayList<Location> results = new ArrayList<Location>(); DatabaseReference targetRef = mReferenceCrossings.child(reference); targetRef.orderByChild(orderKey).addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot datasnapshot) { for (DataSnapshot child : datasnapshot.getChildren()) { double latitude = Double.parseDouble(child.child("Latitude").getValue().toString()); double longitude = Double.parseDouble(child.child("Longitude").getValue().toString()); Location entry = new Location("coordinates"); entry.setLatitude(latitude); entry.setLongitude(longitude); results.add(entry); Log.d("coordinates", results.toString()); } } @Override public void onCancelled(@NonNull DatabaseError error) { Log.d("Firebase error", error.getDetails()); } }); return results; } }
This is my main class:
package com.example.locationpushing; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.fragment.app.FragmentActivity; import android.Manifest; import android.content.Intent; import android.content.IntentSender; import android.content.pm.PackageManager; import android.graphics.Color; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.os.Bundle; import android.os.Looper; import android.util.Log; import com.google.android.gms.common.api.ResolvableApiException; import com.google.android.gms.location.FusedLocationProviderClient; import com.google.android.gms.location.LocationCallback; import com.google.android.gms.location.LocationRequest; import com.google.android.gms.location.LocationResult; import com.google.android.gms.location.LocationServices; import com.google.android.gms.location.LocationSettingsRequest; import com.google.android.gms.location.LocationSettingsResponse; import com.google.android.gms.location.SettingsClient; import com.google.android.gms.maps.CameraUpdate; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.LocationSource; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.BitmapDescriptorFactory; import com.google.android.gms.maps.model.Circle; import com.google.android.gms.maps.model.CircleOptions; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.OnFailureListener; import com.google.android.gms.tasks.OnSuccessListener; import com.google.android.gms.tasks.Task; import androidx.annotation.NonNull; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; import androidx.fragment.app.FragmentActivity; import android.Manifest; import android.content.pm.PackageManager; import android.location.Address; import android.location.Geocoder; import android.location.Location; import android.os.Bundle; import android.util.Log; import com.google.android.gms.location.FusedLocationProviderClient; import com.google.android.gms.location.LocationServices; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; import com.google.android.gms.tasks.OnSuccessListener; import com.google.android.gms.tasks.Task; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.messaging.FirebaseMessaging; import com.google.firebase.messaging.RemoteMessage; import java.io.IOException; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { private static final String TAG = "MapsActivity"; private GoogleMap mMap; private Geocoder geocoder; private int ACCESS_LOCATION_REQUEST_CODE = 10001; FusedLocationProviderClient fusedLocationProviderClient; LocationRequest locationRequest; Marker userLocationMarker; Circle userLocationAccuracyCircle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); // Obtain the SupportMapFragment and get notified when the map is ready to be used. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); geocoder = new Geocoder(this); fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this); locationRequest = LocationRequest.create(); locationRequest.setInterval(330); locationRequest.setFastestInterval(330); locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); Intent intentBackgroundService = new Intent(this, FirebasePushNotification.class); startService(intentBackgroundService); FirebaseDatabaseHelper.sortedQuery("mReferenceCrossings","Latitude"); }
I’ve attempted different solutions but I still receive nullPointerException for the database. How could I properly initialize the database for access?
I would really appreciate the help on this. Thank you for any advice given.
Advertisement
Answer
I found problem in your code. Your error is occurred when call child method of mReferenceCrossings.
public class FirebaseDatabaseHelper { public static FirebaseDatabase mDatabase ; public static DatabaseReference mReferenceCrossings; .................. public static ArrayList<Location> sortedQuery(String reference, String orderKey) { ArrayList<Location> results = new ArrayList<Location>(); // Your error happend by this line. because mReferenceCrossings is null. DatabaseReference targetRef = mReferenceCrossings.child(reference); ..................... } }
Because mReferenceCrossings
is null
.
mReferenceCrossings
will be assigned in following method of your code.
public FirebaseDatabaseHelper(FirebaseDatabase mDatabase) { mDatabase = FirebaseDatabase.getInstance(); mReferenceCrossings = mDatabase.getReference(); }
But I don’t find any part this method calling in your main class.
You just called sort method without creating FirebaseDatabaseHelper
.
So your code have error.
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { ................................ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); ................................................. // This method called without init firebase instance FirebaseDatabaseHelper.sortedQuery("mReferenceCrossings","Latitude"); }
}
EDIT: update for solution.
Solution is several ways.
Here is one of those.
So you need to update FirebaseDatabaseHelper like this.
public class FirebaseDatabaseHelper { public static FirebaseDatabase mDatabase = FirebaseDatabase.getInstance(); public static DatabaseReference mReferenceCrossings = mDatabase.getReference(); ........... }