Why could this crash?

Started by wombats, May 29, 2018, 20:37:58

Previous topic - Next topic

wombats

Hi,

Sometimes I'll get the "Unhandled Exception:Attempt to access field or method of Null object" error on the test.Move() line in the following. I can't figure it out. The method being called is there and surely if the object is in the list, it should work?

For Local test:MyType = EachIn MyList
      If test.active = 1
        test.Move()
    EndIf
Next


Am I just missing something about programming in BlitzMax?

Thanks.

Derron

Just run it in debug mode and let it show what is really crashing


the "For local x:xtype = Eachin list" is already only iterating over valid xtype-objects. Eg. the list could contain TImage and TSound - doing a "For local img:TImage = eachin list" only returns valid TImage instances.

This means "test.Move()" will not fail because "test" is null but because something within "Move()" is tried to access while being null.


OK, there might be some chance - not sure if that is possible at all but: do you remove things from "MyList" during these test.Move() calls. Eg could "Move()" result in "test" being removed from MyList (eg. reaching a black-hole-tile or so)? In that case you do a "concurrent list modification" which should be avoided as you might miss an entry (eg. the iterator gets puzzled and skips an entry). It should not result in "test" being null, but it might be something in the likes of what I just described.


bye
Ron

wombats

I'm not removing anything during Move(). The debug panel does show a local variable within the method as Null, but I can't see why it would be Null as the only time that variable is used is within similar EachIn loops.

Derron

"within": are you using "strict" or "superstrict" ?

for local bla = eachIn blubb
local x:obj = new obj
next

for local bla2 = eachin blubb2
  x.DoSomething()
next


wont work, as "local" is within the scope of the first for loop and gets out of this scope right after the "next".


bye
Ron