Jump to content

Subscript used on non-accessible variable


Recommended Posts

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]

Link to comment
Share on other sites

  • Moderators

Kronitron,

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

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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.

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...