-
May 30th, 2017, 11:30 AM
#1
Thread Starter
Member
ChangeImage Function not working???
System.NullReferenceException: 'Object reference not set to an instance of an object.'
imgArray was Nothing.
This error comes up for my changeimage function for a game i am making. This is the code:
Public Function ChangeIMage(ByVal imgArray() As Image, ByVal imgIncrement As Decimal) As Image
'
imgCounter += imgIncrement
If CInt(imgCounter) > imgArray.GetUpperBound(0) Then
imgCounter = 0
End If
Return imgArray(CInt(imgCounter))
End Function
It was working up until now when i was working on the project. not sure what the error means and how fix it. if i could get an explanation with a solution that would be great. thanks very much in advance.
-
May 30th, 2017, 11:44 AM
#2
Re: ChangeImage Function not working???
Did it really say imgArray was Nothing?? I have yet to see that. It's something that people have been asking MS about for YEARS, so it would be pretty cool to see it finally implemented in the error message. That's progress. Prior to that, you had to find the object that was Nothing, even though the compiler clearly had the information.
However, that is the whole problem: imgArray is Nothing, and your code doesn't include the key piece. In that code, imgArray is supplied as an argument to the method. If it is Nothing inside the method, then Nothing is what was passed in. The problem is not with the code you posted, but with the code that calls the code that you posted.
My usual boring signature: Nothing
-
May 30th, 2017, 11:48 AM
#3
Thread Starter
Member
Re: ChangeImage Function not working???
wdy mean where it was called? likewehre it was declared or where it was used? i can show u either way im rele stuck w this. thanks for the reply btw
-
May 30th, 2017, 12:00 PM
#4
Re: ChangeImage Function not working???
When you call ChangeImage(), you are passing in some value for the imgArray parameter. Whatever you are passing in is Nothing, and that's the problem. So, where are you calling it, and what are you passing in? Also, how are you creating whatever you are passing in?
My reply to your other thread also works pretty well for this one. A breakpoint at the point where you call ChangeImage() would be pretty useful, as you'd be able to look at the value you are passing to the method. I described that in the other thread.
My usual boring signature: Nothing
-
May 30th, 2017, 12:39 PM
#5
Thread Starter
Member
Re: ChangeImage Function not working???
= ok i understand you, i was previously just saying piccharacter = character1standwest (the animation) so it makes sense that imgarray would equal nothing. I replaced this with picbox.image = changeimage(character1standwest, .1). i expected it to work, but the same error came up.
-
May 30th, 2017, 01:04 PM
#6
Re: ChangeImage Function not working???
So, how is character1standwest created, because...it is Nothing, so something is going wrong with that creation.
My usual boring signature: Nothing
-
May 30th, 2017, 01:12 PM
#7
Thread Starter
Member
Re: ChangeImage Function not working???
Character1StandWEST(0) = My.Resources.stand1_0
Character1StandWEST(1) = My.Resources.stand1_1
Character1StandWEST(2) = My.Resources.stand1_2
Character1StandWEST(3) = My.Resources.stand1_3
Character1StandWEST(4) = My.Resources.stand1_4
Character1StandWEST(0) = New Bitmap(My.Resources.stand1_0)
Character1StandWEST(1) = New Bitmap(My.Resources.stand1_1)
Character1StandWEST(2) = New Bitmap(My.Resources.stand1_2)
Character1StandWEST(3) = New Bitmap(My.Resources.stand1_3)
Character1StandWEST(4) = New Bitmap(My.Resources.stand1_4)
Character2StandWEST(0) = My.Resources.stand1_01
Character2StandWEST(1) = My.Resources.stand1_11
Character2StandWEST(2) = My.Resources.stand1_21
Character2StandWEST(3) = My.Resources.stand1_31
Character2StandWEST(4) = My.Resources.stand1_41
Character2StandWEST(0) = New Bitmap(My.Resources.stand1_01)
Character2StandWEST(1) = New Bitmap(My.Resources.stand1_11)
Character2StandWEST(2) = New Bitmap(My.Resources.stand1_21)
Character2StandWEST(3) = New Bitmap(My.Resources.stand1_31)
Character2StandWEST(4) = New Bitmap(My.Resources.stand1_41)
Character3StandWEST(0) = My.Resources.stand1_02
Character3StandWEST(1) = My.Resources.stand1_12
Character3StandWEST(2) = My.Resources.stand1_22
Character3StandWEST(3) = My.Resources.stand1_32
Character3StandWEST(4) = My.Resources.stand1_42
Character3StandWEST(0) = New Bitmap(My.Resources.stand1_02)
Character3StandWEST(1) = New Bitmap(My.Resources.stand1_12)
Character3StandWEST(2) = New Bitmap(My.Resources.stand1_22)
Character3StandWEST(3) = New Bitmap(My.Resources.stand1_32)
Character3StandWEST(4) = New Bitmap(My.Resources.stand1_42)
Character4StandWEST(0) = My.Resources.stand1_03
Character4StandWEST(1) = My.Resources.stand1_13
Character4StandWEST(2) = My.Resources.stand1_23
Character4StandWEST(3) = My.Resources.stand1_33
Character4StandWEST(4) = My.Resources.stand1_43
Character4StandWEST(0) = New Bitmap(My.Resources.stand1_03)
Character4StandWEST(1) = New Bitmap(My.Resources.stand1_13)
Character4StandWEST(2) = New Bitmap(My.Resources.stand1_23)
Character4StandWEST(3) = New Bitmap(My.Resources.stand1_33)
Character4StandWEST(4) = New Bitmap(My.Resources.stand1_43)
this is the code. i have this for standwest, standeast, walkeast, walkwest, and flyup, flydown. each for 4 characters. i know i havent done the rotate flip code yet, but that can come later since im just trying to figure out the nothing thing.
-
May 30th, 2017, 02:31 PM
#8
Re: ChangeImage Function not working???
Well, it all looks reasonable, so the next step is to put a breakpoint on this line:
If CInt(imgCounter) > imgArray.GetUpperBound(0) Then
When execution stops on the breakpoint, put the cursor on imgArray and press Shift+F9. Sometimes, just hovering over the variable is enough, but I find that when the object is Nothing, you often don't get a tooltip, and I wouldn't expect you to get one in this case. Shift+F9 will open a window that will show you the variable. You might get more than you want, such as imgArray.GetUpperBound(0), but if that's the case, you can always edit the textbox in the window that pops up to put in whatever you want. In this case, what you want is JUST imgArray. That should be Nothing, if the error message is correct. If that's the case, then put a breakpoint somewhere earlier. You can confirm that character1standwest is Nothing, but you'd already know that, so it's not worth testing for. In fact, the whole point of that test is to make sure the message that "imgArray Is Nothing" is actually correct.
By the way, I just noticed that you fill the array, but where is that code located? Wherever it is, put a breakpoint on some line like this one:
Character2StandWEST(0) = My.Resources.stand1_01
I would expect that this breakpoint will NOT be hit, which would mean that the whole block of code that is supposed to be filling the arrays is not actually getting called. If that's the case, you'd need to look even earlier.
By the way, what you are doing in that block is kind of bizarre. You fill the array with images from the resources....then you fill the array AGAIN, thereby wiping out whatever was put in there in those first lines. Therefore, you could cut the number of lines in half by removing one set, probably the New Bitmap set.
My usual boring signature: Nothing
-
May 30th, 2017, 04:12 PM
#9
Thread Starter
Member
Re: ChangeImage Function not working???
i did what you said with the breakpoint. i pressed shift + F9 and a window with the whole line came up. i edited and reevaluated it and it said that imgArray is not declared. does this mean the error is incorrect? it is declared in the functionas a ByVal.
-
May 30th, 2017, 04:21 PM
#10
Re: ChangeImage Function not working???
ByVal is correct.
That's a REALLY annoying thing that happens in VS from time to time. The easiest way for that to happen is if you accidentally removed either too many or too few characters. Highlighting imgArray and pressing Shift+F9 should avoid that (I assume you did what I suggested and just put the cursor in imgArray), but sometimes the compiler loses track of what is and is not in scope, which will also give you that message. This has only happened to me when debugging into dlls, though, never in the main app, so I really think that something was spelled wrong in whatever was left after you edited the line.
My usual boring signature: Nothing
-
May 30th, 2017, 04:26 PM
#11
Thread Starter
Member
Re: ChangeImage Function not working???
now something very weird happened... it says that "images" is nothing instead of "imgArray". i have no variable or anything named images. not sure where it came form or what it is referring to. i checked for spelling anyway and everything seems to be fine.
-
May 30th, 2017, 05:01 PM
#12
Re: ChangeImage Function not working???
Well, as I said in my first post, stating WHAT was Nothing was something that has been asked for for a very long time. It may be that it just isn't quite there, yet.
In that case, you had best start the investigation by ignoring everything other than the Null Reference Exception part. The standard response, which still works here, is this: When you get that exception, you should be taken to one particular line. On that line, one of the objects is Nothing. So the place to start is: Which line are you taken to when the exception occurs?
Also, what type is imgCounter? It really should be an Integer, and it sure seems like ImageIncrement should be type Integer, as well. Then you wouldn't need any of that CInt stuff. This has nothing to do with the problem, though, because neither of those variables could be causing this exception. In fact, the exception HAS to be caused by a reference type variable, which means an array, list, or any kind of class, and now that I look at that original method, the ONLY reference type variable in the whole method is imgArray (the ByVal thing is not related to this question, as it only has to do with how a variable is passed, a point that is often misunderstood).
My usual boring signature: Nothing
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|