Object reference not set to an instance of an object - when splatting to a custom function

New Contributor

I've written a module for a specific O365 function - onboarding users to mobile in InTune.


The module works, but I needed to add the ability for users to specify alternative credentials to run the module.

To achieve this, I'm using the splat method, which enables me to add credentials in the hash table 
Unfortunately, I'm getting error "Object reference not set to an instance of an object" with this method and cannot understand why.
The parameters appear to be passed through OK as I can print them to host, but the command doesn't like it.


Hopefully it's something obvious.

FYI, if I rem out @Params and use the $User variable, it will work.



$Params = @{
User = $User
if ($Null -ne $Credential) {$Params.add('Credential', $Credential)}
Try {
get-EXOEnabled @Params #Custom function
} Catch {...}




4 Replies
best response confirmed by iCer19 (New Contributor)



We can't really give you any definitive answers here as there's not enough information.


We'd need to know which line the error is referring to at a minimum, and if that error line is not in the example below, we'd need that area included.


All I can infer from the example is:


  1. It is not possible for Params to be null, since even if $User is null, a key will be added named "User" for which the corresponding object will be a null reference;
  2. It is possible for the "Credential" key to not be added to the hashtable but not possible for it to be null if it is added.


Still, without concise error information, that doesn't help much.


Prior to calling your custom Get-EXOEnabled function, you could simply dump the $Params to output for confirmation.


I'm not connected to Exchange at the moment but here's a "similar enough" example using a user from Azure AD instead. This shows I have two members in the hashtable, i.e. neither entry is null.




If you find the same then the issue is likely something within your function, not the example you've provided in here. But again, this is still an assumption as there's not enough code and only an imprecise error to work from.




Thanks for your time in looking at this. I was hoping I'd made a silly error which was obvious from the details I'd given.
I had printed the values being passed to the function (before and after), and they appeared correct.
RE the error, I was struggling to pull the full error back from the function, with my limited PS experience.

As it turns out, it all works fine from another machine. I've been chasing my tail over something that I can only assume is a local PS issue.
thanks again.



No problem.


If you get curious and want to surface the underlying error without having to change your existing code, include a "throw" statement in your "catch {}" block, like this:


    # Normal stuff ...
    # ... such as calling your cutsom function, etc.
    # Do whatever you need here before calling the throw statement to bubble the caught exception up one more level.
    # .. do stuff ...
    # .. do even more stuff ...
    # Finished doing stuff ...
    throw;  # This re-throws the exception that caused us to get in here in the first place, so you can see the precise exception details such as line numbers, etc.




Yeah, I'm using throw. (But not with a semicolon)
problem I found is that my ISE and VCS are crashing when it bombs out. but even before it crashes, I don't get the error detail, only what I'm capturing as the -errorvariable.