0

Created the auth function for firebase rule service and called the service instance to create the firebase ruleset but it is returning System.NullReferenceException: Object reference not set to an instance of an object.What did I miss here ?

using Google.Apis.FirebaseRules.v1;
using Ruleset = Google.Apis.FirebaseRules.v1.Data.Ruleset;
using Source = Google.Apis.FirebaseRules.v1.Data.Source;
using File = Google.Apis.FirebaseRules.v1.Data.File;

private static FirebaseRulesService _firebaseRulesService;


public static void IntializeFirebaseRules() {
    GoogleCredential credential = GoogleCredential.GetApplicationDefault();
    if (CloudManager.Credential.IsCreateScopedRequired)
    {
        credential = CloudManager.Credential.CreateScoped(FirebaseRulesService.Scope.CloudPlatform);
    }
    _firebaseRulesService = new FirebaseRulesService(
    new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = CloudManager.ApplicationName

        });
}
    

 public static async  void AddRuleset()
{
    Ruleset myRuleset = new Ruleset {
    new Source {
        Files = {
        new File {
        Name = "firestore.rules",
        Content = "service cloud.firestore {" +
        "match /databases/{database}/documents {" +
        "match /users/{userId}{" +
        "allow read: if (request.auth.uid == userId);" +
        "}" +
        "match /licenses/{accountId} {" +
        "allow read: if isAccountOwner(accountId);" +
        "}" +
        "}"+
        "}",
            }
            }
        };
    };


    _firebaseRulesService.Projects.Rulesets.Create(myRuleset , "projects/" + CloudManager.ProjectId);
}

**Edited -> separated the objects as below and the NullReferenceException occurrs on the line Source mySource = new Source

File myFile =  new File {
    Name = "firestore.rules",
    Content = "service cloud.firestore {" +
    "match /databases/{database}/documents {" +
    "match /users/{userId}{" +
    "allow read: if (request.auth.uid == userId);" +
    "}" +
    "function doc(subpath) {" +
    "return get(/databases/$(database)/documents/$(subpath)).data;" +
    "}" +
    "function isAccountOwner(accountId) {" +
    "return request.auth.uid == accountId" + 
    "|| doc(/users/$(request.auth.uid)).accountId == accountId;" +
    "}" +
    "match /licenses/{accountId} {" +
    "allow read: if isAccountOwner(accountId);" +
    "}" +
    "}"+
    "}",
};
Source mySource = new Source {
    Files = {
        myFile
    }  
};
Ruleset myRuleset = new Ruleset {
    Source = mySource
};

_firebaseRulesService.Projects.Rulesets.Create(myRuleset , "projects/" + CloudManager.ProjectId);
Samaritan
  • 55
  • 7
  • which line has issue? – Vivek Nuna Nov 16 '22 at 04:41
  • Ruleset myRuleset = new Ruleset { <- this line – Samaritan Nov 16 '22 at 04:44
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – jazb Nov 16 '22 at 04:51
  • Yeah jazb, I read the answers there, but still I don't get why I am getting NullReferenceException error, I initialized all the instances and gave it a value, I will look through again. – Samaritan Nov 16 '22 at 05:06
  • To isolate the issue, instead of a single statement, try separate steps: first a `new File {..}`, then a `new Source {..}` with that file, finally the `new Ruleset{..}` with that source. Don't you need a `Source=` there? – Hans Kesting Nov 16 '22 at 07:42
  • OT you have an `async void` method. That is almost never a good idea (single exception: event handlers). But in this case just remove the `async`, you are not `await`ing anything here – Hans Kesting Nov 16 '22 at 07:44
  • Hey Hans, I separated as you suggested, I created the new File {...} first then the new Source {...} second and finally I passed it to new Ruleset{...} and now the null exception occur on the new Source line and I also removed the async – Samaritan Nov 16 '22 at 10:58

1 Answers1

2

This statement:

Source mySource = new Source {
    Files = {
        myFile
    }  
};

... is trying to add myFile to the Files collection - but I suspect you'll find that's null by default. You can initialize it to a list instead:

Source mySource = new Source { Files = new List<File> { myFile } };
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194