Simon McNamara

Ranch Hand
+ Follow
since Nov 13, 2019
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
3
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Simon McNamara

Hello code ranchers!

I'm using 3rd party styles and a style sheet to style some JavaFX. For the main application (Stage), the styles work fine. I apply them in Main:



Now I'm creating a Dialog object using Oracle's built-in Dialog API. I attempt to add the styling to that Dialog:



I encounter one or more exceptions. I actually have no idea what's going on here, so I can't even speculate. In the traces below, "Controller.java" is my controller class.

This is the stack trace when I attempt to apply FXSkins ONLY:


Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1857)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1724)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8792)
at javafx.scene.control.Button.fire(Button.java:203)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
at com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3897)
at javafx.scene.Scene.processMouseEvent(Scene.java:1878)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2623)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
at com.sun.glass.ui.View.handleMouseEvent(View.java:557)
at com.sun.glass.ui.View.notifyMouse(View.java:943)
at com.sun.glass.ui.mac.MacView.notifyMouse(MacView.java:127)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:77)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
at com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1854)
... 60 more
Caused by: java.lang.NullPointerException
at com.ioi.gui.Controller.saveUniformityProfile(Controller.java:334)
... 72 more



This is the trace I see when I attempt to apply "gui.fxml" sheet ONLY:


Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1857)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1724)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8792)
at javafx.scene.control.Button.fire(Button.java:203)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
at com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3897)
at javafx.scene.Scene.processMouseEvent(Scene.java:1878)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2623)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
at com.sun.glass.ui.View.handleMouseEvent(View.java:557)
at com.sun.glass.ui.View.notifyMouse(View.java:943)
at com.sun.glass.ui.mac.MacView.notifyMouse(MacView.java:127)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:77)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:275)
at com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:84)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1854)
... 60 more
Caused by: java.lang.NullPointerException
at com.ioi.gui.Controller.saveUniformityProfile(Controller.java:341)
... 72 more



Any ideas?
3 weeks ago

It seems like you think that cancelling the TimerTask will automatically terminate the ScheduledThreadPoolExecutor which it ran. Or maybe you thought that it was possible to pause a ScheduledThreadPoolExecutor and then allow it to continue



Yes, this is what I thought. Pausing the second timer would also do fine, but I was unable to achieve this behavior. Perhaps I'm attempting a bad idea.

My requirements are the following: I'd like one process to occur every X period. This is the initial attraction to ScheduledThreadPoolExecutor#ScheduledAtFixedRate. Within that X period, after some simple initial code runs, I must sequence execution of a subroutine at regular intervals so I can acquire and average some data temporally (need not be perfectly precise, since I'll be averaging hundreds of samples within the X period). All of this is done with Threads because this code will be later migrated to a GUI. There will be no shared memory between threads, so I'm not concerned about data races.

Last night I continued working on this problem and I came up with the following:



This code works, but it seems inelegant to spawn and throw away threads like this. But maybe that's normal work flow. Not sure.

I recognize also that the while-loop in PulseThread is providing a sort of blocking, but my guess is that nothing stops another thread from the operating system from interrupting execution of PulseThread, throwing off the temporal interval. I recognize also that the threads in main can start in any order, but that doesn't matter for my application.

In my ignorance, I'm attempting to transition from the "I got a thing to work" stage of my skills and into the robust code-writing part of my skills. Threads are my weak spot at the moment. Any feedback is welcomed.

Thank you,
Hello all,

I am reviving this thread since I have now encountered a need which, ironically, more closely matches the title of this thread than my original description.

In this new version of reality, I'd like to use ScheduledThreadPoolExecutor#scheduleAtFixedRate to run some simple initial code once. Then, while the period of that first schedule is waiting, use a second ScheduledThreadPoolExecutor to execute another piece of code at regular intervals. When the first period completes and restarts, that second ScheduledThreadPoolExecutor stops temporarily.

I wrote this up to test this function:



The outer schedule succeeds at running a thing once. But, the inner timer never cancels. Therefore, the inner timer runs 5 times on the first outer period, then 10, then 15, then 20, and so on.

Any suggestions are much appreciated.

Thanks,
Hey All!

Well, turns out that the timing (to hardware) for this code is abysmal. As a result, I have abandoned this thread structure. Many thanks for your feedback.

Until next time,

Campbell Ritchie wrote:

Simon McNamara wrote:. . . This class will be built into a JavaFX GUI later on. . . .

That sounds hazardous. GUI frameworks are usually non‑thread‑safe, so you should run the GUI from one thread and one only. Long tasks in parallel may be run in different threads however.



I mean only that a user can create a FunctionGenerator object as a program feature. The two threads are otherwise completely separate. Am I misunderstanding the potential risk here?

A disadvantage is that if you cancel right after the try has been entered, you have to wai for 7 seonds before the thread finishes, since only then do you check status.



I'm not so concerned with that, since I the typical use case for this code will be states that persist for tens of milliseconds, rather than around 7 seconds. I just used full seconds for illustration.

...it was suggested to use two scheduleAtFixedRate()'s. What is the problem with that?



It's not clear to me how I would write this and keep the two synchronized, e.g., one starts at the end of the first and so on. I do like the scheduleAtFixedRate() methods since the timing won't "drift" over time due to subtle delays in execution inside of run(). I'm just not seeing how to do this well.

Many thanks for the feedback.
Alrighty, so I think I just found a stupid simple solution:



I can run this and print stuff to console in the timing I expect, but I'm pretty new to threads. Are there any peculiar timing details I'm not seeing by using sleep()? I'm sleeping the thread itself, so that shouldn't screw around with my program's other processes, right? This class will be built into a JavaFX GUI later on.

Thanks for your feedback!
Hello,

I'd like to use scheduleAtFixedRate(), or something similar, to execute some code at TWO fixed rates. ScheduleAtFixedRate won't work because it will only run with one fixed period in between each execution at some fixed (one-time) delay.

In other words, it will allow me to run some code every 5ms. I would like to run some code every 5ms, 12ms, 5ms, 12ms, 5ms, 12ms, and so on. Is there some other java library feature to do this? I'm not seeing one. Any tweak or hack to get scheduleAtFixedRate to work would be fine, also.

Thanks for your feedback!
Wow that's such a succinct solution. I don't know python very well, but I'll try my hand at translating that to java!
5 months ago
Thanks Piet, that was driving me bonkers.

I've returned to the question of building the set of all possible combinations (with "duplicates") as discussed earlier. I've started with some version of the pseudo code that Paul suggested.

For an example List = [5, 6, 7], I can get all possible combination of two elements with this:



So, obviously, I place a call to List.add() with two indices as arguments, so I'll only ever get combinations of two items. I'm now trying to generalize this to start by adding all combinations of 3 items, decrementing my list, adding all combinations of 2 items, and so on. However I'm having some trouble figuring out how to add a series of 3 indices. Any suggestions for this?
5 months ago
Paul,

On my way to a solution, I'm strangely confused.

I'm trying here to get all combinations of two lists, where the first list is the 1st dimension of the list and the 2nd list is the 2nd dimension of the list.

args = [ [ 1, 2 ], [8, 9] ]

Why doesn't this work?:



I'm getting:
[1, 2], [1, 2], [8, 9], [8, 9],

Instead of (what I expect):
[1,8], [1,9], [2,8], [2,9]

I don't often work with Lists, maybe I'm missing something in the API.

Thanks for your time.
5 months ago
Thanks Paul!

Yes, what you describe is what I'd like! What a name, huh?

Would anyone happen to know of an implementation of such an esoteric combination?

Many thanks,
5 months ago
Darn.

After some more googling, I think I can better describe what I want as all possible permutations of an array. From what I've seen, all permutations of some array [a, b, c] necessarily involves the many combinations of only 3-item arrays (each resulting array is 3-items long).

This may seem hackey, but perhaps I can retrieve those permutations, decrement my array by one object, compute those permutations, and so on, until I've exhausted my array. Then, I can mash all of the arrays together.

Unless one of you tries to stop me, I might just try this. Really though, if you suggests an easier solution, I'm all ears.

Thanks for your feedback,
5 months ago
Hey Paul,

Yes, you've got it exactly. I do recognize that this is not a power set, strictly speaking. I thought perhaps this is a well-known problem of which I don't know the name!

Thanks for your time,
5 months ago
Hello all,

Well, when it rains, it pours. When I find myself on code ranch, it's usually because I have 5 or 25 questions, not just 1! Here I go again for the 2nd time in 2 days!

I'm looking for a tidy way to assemble every possible combination for arguments I'm feeding to a test.

I have, for example, and array of integers from 4, 7 (inclusive): [ 4, 5, 6, 7 ]

I'd like to basically build the power set. I find lots of useful stuff on the web for building a power set from an array in java. However, I want to include [ 4, 5 ] AND [ 5, 4 ]. A typical power set omits these, as they're considered duplicate. My tests does different things with the 1st or the second item in that subset, so I want to preserve "duplicates".

My current code is:


This code is not mine, it's adapted from Matt McPeak here: https://stackoverflow.com/questions/5162254/all-possible-combinations-of-an-array

This code is great, but it doesn't allow duplicates ( it includes [5, 6] but not [6, 5]). Any feedback is welcomed. Does this problem go by a different name?

Many thanks,
5 months ago
Thanks for the feedback. Yes, I suppose it is rather verbose. I'll check out that link. Thanks for your help.
5 months ago