简体   繁体   English

Powershell给我“您不能在空值表达式上调用方法。”但工作原理不一致。 到底是怎么回事?

[英]Powershell gives me “You cannot call a method on a null-valued expression.” but works - inconsistently. What is going on?

TL:DR I have three nearly identical PowerShell pieces of code with different results. TL:DR我有三个几乎相同的PowerShell代码段,但结果不同。

I am a PowerShell newbie using it to make changes to an XML file. 我是PowerShell的新手,使用它可以对XML文件进行更改。 In the file below, I am trying to change the two android:color fields and the android:src field. 在下面的文件中,我尝试更改两个android:color字段和android:src字段。 (BTW, the following is for illustration purposes only. It is the result of my attempts to understand what is happening and how to get it to work) (顺便说一句,以下内容仅用于说明目的。这是我尝试了解正在发生的事情以及如何使其工作的结果)

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<foo>
    <color android:color="#FFFFFF" />
</foo>
<item>
    <color android:color="#000000" />
</item>
<item>
    <bitmap android:src="@drawable/square150x150logo" android:tileMode="disabled" android:gravity="center" />
</item>
</layer-list>

The following is the snippet of code that I am using to make the desired changes: 以下是我用来进行所需更改的代码段:

Write-Host "Start test"

$xdoc."layer-list".foo.color.SetAttribute(
"color", 
"http://schemas.android.com/apk/res/android",
"FirstValue")
if(-not $?){
   Write-Host "one failed"
}

$xdoc."layer-list".item.color.SetAttribute(
"color", 
"http://schemas.android.com/apk/res/android",
"SecondValue")
if(-not $?){
    Write-Host "two failed"
}
$xdoc."layer-list".item.bitmap.SetAttribute(
"src", 
"http://schemas.android.com/apk/res/android",
"@drawable/Splash_150.png")
if(-not $?){
   Write-Host "three failed"
}

$xdoc.Save($SplashPath)
Write-Host "splashscreen.xml data updated"

This is the file after PowerShell has done its thing: 这是PowerShell完成其操作后的文件:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<foo>
    <color android:color="FirstValue" />
</foo>
<item>
    <color android:color="SecondValue" />
</item>
<item>
    <bitmap android:src="@drawable/square150x150logo" android:tileMode="disabled" android:gravity="center" />
</item>
</layer-list>

So, the first and second desired changes have been made. 因此,已经进行了第一和第二所需的更改。 But the output from PowerShell indicates that both the second and third changes failed because of "You cannot call a method on a null-valued expression." 但是PowerShell的输出表明第二个和第三个更改均失败,原因是“您无法在空值表达式上调用方法”。

Here is the output from PowerShell: 这是PowerShell的输出:

1>  Start test
1>  FirstValue
1>  You cannot call a method on a null-valued expression.
1>  At C:\Users\will\Documents\XF\myApp\SetConfig.ps1:105 char:6
1>  +      $xdoc."layer-list".item.color.SetAttribute(
1>  +      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1>      + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
1>      + FullyQualifiedErrorId : InvokeMethodOnNull
1>   
1>  two failed
1>  You cannot call a method on a null-valued expression.
1>  At C:\Users\will\Documents\XF\myApp\SetConfig.ps1:113 char:7
1>  +       $xdoc."layer-list".item.bitmap.SetAttribute(
1>  +       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1>      + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
1>      + FullyQualifiedErrorId : InvokeMethodOnNull
1>   
1>  three failed
1>  splashscreen.xml data updated
1>  Ending Powershell

So, why did the second one work but PowerShell gave an error message? 那么,为什么第二个程序可以正常工作,而PowerShell却给出了错误消息? How can I get the third one to work? 我怎样才能使第三个工作? Thanks! 谢谢!

As per the XML you have two "item" XML elements. 根据XML,您有两个“项目” XML元素。 So I think you might need to be reference individual element using the array notation instead. 因此,我认为您可能需要使用数组符号来引用单个元素。 See if the below code snippet works. 查看以下代码片段是否有效。

Write-Host "Start test"
$xdoc."layer-list".foo.color.SetAttribute(
"color", 
"http://schemas.android.com/apk/res/android",
"FirstValue")
if(-not $?){
   Write-Host "one failed"
}

$xdoc."layer-list".item[0].color.SetAttribute(
"color", 
"http://schemas.android.com/apk/res/android",
"SecondValue")
if(-not $?){
    Write-Host "two failed"
}
$xdoc."layer-list".item[1].bitmap.SetAttribute(
"src", 
"http://schemas.android.com/apk/res/android",
"@drawable/Splash_150.png")
if(-not $?){
   Write-Host "three failed"
}

$xdoc.Save($SplashPath)
Write-Host "splashscreen.xml data updated"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的书中说:“您不能在空值表达式上调用方法” - “You cannot call a method on a null-valued expression” in my scprit Xml:无法调用方法&#39;getElementsByTagName&#39;为null - Xml : Cannot call method 'getElementsByTagName' of null XML,JavaScript-未捕获的TypeError:无法调用方法&#39;getElementsByTagName&#39;为null - XML, Javascript - Uncaught TypeError: Cannot call method 'getElementsByTagName' of null 无法读取xml文档(无法调用null的方法&#39;getElementsByTagName&#39;) - Can't read xml document (Cannot call method 'getElementsByTagName ' of null) 反序列化XML给我null而不是对象 - Deserializing XML gives me null instead of the object Powershell -match“无法索引到空数组” - Powershell -match “Cannot index into a null array” 从xml字符串构建DOM文档给我一个空文档 - Building DOM document from xml string gives me a null document 将行连接成字符串的查询在Select语句中有效,但在表值函数中无效。 我想念什么? - Query to concatenate rows into string works in Select Statement, but not in Table Valued Function. What am I missing? GoogleScript / JavaScript; 解析XML; “ TypeError:无法调用null的方法“ getValue”。” - GoogleScript / JavaScript; parse XML; “TypeError: Cannot call method ”getValue“ of null.” InvalidRegex:模式值&#39;(?:Y | N)&#39;不是有效的正则表达式。 - InvalidRegex: Pattern value '(?:Y|N)' is not a valid regular expression.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM