On iPhone, I frequently have null pointer exceptions in SwipeableContainer.close() due to getComponentForm returning null (see stacktrace below). I assume this means the SwipeListener has not been removed correctly which seems to happen in SwipeableContainer.deinitialize(). However, I cannot figure out from where deinitialize() is supposed to be called so I don't understand what I need to do to avoid this situation.
Any ideas?
[EDT] 12:19:43,5 - Exception: java.lang.NullPointerException - null
java.lang.NullPointerException
at com_codename1_ui_SwipeableContainer.close:199
at com_codename1_ui_SwipeableContainer_SwipeListener.actionPerformed:437
at com_codename1_ui_util_EventDispatcher.fireActionSync:459
at com_codename1_ui_util_EventDispatcher.fireActionEvent:362
at com_codename1_ui_Form.fireReleaseListeners:3770
at com_codename1_ui_Form.pointerReleased:3847
at com_codename1_ui_Component.pointerReleased:4710
at com_codename1_ui_Display.handleEvent:2359
deinitialize should be invoked on removal of the component or the form. But this can fail in this case if we have an event chain with multiple operation where a user action can trigger both the close and the removal.
The workaround is to wrap the second operation in a callSerially to postpone it to the next EDT cycle but for now I just added a fix to the null pointer exception in close() so the method won't fail if there's no parent Form.
Related
I'm trying to move across a grid, navigating obstacles. This requires me performing an action 'move' (certain number of steps, X,Y). If I bump into an obstacle on the way, I call an internal action that allows me to go around it, then move the remainder of my way.
This will give a
'No failure event was generated for..'
in the plan which I move in (when it hits the obstacle), as if there's an obstacle on the way the whole move won't be completed in the agent's mind, even though I am navigating the same distance eventually with help from my internal action (I think this is the reason). It continues on even with the error.
I was wondering if there's a way to deal with this warning/error, as I'm reaching the correct destination anyway. I hope this makes sense.
Regarding the error message, if it due to the lack of applicable plans, we can simply add a new plan without context. For example:
// your plans:
move(...) : .... <- ...
move(...) : .... <- ...
move(...) : .... <- ...
// new plan:
move(...). // always applicable, does nothing.
What's the point of throwing exceptions?
For example I stumbled across this:
static List<Integer> list(int [] a) {
if (a == null)
throw new NullPointerException();
//...
But when you don't throw the nullpointer, you'll also get a nullpointer?
I see this regularly and I wanted to know if this is a good habit to learn?
It's better to fail fast. For example the function could do a bunch of stuff before it even references the variable "a" in your example resulting in a lot of unnecessary processing.. It would be best just to fail immediately if you know "a" is null from the very beginning. You could also append a custom error message to the exception as well.
The idea behind the THROW is to prevent the error from stopping your program.
If it's a fatal enough error, your program will stop anyway. But if the program can continue it will, and just let you know that an error occurred.
In many cases, you assign a function to report the error, since you threw it up and know what it is.
I always found throwing exceptions to be a matter of design and readability. For example, generally when I design something I prefer to handle errors where they occur. However an equally valid design would be to throw the exception and handle it somewhere else. I have seen some abstractions where generally your flow is something similar to this...
FlowControl -> GenericMethod(catches exceptions and calls methods only) -> PrivateMethods (generally used to do the work, throws exceptions).
You might find a more complete answer here as well: When to catch the Exception vs When to throw the Exceptions?
It's possible that the rest of your method will not throw the exception and will instead have some kind of undesirable behavior if you use a null pointer. In your example, if it's basically a "ToList" wrapper, it might be implemented as:
static List<Integer> list(int[] a)
{
List<int> ret = new List<int>();
foreach (int i in a)
ret.add(i);
return ret;
}
Instead of throwing, this will simply return an empty list (if I recall correctly at least, I don't think C# throws on null lists used in foreach). As such, you'll need to include an explicit null check and throw to get your desired behavior.
Throwing specific exception means that your application has faced something it shouldn't have. It can be either invalid argument(when someone passes null and current method cannot work with value of null), invalid field state(its value has been changed to some value, which is forbidden for instance current state) and many many more.
Basically when you throw exceptions in a good manner, each person using your e.g. library can preserve its correct flow.
I've seen the Index.search method raise a search.TransientError, and I know other methods may raise such errors as well, but in the documentation, I'm seeing code like this:
# Index the document.
try:
index.put(doc)
except search.PutError, e:
result = e.results[0]
if result.code == search.OperationResult.TRANSIENT_ERROR:
# possibly retry indexing result.object_id
except search.Error, e:
# possibly log the failure
When are checks like this necessary, and in which cases can I rely on search.TransientErrorbeing raised?
Looking through the source code, it looks like some (if not all) methods of the Index class use the _MakeSyncSearchServiceCall function, which, if anything goes wrong, calls _ToSearchError passing in an exception. This error returns an element from the _ERROR_MAP dictionary, based on the application_error property of the error. This dictionary contains theTransientError`, so it is clear that it is raised in certain cases.
So, when is it necessary to check result.code, and what other errors could be raised in cases that are out of my control?
So I have seen this error show up, just sometimes, but it is not helpful in describing the actual error which has occured. Nor does it give any clues as to what might cause it to display.
Cannot use modParams with indexes that do not exist.
Can anyone explain more verbosly what this error means, what it relates to (such as a behaviour, component, controller, etc), the most common causes and how to fix it?
To kickstart the investigation, you can find the error here.
https://github.com/cakephp/cakephp/blob/master/lib/Cake/Utility/ObjectCollection.php#L128
Layman's Terms
CakePHP is being told to apply an array of parameters to a collection of objects, such that each particular object can modify the parameters sent on to the next object. There is an error in how CakePHP is being told to do this.
In Depth
Generically, this rises from the CakePHP event publication mechanism. Somewhere in your code is an instance of ObjectCollection, which is being triggered with certain parameters. That is, a method is being called on every object in that collection.
Each callback method is given parameters. Originally the parameters are passed into trigger(). In normal cases (where modParams is false), every callback gets the same parameters. But when modParams is not strictly false, the result of each callback overwrites the parameter indicated by modParams.
So if there are two objects in the collection, modParams is 1, and the params[1] is 'a' initially, then the callback is given the first object with params[1] == a. That callback returns 'b', so when the next callback is called, the second object gets params[1] == b.
The exception raises when the modParams value given does not exist in the originally given params. Eg, if modParams is 2 and params is array (0 => 'a', 1 => 'b'), you'll get this exception.
In your case
Specifically, debugging this has to be done at a low-level because it's a method on a generic class. The backtrace from the Exception should get you bubbled up to a trigger() call on a particular concrete class. That call is being given non-false modParams and a params that doesn't have the given modParams. It could be a code bug in a concrete class extending ObjectCollection, or it could simply be a generic message arising from a method not being given expected arguments.
Have you tried reading the documentation?
/*
* - `modParams` Allows each object the callback gets called on to modify the parameters to the next object.
* Setting modParams to an integer value will allow you to modify the parameter with that index.
* Any non-null value will modify the parameter index indicated.
* Defaults to false.
*/
You did not paste any code, so I guess your 3rd arg of the method contains something wrong.
I've a single statement running on WPF application
that takes a long time (by long I mean 5000ms , which is too long )
i need to run this statement in thread but ,
i need this thread return bool value indicate status
if it successfully execute it return true
else for Exception or i forced to stop this single statement return false
What is the best way to accomplish this?
and again it single statement
and how to safety abort "force to end execute statement "
You might have some luck taking a look at the System.Threading.Tasks.Task<TResult> class. Creating it simply means passing it a Func<TResult> object, so it can be one statement or many. It can handle a return value (that's the difference between it and System.Threading.Tasks.Task). You can also use a CancellationToken to handle a forced stop. See this article for more on that.
You can acomplish that with a background worker. The worker can also report the progress (with events) and have an oncomplete function which you can pass the result too.
For a good guide to it:
http://www.albahari.com/threading/part3.aspx#_BackgroundWorker