[英]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.