0

Why cant I have a counter in a do until loop like this? I don't understand the error. Is there any correlation with the $table ?

I also tried renaming the variable $row, nothing changed.

            $row = [int]1
    do {    #Create a row
            $r = $Table.NewRow()
            #Enter data in the row
            $r.HostName   = $Sheet4.Cells.Item($row,2).Text
            $r.RecordType = $Sheet4.Cells.Item($row,3).Text
            $r.TimeStamp  = $Sheet4.Cells.Item($row,4).Text
            $r.TimeToLive = $Sheet4.Cells.Item($row,5).Text
            $r.RecordData = $Sheet4.Cells.Item($row,6).Text
            $r.Preference = $Sheet4.Cells.Item($row,7).Text  
            #Add the row to the table
            $Table.Rows.Add($r)
            $row++                
       } until (!$Sheet4.Cless.Item($row,2))  

Errorcode:

You cannot call a method on a null-valued expression.
At C:\Script\SCRIPT.ps1:392 char:17
+                 $row++
+                 ~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Table creation data:

    $TabName = "ExcelRecords"
    #Create Table object
    $Table = New-Object system.Data.DataTable “$ExcelRecords”
    #Define Columns
    $Col1 = New-Object system.Data.DataColumn ZoneName,([string])
    $Col2 = New-Object system.Data.DataColumn HostName,([string])
    $Col3 = New-Object system.Data.DataColumn RecordType,([string])
    $Col4 = New-Object system.Data.DataColumn TimeStamp,([string])
    $Col5 = New-Object system.Data.DataColumn TimeToLive,([string])
    $Col6 = New-Object system.Data.DataColumn RecordData,([string])
    $Col7 = New-Object system.Data.DataColumn Preference,([string])
    #Add the Columns
    $Table.columns.add($Col1)
    $Table.columns.add($Col2)
    $Table.columns.add($Col3)
    $Table.columns.add($Col4)
    $Table.columns.add($Col5)
    $Table.columns.add($Col6)
    $Table.columns.add($Col7)
0

1 Answer 1

1

I am guessing you have a typo in your script:

change the line

} until (!$Sheet4.Cless.Item($row,2)) 

to:

} until (!$Sheet4.Cells.Item($row,2)) 

This would explain the Null reference exception. Because I can't remember that a worksheet has a property of Cless

Sign up to request clarification or add additional context in comments.

5 Comments

Guess I'm the incorporation of stupidity! Thanks a lot! After hundreds of lines, I missed the tree for all the woods.
Before answering I was honestly trying to mistype Cells to be sure this is it, instead of just checking my subversion for old code to confirm.
Hab gerade gesehen, dass du ja Deutsch sprichst ;). Danke nochmal. Grüße!
Yes I do, but SO is an English site, so we should stick to our manners.
Ofc, I would never ask a question in german. I just thought it to be an amusing coincidence.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.