I am trying to use the new Firestore released by Firebase in my android app. Unfortunately I keep getting this error when trying to write to the database.
public class MainActivity extends AppCompatActivity { EditText txtName,txtSurname; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); txtName = findViewById(R.id.Name); txtSurname = findViewById(R.id.Surname); FloatingActionButton fab = findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { FirebaseFirestore db = FirebaseFirestore.getInstance(); Map<String, Object> user = new HashMap<>(); user.put("first", "Ada"); user.put("last", "Lovelace"); user.put("born", 1815); db.collection("users") .add(user) .addOnSuccessListener(new OnSuccessListener<DocumentReference>() { @Override public void onSuccess(DocumentReference documentReference) { //Log.d(TAG, "DocumentSnapshot added with ID: " + documentReference.getId()); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { //Log.w(TAG, "Error adding document", e); } }); } }); }
This is the error I keep getting. I have added the project to my android app via the assistant. So there shouldn't be an issue.
Process: damn.testapp, PID: 2622
java.lang.IllegalArgumentException: FirebaseOptions.getProjectId() cannot be null at com.google.firebase.firestore.FirebaseFirestore.zze(Unknown Source) at com.google.firebase.firestore.FirebaseFirestore.getInstance(Unknown Source) at damn.testapp.MainActivity$1.onClick(MainActivity.java:44) at android.view.View.performClick(View.java:5637) at android.view.View$PerformClick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Any help would be greatly appreciated.
Here is my security rules for the database
service cloud.firestore { match /databases/{database}/documents { match /{document=**} { allow read, write; } } }
And this is the dependency I am using:
compile 'com.google.firebase:firebase-firestore:11.4.2'
I had the same problem. Solved with this in gradle : 3.0.0 to 3.1.0
buildscript { dependencies { classpath 'com.google.gms:google-services:3.1.0' } }
This failure can happen if you have a google-services.json file but it doesn't have a project_id
key in the project_info
section.
This could happen if you're adding Firestore to an existing app with an older google-services.json file. Project ID is a relatively new field in this file, added to support Firestore.
Sign into the Firebase console, find your app in there, re-download the google-services.json file, replace it in your project, and you should be good to go.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With