Firestore getProductID cannot be null error

Viewed 5048

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'
6 Answers

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.

I have

buildscript {
    dependencies {
        classpath 'com.google.gms:google-services:4.2.0'
    }
}

and the solution is in the constructor of FirebaseOptions .setProjectId(prjID)

    String projectID = "project-id-com";
    FirebaseOptions options = new FirebaseOptions.Builder()
            .setApplicationId("1:26000034:android:6d45000000534")
            .setApiKey("AIzaSyAbsrv0000000000eml95vFOs")
            .setDatabaseUrl("https://" + projectID + ".firebaseio.com")
            .setProjectId(projectID)
            .build();

    FirebaseApp.initializeApp(context, options, projectID);
    FirebaseApp app = FirebaseApp.getInstance(projectID);
    FirebaseFirestore db = FirebaseFirestore.getInstance(app);

    Map<String, Object> city = new HashMap<>();
    city.put("name", "Los Angeles");
    city.put("state", "CA");
    city.put("country", "USA");

    db.collection("documents")
            .document("example")
            .set(city)
            .addOnSuccessListener(documentReference ->
                        Log.d(TAG, "DocumentSnapshot added with ID: " ))
            .addOnCompleteListener(ab ->
                        Log.d(TAG, "complete: "))
            .addOnFailureListener(e ->
                        Log.d(TAG, "Error adding document", e));

This is causing the problem.

FirebaseFirestore db = FirebaseFirestore.getInstance();

Instead, use it like this:

private DocumentReference documentReference = FirebaseFirestore.getInstance().document("sampleData/myData");

And the save the data like this;

    Map<String, Object> user = new HashMap<>();
    user.put("first", "Ada");
    user.put("last", "Lovelace");
    user.put("born", 1815);
    documentReference
            .set(user).addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            Log.d("MainActivity", "DocumentSnapshot added with ID: " + documentReference.getId());
        }
    });
Related