Hello I need to check if a variable which loads a photo is empty. It's define as Byte(). IDE throwing error when I do this - If Photo.Length = 0 then. I really need your help. Thanks.
Asked
Active
Viewed 4,359 times
-1
-
Post the code and the exception. – TEK May 01 '16 at 20:33
-
If Photo.Length = 0 Then - this is the statement Photo is empty. Error - Photo.Length threw an exception of type System.NullReferenceException. How can I avoid this when Photo is empty? – WhiteStar1 May 01 '16 at 21:04
-
Really? duplicate? I can't see how my question could be said to be a duplicate. It's VB.Net not C# and I am testing on a property of a variable that for me should had register 0 if there is nothing in it. Instead it throws a System.NullReferenceException. I know I am a noob but eh I am here to learn. I admire TEK's patience and support that really helped me out of a rut. – WhiteStar1 May 02 '16 at 22:14
-
1`NullReferenceExceptions` are quite common, and are frequently closed on SO. The reason why you received that exception is because you attempted to access an instance property (`Length`) when there wasn't an object (`Photo`) instance. That is, you had no reference to an object of type `Photo` in order to access its properties. – TEK May 03 '16 at 09:24
1 Answers
1
Perhaps you need to perform a null check on the array before you attempt to access any properties. You'll need to post more code if you want better help.
If Photo IsNot Nothing AndAlso Photo.Length > 0 Then
'Photo isn't empty
End If

TEK
- 1,265
- 1
- 15
- 30
-
Thanks very much TEK, it worked. I have a lot of code depending on the IF-THEN to filter out the Photos that are empty, a little re-arranging to fit your logic and it worked. This would never had occurred to me. I wonder why I couldn't test on the .Length property? It's there so why not? – WhiteStar1 May 01 '16 at 21:11