Sorting an array of null objects
BlitzMax Forums/BlitzMax Programming/Sorting an array of null objects
| ||
Not sure whether this should happen or not. If you do: Local a:Object[5] Rem 'uncomment this to solve it since they are no longer null For Local i:Int = 0 To 4 a[i] = New TImage Next EndRem a.sort() It causes an unhandled memory exception. I realise this is because it's trying to access the compare method of the null object, but would it not be better to simply stop the sort there rather than throw an exception? *EDIT* for instance, the first two elements could contain a valid object, but the array can't be sorted until all elements are valid. |
| ||
Or I have some spirit in my computer - but I copied your code, run it with debug mode on - but nothing happens (=no errors) [edit] I must stop drinking...:P - You are right! |
| ||
I think its because sort calls the compare method of all the objects, any that are null would raise an exception. |
| ||
I realise this is because it's trying to access the compare method of the null object, but would it not be better to simply stop the sort there rather than throw an exception? |
| ||
You could modify the implementation of sort to reflect this behavior. But if you know that there will be "null" objects, you should perhaps prefer lists ... this would speed up the sorting as well ... |
| ||
Isn't it possible to catch the exception? |
| ||
Isn't it possible to catch the exception? nope. enclosing it in a try-catch block doesn't help. |
| ||
This worksLocal a:Object[5] Rem 'uncomment this to solve it since they are no longer null For Local i:Int = 0 To 4 a[i] = New TImage Next EndRem Try a.sort() Catch except:TRuntimeException Print except.ToString() EndTry |