Weird Undandled Exception in Select block

BlitzMax Forums/BlitzMax Programming/Weird Undandled Exception in Select block

christian223(Posted 2010) [#1]
I have a weird error happening in the begining of a Select block.

Check the code bellow, the compiler says the variable "Sprite" is Null, but the debugger says it is not, it actually has something there, the print command agrees, but when I try to compile it throws me this "RUNTIME ERROR:Attempt to access field or method of Null object" error at the where the Select is.

Why would this happen???, I'm clueless on how to fix this...

This is the code of where this happens:

For Local Sprite:Object = EachIn Collisiones
		
			If Not Sprite Then Print "NULL SPRITE" Else Print "SPRITE EXISTS!"
		        'in the console, this is what it prints: "SPRITE EXISTS!"
			
			Select (Sprite) 'Unhandled Exception:Attempt to access field or method of Null object



_JIM(Posted 2010) [#2]
I've encountered this once before. I remember I was using some pointers near that code and when I chose a different approach (without pointers) the error was gone. If you're using pointers anywhere, try commenting that and give it a go.


GfK(Posted 2010) [#3]
Does it work if your structure your code differently?
For Local Sprite:Object = EachIn Collisiones
	
	If Not Sprite
		Print "NULL SPRITE"
	Else
		Print "SPRITE EXISTS!"
		Select Sprite
		...whatever
	EndIf
Next



christian223(Posted 2010) [#4]
Thanks both. I am not using pointers near that, so I don't think that's it.

I changed the structure as suggested and the problem remains.

Any more ideas?.


GfK(Posted 2010) [#5]
There was some issue a while ago with putting null objects into lists. Basically you were allowed to do it, but you'd get all manner of problems when trying to access the list that contains it.

Have you accidentally put any null objects in there?

Failing that, what type of object are you putting into the list? Are the actually Objects? Or a custom type? If so then try changing the definition of Sprite (i.e. something other than Object).


jsp(Posted 2010) [#6]
I'm using the Select statement with my base type and then doing a case for any subtype and it works well.
The only difference I see is that you use the :Object and I wonder if that makes a problem. Could you use another type just for testing?
I assume there are a lot different types in your Collisiones...


christian223(Posted 2010) [#7]
Collisiones is an array, not a list, and in the debugger it says it contains 1 instance, so it is not empty.

I also changed Sprite to another type (custom type "T_objeto") and the problem remains exactly the same.

The other thing that might help mention is that Collisiones is an array given by CollideImage(), that's why I used Object for Sprite.

Thanks a lot for your suggestions so far.


Brucey(Posted 2010) [#8]
Collisiones is an array

Well, there you are then...

Entries in the array can be null... this would be a problem were you to iterate the array and assume they are not null.


_JIM(Posted 2010) [#9]
No, what he means is that on one line of code, the test returns a "not null" and on the following it is null. This is probably the work of the GC doing it's thing in the middle of the loops or something.


Brucey(Posted 2010) [#10]
If you have a reference to something, it won't mysteriously disappear.


Gabriel(Posted 2010) [#11]
This is probably the work of the GC doing it's thing in the middle of the loops or something.


If the garbage collector did that sort of thing, it would be hideously broken and not fit for purpose. It may have it's faults, but I've certainly not seen any evidence that it's either of those things.


christian223(Posted 2010) [#12]
It is not actually Null, that's the weird thing. The debugger says that both the variable Sprite and the array hold the same instance, but the compiler says that Sprite is Null, but there is nothing null in reality, the compiler goes blind or something.


Dreamora(Posted 2010) [#13]
the compiler won't go blind but perhaps you have some side effect thats working against you