Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Cannot index into null array

Cannot index into null array

Cannot index into null array

Hi, I've just joined here because it looks like a good place to troubleshoot PowerShell (which I've just started to learn) and I'll be doing some Android programming at some point so I'm sure I'll be needing all you guys help.

Ok...My problem is that the code below brings about the dreaded cannot index into null array error when using the $ListOfUsers variable.

Does anyone have any ideas how I resolve this or rewrite the code to work better?


NOTE: All variable instances not initialised have been done so in the code elsewhere


# Store the list of users due to expire
$Count = -1
# If the user's account is due or almost due to expire then capture details of account into an array
if (($DaysLeft -eq $NumOfDays) -or ($DaysLeft -le $NumOfDays) -and ($DaysLeft -ge 0) -or ($DaysLeft -le 0)) {
  $ListOfUsers[$Count++] = $Users.Name
  $ListOfUsers[$Count++] = $Users.Email
  $ListOfUsers[$Count++] = $ExpiredDate
  $ListOfUsers[$Count++] = $DaysLeft

RE: Cannot index into null array

What is the standalone line
supposed to accomplish?

Your If statement is also too complex. Based on this code it should just read:
If ($DaysLeft -le $NumOfDays)

-le stands for "Less than or Equal", so that covers your first clause testing just for equal. Your last two clauses comparing $DaysLeft to 0 mean $DaysLeft can be any number including 0. Unless there was something else you were trying to test for those last two clauses aren't needed.

RE: Cannot index into null array

$ListOfUsers[0] was my thinking of how you initialise an array

I've since learned elsewhere that this is accomplished with
$ListOfUsers = @( )

But now when I reference the array, I get the error:
Array assignment failed because index '0' was out of range.

Basically, how do I create an array that I can then store information in like the following

$ListOfUsers[0] = "Peter"
$ListOfUsers[1] = "Michael"
$ListOfUsers[2] = "John"


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close