-1

I have edited this question; I couldn't find a solution to my problem, and I have decided on catching the error and handling it instead.

I'm using a Spinner controller to accept Integer values. How do I catch this kind of error java.lang.NumberFormatException ? I'm getting this error when a user enters a character into the text edit box.

The Spinner is editable.

I really appreciate any help you can provide.

My code:

Spinner<Integer> mySpin = new Spinner<Integer>(50, 80, 50);

How can I catch the error and display a message to the user to indicate the error?

    Exception in thread "JavaFX Application Thread" 
    
        java.lang.NumberFormatException: For input string: "d"
            at 
    
        

java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
        at java.base/java.lang.Integer.parseInt(Integer.java:660)
        at java.base/java.lang.Integer.valueOf(Integer.java:991)
        at javafx.base/javafx.util.converter.IntegerStringConverter.fromString(IntegerStringConverter.java:49)
        at javafx.base/javafx.util.converter.IntegerStringConverter.fromString(IntegerStringConverter.java:35)
        at javafx.controls/javafx.scene.control.Spinner.commitValue(Spinner.java:455)
        at javafx.controls/javafx.scene.control.Spinner.lambda$new$3(Spinner.java:163)
        at javafx.base/com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:348)
        at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
        at javafx.base/javafx.beans.property.ReadOnlyBooleanPropertyBase.fireValueChangedEvent(ReadOnlyBooleanPropertyBase.java:72)
        at javafx.graphics/javafx.scene.Node$FocusedProperty.notifyListeners(Node.java:8148)
        at javafx.graphics/javafx.scene.Node.setFocused(Node.java:8201)
        at javafx.graphics/javafx.scene.Scene$KeyHandler.setWindowFocused(Scene.java:4026)
        at javafx.graphics/javafx.scene.Scene$KeyHandler.lambda$new$0(Scene.java:4048)
        at javafx.base/com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:136)
        at javafx.base/com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80)
        at javafx.base/javafx.beans.property.ReadOnlyBooleanPropertyBase.fireValueChangedEvent(ReadOnlyBooleanPropertyBase.java:72)
        at javafx.base/javafx.beans.property.ReadOnlyBooleanWrapper.fireValueChangedEvent(ReadOnlyBooleanWrapper.java:103)
        at javafx.base/javafx.beans.property.BooleanPropertyBase.markInvalid(BooleanPropertyBase.java:111)
        at javafx.base/javafx.beans.property.BooleanPropertyBase.set(BooleanPropertyBase.java:145)
        at javafx.graphics/javafx.stage.Window.setFocused(Window.java:675)
        at javafx.graphics/javafx.stage.Window$1.setFocused(Window.java:150)
        at javafx.graphics/com.sun.javafx.stage.WindowHelper.setFocused(WindowHelper.java:112)
        at javafx.graphics/com.sun.javafx.stage.WindowPeerListener.changedFocused(WindowPeerListener.java:64)
        at javafx.graphics/com.sun.javafx.tk.quantum.GlassWindowEventHandler.run(GlassWindowEventHandler.java:126)
        at javafx.graphics/com.sun.javafx.tk.quantum.GlassWindowEventHandler.run(GlassWindowEventHandler.java:40)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
        at javafx.graphics/com.sun.javafx.tk.quantum.GlassWindowEventHandler.lambda$handleWindowEvent$4(GlassWindowEventHandler.java:176)
        at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
        at javafx.graphics/com.sun.javafx.tk.quantum.GlassWindowEventHandler.handleWindowEvent(GlassWindowEventHandler.java:174)
        at javafx.graphics/com.sun.glass.ui.Window.handleWindowEvent(Window.java:1346)
        at javafx.graphics/com.sun.glass.ui.Window.notifyFocus(Window.java:1325)
        at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
        at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
        at java.base/java.lang.Thread.run(Thread.java:831)
Al1nuX
  • 393
  • 1
  • 4
  • 17
  • Does this help? [Insert only numbers in Spinner Control](https://stackoverflow.com/questions/25885005/insert-only-numbers-in-spinner-control) – Abra Oct 03 '21 at 10:50
  • @kleopatra I will correct the name. thanks for the reminder. – Al1nuX Oct 03 '21 at 11:24
  • setEditable(false)? You did read the api doc, didn't you :) – kleopatra Oct 03 '21 at 14:21
  • @kleopatra, I don't want to disable the entry, I just need to stop the user from entering a character. – Al1nuX Oct 03 '21 at 15:34
  • Not sure you _can_ catch it, as the stack trace doesn't touch your code. – Slaw Oct 03 '21 at 20:35
  • @Slaw, can you please explain more? – Al1nuX Oct 03 '21 at 21:18
  • Your code doesn't show up in the stack trace. Notice every line of code in the stack trace is either in the `java.base`, `javafx.base`, `javafx.graphics`, or `javafx.controls` module. Those aren't your modules. Because of this, there's no where in your code where you could put a `try`-`catch` block to catch the exception. – Slaw Oct 03 '21 at 22:33
  • If I'm not mistaken, the exception is being caught and reported here: https://github.com/openjdk/jfx/blob/master/modules/javafx.base/src/main/java/com/sun/javafx/binding/ExpressionHelper.java#L135 – Slaw Oct 03 '21 at 22:36
  • 2
    @Slaw well .. no messing needed ;) This question evolved to the worse: starting out with being unclear (me at least couldn't decipher the requirement - that's why editability came into play - Abra seems to have been on the right track but never got a comment from the OP), getting an inadequate answer (suggesting exception handling) and now being a real xy-problem ;) – kleopatra Oct 04 '21 at 09:58
  • okay: starting again - you need an editable spinner and intercept invalid input from the user (either on typing or on committing)? If so, what's wrong with the answer referenced by @Abra? It does solve _exactly_ that problem :) Learn how to use a TextFormatter, when stuck, come back with a [mcve] demonstrating what's not working as expected. – kleopatra Oct 04 '21 at 10:02
  • @Slaw, Thanks, I think your answer is the best solution. I have used this Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> { }); – Al1nuX Oct 05 '21 at 15:16

2 Answers2

-1

I have solved this issue using this code as suggested by @Slaw.

Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> {

            System.out.println(throwable.getClass() + " - Error.");
    
        });

Thanks to everyone else also.

Al1nuX
  • 393
  • 1
  • 4
  • 17
  • no, that's not a solution but a hack to fix the "y" of an xy-problem - instead, fix the "x" (the original problem) with a textFormatter on the spinner's editor – kleopatra Oct 05 '21 at 15:44
  • It is a solution and it works for me. Thanks @Slaw. – Al1nuX Oct 06 '21 at 11:11
  • Maybe you should provide a solution instead of down voting other's solutions. And try to relax a little, we are trying to learn. – Al1nuX Oct 06 '21 at 11:16
  • you already got a solution (see the very first comment to your question) - it's _your_ job to apply it to your context. When stuck, post a new question with a [mcve] demonstrating how it doesn't help. – kleopatra Oct 06 '21 at 11:20
-2

You can catch those errors using a try catch statement when inserting elements in your spinner.

  • hmm .. how exactly? The user is typing/pasting and you catch errors by .. ? But then, the question has not enough details for a reasonable answer, so we probably should wait a bit before investing time/effort :) – kleopatra Oct 03 '21 at 11:05
  • @Ivan, I have tried but can not understand why nothing happens. – Al1nuX Oct 03 '21 at 15:32