Meine Anwendung ermöglicht es einem Benutzer, Details zu einem Ort hinzuzufügen, wenn ein Marker auf der Google-Karte hinzugefügt wird. Diese Details werden in Firebase mit allen Details gespeichert, die NICHT null sind. HierAndroid/Firebase: Warum gibt onDataChange() Nullwerte zurück?
ist der AKTUALISIERT Baum:
{
"Algiers" : {
"Description" : "Capital of Algeria",
"Latitude" : 36.77202162442747,
"Longitude" : 3.0560945346951485,
"Rating" : "9",
"Type" : "Town"
},
"Brussels" : {
"Description" : "Capital of Belgium",
"Latitude" : 50.850103621427685,
"Longitude" : 4.3520765751600266,
"Rating" : "8",
"Type" : "Town"
},
"Burger King" : {
"Description" : "Fast but not healthy food",
"Latitude" : 51.236331,
"Longitude" : -0.575385,
"Rating" : 5.29,
"Type" : "Restaurant"
},
"Chippy Pizza" : {
"Description" : "Best American pizza ever",
"Latitude" : 36.72722332469427,
"Longitude" : 3.0647138133645058,
"Rating" : "10",
"Type" : "Restaurant"
},
"European Parliament" : {
"Description" : "Some decisions about Europe are done there",
"Latitude" : 50.83868986886192,
"Longitude" : 4.375510700047016,
"Rating" : "8",
"Type" : "Museum"
},
"Friary Center McDonalds" : {
"Description" : "Good food",
"Latitude" : 51.23544690507358,
"Longitude" : -0.5764660611748695,
"Rating" : "7.4",
"Type" : "Restaurant"
},
"Guildford Town Center" : {
"Description" : "Town center of Guildford",
"Latitude" : 51.23622,
"Longitude" : -0.570409,
"Rating" : 8.74,
"Type" : "Town"
},
"Guildfords Castle" : {
"Description" : "Very old castle",
"Latitude" : 51.23428622281793,
"Longitude" : -0.5723629519343376,
"Rating" : "8",
"Type" : "Museum"
},
"Guildfords Hospital" : {
"Description" : "Treats all types of pathologies",
"Latitude" : 51.240506,
"Longitude" : -0.60838,
"Rating" : "9",
"Type" : "Hospital"
},
"Guildfords Museum" : {
"Description" : "Museum with several exhibitions",
"Latitude" : 51.233797,
"Longitude" : -0.573549,
"Rating" : "7.84",
"Type" : "Museum"
},
"Martyrs Monument" : {
"Description" : "Describes the History of Algeria",
"Latitude" : 36.745560268045196,
"Longitude" : 3.0698012933135033,
"Rating" : "10",
"Type" : "Museum"
},
"Military Hospital" : {
"Description" : "Excellent Hospital",
"Latitude" : 36.71973881144668,
"Longitude" : 3.063882999122143,
"Rating" : "10",
"Type" : "Hospital"
},
"Mount Alvernia Hospital" : {
"Description" : "Small and correct hospital",
"Latitude" : 51.23583085374619,
"Longitude" : -0.5647089332342148,
"Rating" : "7",
"Type" : "Hospital"
},
"Mustapha Bacha Hospital" : {
"Description" : "Complete Hospital",
"Latitude" : 36.76243892522928,
"Longitude" : 3.053263798356056,
"Rating" : "7",
"Type" : "Hospital"
},
"Nandos Guildford" : {
"Description" : "Tasty but expensive chicken",
"Latitude" : 51.236343270137674,
"Longitude" : -0.5743494629859924,
"Rating" : "9",
"Type" : "Restaurant"
},
"Pachéco Hospital" : {
"Description" : "Excellent Hospital",
"Latitude" : 50.854205318946484,
"Longitude" : 4.349818155169487,
"Rating" : "10",
"Type" : "Hospital"
},
"Paris" : {
"Description" : "Capital",
"Latitude" : "48.84191426189115",
"Longitude" : "2.3034103587269783",
"Rating" : "8",
"Type" : "Town"
},
"Starbucks Coffee" : {
"Description" : "All types of coffee are found there",
"Latitude" : 51.235304,
"Longitude" : -0.575049,
"Rating" : 7.84,
"Type" : "Restaurant"
},
"Test 2" : {
"Description" : "Test",
"Latitude" : "26.567103702363973",
"Longitude" : "2.967230938374996",
"Rating" : "8",
"Type" : "Town"
},
"Test Overflow" : {
"Description" : "Test",
"Latitude" : "31.29925316955398",
"Longitude" : "2.439718544483185",
"Rating" : "10",
"Type" : "Town"
},
"Test3" : {
"Description" : "Testf",
"Latitude" : "45.24608905762172",
"Longitude" : "3.4947332739830017",
"Rating" : "8",
"Type" : "Town"
}
}
ich die Firebase docs bin mit Daten aus der Datenbank abgerufen werden (unter Verwendung von onDataChange()
).
jedoch meine Anwendung abstürzt und druckt mir folgende Fehlermeldung:
05-16 14:37:15.772 13332-13332/org.com1032.flagged_v2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: org.com1032.flagged_v2, PID: 13332
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at org.com1032.flagged_v2.MainActivity$6.onDataChange(MainActivity.java:877)
at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:56)
at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5219)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Nach einigen Logs, wurde mir klar, dass alle Werte dieser spezifischen Schlüssel null sind. Hier
ist der Code für die Daten von Firebase Abrufen:
firebaseMarkers.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
String descPath = dataSnapshot1.getKey() + "/Description";
String latPath = dataSnapshot1.getKey() + "/Latitude";
String lonPath = dataSnapshot1.getKey() + "/Longitude";
String ratePath = dataSnapshot1.getKey() + "/Rating";
String typePath = dataSnapshot1.getKey() + "/Type";
Log.v("INSERTING?", "MAYBE");
Log.v("the KEY", dataSnapshot1.getKey());
Log.v("TYPE:", dataSnapshot.child(typePath) + "");
Log.v("NAME:", dataSnapshot1.getKey());
Log.v("LATITUDE:", dataSnapshot.child(latPath).getValue().toString());
Log.v("LONGITUDE:", dataSnapshot.child(lonPath).getValue().toString());
Log.v("RATING:", dataSnapshot.child(ratePath).getValue().toString());
Log.v("DESCRIPTION", dataSnapshot.child(descPath).getValue().toString());
markersDatabase.insertData(dataSnapshot.child(typePath).getValue().toString(), dataSnapshot1.getKey(),
Double.parseDouble(dataSnapshot.child(latPath).getValue().toString()), Double.parseDouble(dataSnapshot.child(lonPath).getValue().toString()),
dataSnapshot.child(ratePath).getValue().toString(), dataSnapshot.child(descPath).getValue().toString());
Log.v("INSERTED!", "YES");
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Log.v("Firebase Error", "Wow, you're asking very deep");
}
});
Jedes Mal, wenn ich ein Daten hinzufügen, alles auf einer lokale Datenbank im Telefon gespeichert wird (alle Duplikate werden entfernt nach). Hier
ist der Code für Daten in die Feuerbasis ergänzt:
fire.child(markerName.toString()).setValue(markerName.toString());
fire.child(markerName.toString()).child("Description").setValue(markerDescription.toString());
fire.child(markerName.toString()).child("Latitude").setValue(String.valueOf(point.latitude));
fire.child(markerName.toString()).child("Longitude").setValue(String.valueOf(point.longitude));
fire.child(markerName.toString()).child("Rating").setValue(markerRating.toString());
fire.child(markerName.toString()).child("Type").setValue(markerType.toString());
MarkerOptions marker = new MarkerOptions().position(new LatLng(point.latitude, point.longitude)).title(markerName);
map.addMarker(marker);
markersDatabase.insertData(markerType, markerName, point.latitude, point.longitude, markerRating, markerDescription);
Vielen Dank im Voraus.
Sie haben einen Link zu einem Bild des JSON-Baums in Ihrer Frage eingefügt. Bitte ersetzen Sie dies durch den tatsächlichen JSON als Text, den Sie einfach erhalten, indem Sie auf die Schaltfläche "Exportieren" in Ihrer Firebase-Datenbank klicken. Da der JSON als Text durchsuchbar ist, können wir ihn einfach verwenden, um mit Ihren tatsächlichen Daten zu testen und sie in unserer Antwort zu verwenden, und im Allgemeinen ist dies nur eine gute Sache. –
Vielen Dank für Ihren Rat, ich habe meine Frage aktualisiert. – Imad
Ihre Fehlermeldung besagt, dass Sie 'toString()' für eine 'null'-Referenz bei' MainActivity.java: 877' aufrufen. Setzen Sie dort einen Haltepunkt und prüfen Sie, welche genaue Referenz 'null' ist. –