I have come across this when using as3hx to port my AS3 code to Haxe:
delete classMemberDictionary[key]
as3hx can't translate this to Haxe, you have to do it manually. I've read the
as3hx README and it says
if it is a local variable, replace delete varname with varname = null
if it is a class member variable, remove the delete entirely
Since it is a dictionary item it doesn't seem right to just remove the line, as the README says. Should I set it to null?
Are you using Map or openfl.utils.Dictionary? In either case you can use remove():
Map.remove()
Dictionary.remove()
Setting the value to null isn't quite the same. Although get() would return null in either case, exists() still returns true for entries that weren't explicitly removed.
I am calling a user-defined function and passing an argument that is in dot notation. For example,
of_testingnullarg( tab_2.tabpage_comp_info.dw_webcomp_info.object.dormant[i] )
The above function is purely to demonstrate this problem. It accepts a long parameter and uses isNull() and messagebox() to tell the user if the parameter is null or not. Here is its listing:
// of_testingnullarg( al_arg1 )
// strictly an experiment
if isNull(al_arg1) then
messageBox("al_arg1", "is null inside of_testingNullArg")
else
messageBox("al_arg1", "is NOT null inside of_testingNullArg, it is "+string(al_arg1))
end if
When the dormant[i] (referring to the first function call) control contains a number, there is no problem--the parameter inside of_testingnullarg will contain the same number, as expected. But when the dormant[i] control has read null from the database, I find that the parameter inside of_testingnullarg does NOT hold null. It holds a number such as 16. Null was passed in (according to isNull()), but the parameter inside of_testingnullarg holds a number. It is not a random number, it is consistent, but seems to be dependent on how many parameters the function accepts.
This problem does NOT reveal itself when I run the program inside the Powerbuilder IDE. (I am running Powerbuilder 12.5) It shows up only when I build the program and run the executable.
I have found that if I first do this, the problem goes away:
long ll_dormant
ll_dormant = tab_2.tabpage_comp_info.dw_webcomp_info.object.dormant[i]
of_testingnullarg( ll_dormant )
To summarize, the problem shows up when 3 things are true:
I am NOT running the program through the Powerbuilder IDE
My argument is in dot-notation (not inside a variable)
The value in the control is null
I think this must have something to do with a pointer on the call stack. It must be off 2 bytes or something. Is it common knowledge to always put the value into a variable first?
I have found out that dot-notation returns type 'ANY'. This is a problem when the value is NULL which is typeless. PowerBuilder is trying to do an implicit cast of a NULL Any value to a NULL Long and gets confused. The solution is to help PowerBuilder with the type BEFORE the function call (which is why putting it into a variable first worked). These also work:
of_testingnullarg( LONG(tab_2.tabpage_comp_info.dw_webcomp_info.object.dormant[i]) )
or
of_testingnullarg( tab_2.tabpage_comp_info.dw_webcomp_info.GetItemNumber(i, "dormant") )
I'm writing an exception handler and I'd like to make use of the FSR ( fault status registers)
I'm using Cortex-A9 , L1\L2 and MMU on , OS is nucleus.
I'm generating different exceptions but it seems that DFSR\ISFR do not change their value after the exception occurred. is there a need to enable this functionality or FSR should be captured by default ?
Ran
I'm getting grails "BadlyFormattedFlowExecutionKeyException" exception when I change the execution param value in address bar!! Does anyone know how to handle such Exceptions?
Cheers!
I had the same problem. After a lot of googling, i made a filter for each flow.
And In 'before' closure i read params.execution, if params.execution is not null i test the state with getFlowStateName function:
Accessing flow state name from filter
if is a invalid state (the function return null) i redirect to the begin of flow (redirect controller: 'xx', action: 'yy').
Best Regards!
I have an odd question that I have always thought about, but could never see a practical use for. I'm looking to see if there would be enough justification for this.
When is handling a null pointer/reference exception preferred over doing a null check? If at all.
This applies to any language that has to deal with null pointers/references which has exception handling features.
My usual response to this would be to perform a null check before doing anything with the pointer/reference. If non-null, continue like normal and use it. If null, handle the error or raise it.
i.e., (in C#)
string str = null;
if (str == null)
{
// error!
}
else
{
// do stuff
int length = str.Length;
// ...
}
However if we were not to do the check and just blindly use it, an exception would be raised.
string str = null;
int length = str.Length; // oops, NullReferenceException
// ...
Being an exception, we could certainly catch it so nothing is stopping us from doing this (or is there?):
string str = null;
try
{
int length = str.Length; // oops, NullReferenceException
// ...
}
catch (NullReferenceException ex)
{
// but that's ok, we can handle it now
}
Now I admit, it's not the cleanest code, but it's no less working code and I wouldn't do this normally. But is there a design pattern or something where doing this is useful? Perhaps more useful than doing the straight up, null check beforehand.
The only cases where I can imagine this might be useful is in a multi-threaded environment where an unprotected shared variable gets set to null too soon. But how often does that happen? Good code that protects the variables wouldn't have that problem. Or possibly if one was writing a debugger and wanted the exception to be thrown explicitly only to wrap it or whatnot. Maybe an unseen performance benefit or removes the need for other things in the code?
I might have answered some of my questions there but is there any other use to doing this? I'm not looking for, "do this just because we can" kinds of examples or just poorly written code, but practical uses for it. Though I'll be ok with, "there's no practical use for it, do the check."
The problem is that all null pointer exceptions look alike. Any accounting that could be added to indicate which name tripped the exception can't be any more efficient than just checking for null in the first place.
If you expect the value to not be null, then there is no point in doing any checks. But when accepting arguments, for example, it makes sense to test the arguments that you require to be non-null and throw ArgumentNullExceptions as appropriate.
This is preferred for two reasons:
Typically you would use the one-string form of the ArgumentNullException constructor, passing in the argument name. This gives very useful information during debugging, as now the person coding knows which argument was null. If your source is not available to them (or if the exception trace was submitted by an end user with no debug symbols installed) it may otherwise be impossible to tell what actually happened. With other exception types you could also specify which variable was null, and this can be very helpful information.
Catching a null dereference is one of the most expensive operations that the CLR can perform, and this could have a severe performance impact if your code is throwing a lot of NullReferenceExceptions. Testing for nullity and doing something else about it (even throwing an exception!) is a cheaper operation. (A similar principle is that if you declare a catch block with the explicit intent of catching NREs, you are doing something wrong somewhere else and you should fix it there instead of using try/catch. Catching NREs should never be an integral part of any algorithm.)
That being said, don't litter your code with null tests in places you don't ever expect null values. But if there is a chance a reference variable might be null (for example if the reference is supplied by external code) then you should always test.
I've been programming Java for more than 10 years, and I can't remember a single time I've explicitly tried to catch a null pointer exception.
Either the variable is expected to be null sometimes (for example an optional attribute of an object), in which case I need to test for null (obj.attr != null), or I expect the variable to be not null at all times (in which case a null indicates a bug elsewhere in the code). If I'm writing a method (say isUpperCase) whose argument (String s) is null, I explicitly test for null and then throw an IllegalArgumentException.
What I never ever do, is silently recover from a null. If a method should return the number of upper case characters in a string and the passed argument was null, I would never "hide" it by silently return 0 and thus masking a potential bug.
Personnally (doing C++), unless there is a STRONG contract that assures me that my pointers are not nullptr, I always test them, and return error, or silently return if this is allowed.
In Objective-C you can safely do anything with nil you can do with a non-nil and the nil ends up being a noop. This turns out to be surprisingly convenient. A lot less boilerplate null checking, and exceptions are reserved for truly exceptional circumstances. Or would that be considered sloppy coding?