Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello! I have code:
VB
Public ImageList As ArrayList
    Dim strFileSize As String = ""
    Dim path As String = "c:\catalog_images\"
    Dim di As New IO.DirectoryInfo(Path)
    Dim aryFi As IO.FileInfo() = di.GetFiles("*.png")
    Dim fi As IO.FileInfo
    Dim counter As Integer = 1


And in constructor Sub New have:

VB
ImageList = Nothing
       For Each Me.fi In aryFi
           ImageList.Add(fi.FullName)
       Next

But calls error "Object reference not set to an instance of the object."
What is wrong?
Posted
Comments
[no name] 8-Mar-13 20:38pm    
You set ImageList = Nothing and then tried to use it.

1 solution

You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: wither make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

[EDIT]

After you comment I can see one you bug: you assign the reference ImageList to Nothing (which is Null, in VB.NET), and later attempt to dereference it by calling ImageList.Add. That's it.

(I hope it's just a typo; doing it intentionally would mean having no clue in programming in general.)

(which shows that you don't have a clue on what you are doing; so nothing can help you, unless you sit down to learn some basics of prog

Good luck,
—SA
 
Share this answer
 
v2
Comments
Jibesh 8-Mar-13 20:55pm    
OP Posted his Comment as solution so moved the comment here

At "aryFi" there are 35 element
But "for each" works only 1 eteraction. why?
Sergey Alexandrovich Kryukov 8-Mar-13 21:13pm    
In youр code sample exception is thrown. Yes, you start first iteration, and then go out of this position in stack at all due the exception thrown. Please see the updated answer, after [EDIT].
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900