You cannot call a method in null expression - general

You create a script, it runs for a while, and then suddenly crashes with “You cannot call a method in a null expression” or “Property” Property “cannot be found on this object. Make sure the property exists and can be set.” . What does it mean?

+2
source share
1 answer

This is a Powershell version with a null pointer exception. This exception occurs every time you try to request a variable that seems to be null. To determine which variable is null, and where you need to read the stack trace and line / character numbers of the corresponding line. Example:

You cannot call a method on a null-valued expression. At E:\temp\testsest.ps1:35 char:12 + If($Search.value() -contains $SearchString) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull 

Explain the error message. Firstly, there is a wording that is in the title of this question. If you are going to ask a question with this wording, you will get a set of similar questions suggested by StackOverflow. But there are more errors in the description. The second line shows the script, line number, and character of the first character of the expression that throws this exception. Here the query $Search.value() requested if it is -contains $SearchString . A wavy underline separates the expression completely, although only $Search.value() will underline the correct way. Next, there is CategoryInfo and FullyQualifiedErrorId , the latter says “Invoke method on null”, omitting “pointer” or “variable”.

Now debug the message. Here, the only method that needs to be called is value() , which means that $Search is null. Therefore, we need to go up from line 35 of the script and find the place where the last value is assigned to the variable in question. This particular script had a Range.Find() request, which returns null if there is no match for the search string. Exposure:

 $Excel = New-Object -ComObject Excel.Application $Excel.Visible = $true $ExcelWorkBook = $Excel.Workbooks.Open($ExcelPath) $ExcelWorkSheet = $Excel.WorkSheets.item("$location") $Range = $ExcelWorkSheet.Range("A1").EntireColumn $Search = $Range.find($user) # <<< here we get null If($Search.value() -contains $user) 

So we found where we get zero.

Remedies vary, but they all include checks against $null . In this case, it is enough to check $Search for null and return "Nothing found" if it is really null. It may not be so simple, there may be more structures that can be null, for example, in $abcsomeMethod() - here either $a , $ab , or $abc is null, so you need to check all the results. There are also situations where a complex structure is returned, and it is expected that it will have a value in a specific field, but the field is empty, so trying to use this field value will throw an exception.

Moral: if you get an exception that says "null", you did not expect something to return null, and you need to add checks for null (or, in fact, any unexpected) values ​​before trying to use the result.

+2
source

Source: https://habr.com/ru/post/1209345/


All Articles