Kronitron

Subscript used on non-accessible variable

4 posts in this topic

#1 ·  Posted

I've been running into issues with arrays. This error seems to be popping up at random. Yesterday I ran a script once and didn't get it. Then I changed some string variable values and got it. I undid these actions and got the error with the version of the script that worked originally. I went to bed baffled, computer off, woke up and tried the script again and it worked. Powering off in and of itself doesn't fix the issue. Here it goes again, in yet another script:

"C:\...\Test_4.au3" (17) : ==> Subscript used on non-accessible variable.:
$aPixelList[$iCounter] = $aPixFound[0]
$aPixelList^ ERROR

#include <array.au3>

Global $aPixelList
Global $aPixFound
Global $aCurPix
$iXcoordinate = 0
$iYcoordinate = 0
$iCounter = 0

Sleep(4000)

Do
    ;search for pixels matching color 5093208
    $aPixFound = PixelSearch($iXcoordinate, $iYcoordinate, 1920,1080, 5093208)

    ;log any pixels found in an array
    $aPixelList[$iCounter] = $aPixFound[0]   ;error found here
    $aPixelList[$iCounter + 1] = $aPixFound[1]

    ;set new starting coordinates for search at the last pixel found was plus one
    If $aPixFound[0] = 1920 Then
        $iXcoordinate = 0
        $iYcoordinate = $aPixFound[1] + 1
    Else
        $iXcoordinate = $aPixFound[0] + 1
    EndIf

    ;the counter determines which array elements to log pix coordinates to
    $iCounter += 2

Until @error = 1 ;no more pixels found

_ArrayDisplay($aPixelList) ;show me some pixels coordinates!

I'm using SciTE Version 3.6.6 and Windows 7 x86. 


[post='1192322']Detect and Switch Monitor Orientation[/post]

Share this post


Link to post
Share on other sites



#2 ·  Posted

Kronitron,

Add some error-checking to your code to confirm you have an array before trying to access it.

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#3 ·  Posted

Thank you very much, @Melba23. I tend to be far more thorough in my error-checking but I was errantly confident that nothing could be wrong with the code itself. Logic failed me. I managed to find two parts that needed editing. Declaring and initializing the array with the decimal color code solved one problem and really does great things for the larger script this is all part of. Also, checking that the pixel search found something before proceeding prevented errors as the loop completes its final circuit.

Here's what it looks like now:

#include <array.au3>

$iDecimalColorCode = 5093208
$iXcoordinate = 0
$iYcoordinate = 0
$iCounter = 0

Global $aPixelList[1] = [$iDecimalColorCode]
Global $aPixFound
Global $aCurPix

Sleep(4000)

Do
    $aPixFound = PixelSearch($iXcoordinate, $iYcoordinate, 1920,1080, $iDecimalColorCode)

    If $aPixFound <> "" Then

        _ArrayAdd($aPixelList, $aPixFound[0])
        _ArrayAdd($aPixelList, $aPixFound[1])

        If $aPixFound[0] = 1920 Then
            $iXcoordinate = 0
            $iYcoordinate = $aPixFound[1] + 1
        Else
            $iXcoordinate = $aPixFound[0] + 1
        EndIf

        $iCounter += 2

    EndIf

Until @error = 1

_ArrayDisplay($aPixelList)

 


[post='1192322']Detect and Switch Monitor Orientation[/post]

Share this post


Link to post
Share on other sites

#4 ·  Posted

Instead of
If $aPixFound <> "" Then ...
It should be
If IsArray($aPixFound) Then ...
-Or-
If Not @error Then ..

From the helpfile
PixelSearch
Return Value:
Success: a two-element array of pixel's coordinates. (Array[0] = x, Array[1] = y).
Failure: sets the @error flag to 1 if the color is not found.

Make yourself sheep and the wolves will eat you - Benjamin Franklin

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now