RangeSlider causes NullPointerException when pressing Tab on the focused slider - java

I'm getting NullPointerException when using org.controlsfx.control.RangeSlider. Here is the simple code example:
package experimental_main;
import org.controlsfx.control.RangeSlider;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Experimental_start extends Application
{
public static void main(String[] args)
{
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception
{
Button button = new Button("Click me");
RangeSlider rangeSlider = new RangeSlider(0, 10, 2, 4);
BorderPane borderPane = new BorderPane();
borderPane.setCenter(rangeSlider);
borderPane.setTop(button);
Scene scene = new Scene(borderPane, 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
}
The exception occurs when pressing Tab while right thumb of RangeSlider is focused or pressing Shift+Tab while its left thumb is focused. These key pressings cause to lose focus from RangeSlider as expected, but this action also causes NullPointerException. Here is exception's stack trace:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at com.sun.javafx.scene.traversal.TraversalEngine.select(Unknown Source)
at impl.org.controlsfx.skin.RangeSliderSkin$5.handle(RangeSliderSkin.java:150)
at impl.org.controlsfx.skin.RangeSliderSkin$5.handle(RangeSliderSkin.java:132)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$KeyHandler.process(Unknown Source)
at javafx.scene.Scene$KeyHandler.access$1800(Unknown Source)
at javafx.scene.Scene.impl_processKeyEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.keyEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleKeyEvent$353(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(Unknown Source)
at com.sun.glass.ui.View.handleKeyEvent(Unknown Source)
at com.sun.glass.ui.View.notifyKey(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
at java.lang.Thread.run(Unknown Source)
There is no exception when pressing Tab (or Shift+Tab) with a view to make RangeSlider's thumb focused.
Am I doing something wrong or it's a bug? Is there alternative to RangeSlider class realizations?

This listener of the RangeSliderSkin seems to cause the NPE:
EventHandler<KeyEvent> keyEventHandler = new EventHandler<KeyEvent>() {
#Override public void handle(KeyEvent event) {
if (KeyCode.TAB.equals(event.getCode())) {
if (lowThumb.isFocused()) {
if (event.isShiftDown()) {
lowThumb.setFocus(false);
new ParentTraversalEngine(rangeSlider).select(rangeSlider, Direction.PREVIOUS);
} else {
lowThumb.setFocus(false);
highThumb.setFocus(true);
}
event.consume();
} else if (highThumb.isFocused()) {
if(event.isShiftDown()) {
highThumb.setFocus(false);
lowThumb.setFocus(true);
} else {
highThumb.setFocus(false);
new ParentTraversalEngine(rangeSlider).select(rangeSlider, Direction.NEXT);
}
event.consume();
}
}
}
};
To be more specific, the line new ParentTraversalEngine(rangeSlider).select(rangeSlider, Direction.NEXT);
ParentTraversalEngine is initalized without an Algorithm argument, so calling select(rangeSlider, Direction.NEXT) is causing a NPE, as it is supposed to:
Throws:
java.lang.NullPointerException - if there is no algorithm
javadoc

The bug is fixed beginning from version 8.40.13.

Related

How can i call TextInputDialog at the end of animation?

I'm looking for a way around the problem with showAndWait call.
I've thought onFinishedProperty is fired when the animation process is over, still im unable to call showAndWait because of error saying that im trying to call it during animation process. Is there anyway to call my dialog right after finish of animation?
TextInputDialog dialog = new TextInputDialog("Litera");
dialog.setTitle("");
dialog.setHeaderText("");
dialog.setContentText("Wybierz literÄ™:");
for(int i = 0 ; i<pieChartData.size();i++)
System.out.println(pieChartData.get(i));
RotateTransition rotateTransition = new RotateTransition();
rotateTransition.setDuration(Duration.millis(5000));
rotateTransition.setNode(fortuna);
rotateTransition.setCycleCount(1);
rotate.setOnAction(
(e)->{
fortuna.setRotate(0);
rotate.setDisable(true);
rotateTransition.setAutoReverse(false);
rotateTransition.setByAngle((int)(Math.random()*10800+360));
rotateTransition.play();
rotateTransition.onFinishedProperty().set(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println(fortuna.getRotate());
Optional<String> result = dialog.showAndWait();
rotate.setDisable(false);
}
}
);
});
Exception in thread "JavaFX Application Thread" java.lang.IllegalStateException: showAndWait is not allowed during animation or layout processing
at javafx.controls/javafx.scene.control.Dialog.showAndWait(Unknown Source)
at Main$1.handle(Main.java:78)
at Main$1.handle(Main.java:1)
at javafx.graphics/javafx.animation.Animation.finished(Unknown Source)
at javafx.graphics/javafx.animation.AnimationAccessorImpl.finished(Unknown Source)
at javafx.graphics/com.sun.scenario.animation.shared.SingleLoopClipEnvelope.timePulse(Unknown Source)
at javafx.graphics/javafx.animation.Animation.doTimePulse(Unknown Source)
at javafx.graphics/javafx.animation.Animation$1.lambda$timePulse$0(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/javafx.animation.Animation$1.timePulse(Unknown Source)
at javafx.graphics/com.sun.scenario.animation.AbstractMasterTimer.timePulseImpl(Unknown Source)
at javafx.graphics/com.sun.scenario.animation.AbstractMasterTimer$MainLoop.run(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulse(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(Unknown Source)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$11(Unknown Source)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(Unknown Source)
at java.base/java.lang.Thread.run(Unknown Source)

JavaFX Clock widget throws Exceptions randomly

I have written a little Clock Widget extending the Text class of JavaFX. To update the Time I am using a Task that basically sets the text to the current System Time. When I run this application in Eclipse it sometimes throws a NullpointerException in the line I call stage.show().
This is what my widgets sourcecode looks like:
package clock;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import javafx.concurrent.Task;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
public class CurrentClockText extends Text {
private final DateTimeFormatter formatter;
public static final int HOUR = 1;
public static final int HOUR_MINUTE = 2;
public static final int HOUR_MINUTE_SECOND = 3;
public static final int HOUR_MINUTE_SECOND_MILLISECOND = 4;
private final long updateInterval;
private final Task<Void> updater = new Task<Void>() {
#Override
protected Void call() throws Exception {
while (true) {
LocalDateTime now = LocalDateTime.now();
String nowTextual = formatter.format(now);
setText(nowTextual);
try {
Thread.sleep(updateInterval);
} catch (InterruptedException ignore) {
}
}
}
};
public CurrentClockText() {
this(HOUR_MINUTE);
}
public CurrentClockText(final int detailLevel) {
String timeFormat = "";
switch (detailLevel) {
case HOUR:
timeFormat = "HH";
updateInterval = 60000;
break;
case HOUR_MINUTE:
timeFormat = "HH:mm";
updateInterval = 15000;
break;
case HOUR_MINUTE_SECOND:
timeFormat = "HH:mm:ss";
updateInterval = 500;
break;
case HOUR_MINUTE_SECOND_MILLISECOND:
updateInterval = 1;
timeFormat = "HH:mm:ss.S";
break;
default:
throw new IllegalArgumentException(
"Unknown detail level for Clock: " + detailLevel);
}
setFont(new Font("Verdana", 28));
formatter = DateTimeFormatter.ofPattern(timeFormat);
Thread updaterThread = new Thread(updater, "CurrentClockText.updaterThread");
updaterThread.setDaemon(true);
updaterThread.start();
}
}
This is the main class:
package clock;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Boot extends Application {
#Override
public void start(Stage stage) throws Exception {
BorderPane pane = new BorderPane();
CurrentClockText clock = new CurrentClockText(4);
pane.setCenter(clock);
Scene scene = new Scene(pane);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
Boot.launch(args);
}
}
And this is the stacktrace:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$50/14845382.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at com.sun.javafx.text.PrismTextLayout.addTextRun(Unknown Source)
at com.sun.javafx.text.GlyphLayout.addTextRun(Unknown Source)
at com.sun.javafx.text.GlyphLayout.breakRuns(Unknown Source)
at com.sun.javafx.text.PrismTextLayout.buildRuns(Unknown Source)
at com.sun.javafx.text.PrismTextLayout.layout(Unknown Source)
at com.sun.javafx.text.PrismTextLayout.ensureLayout(Unknown Source)
at com.sun.javafx.text.PrismTextLayout.getBounds(Unknown Source)
at javafx.scene.text.Text.getLogicalBounds(Unknown Source)
at javafx.scene.text.Text.impl_computeLayoutBounds(Unknown Source)
at javafx.scene.Node$12.computeBounds(Unknown Source)
at javafx.scene.Node$LazyBoundsProperty.get(Unknown Source)
at javafx.scene.Node$LazyBoundsProperty.get(Unknown Source)
at javafx.scene.Node.getLayoutBounds(Unknown Source)
at javafx.scene.Node.prefWidth(Unknown Source)
at javafx.scene.Node.minWidth(Unknown Source)
at javafx.scene.layout.Region.computeChildPrefAreaWidth(Unknown Source)
at javafx.scene.layout.BorderPane.getAreaWidth(Unknown Source)
at javafx.scene.layout.BorderPane.computePrefWidth(Unknown Source)
at javafx.scene.Parent.prefWidth(Unknown Source)
at javafx.scene.layout.Region.prefWidth(Unknown Source)
at javafx.scene.Scene.getPreferredWidth(Unknown Source)
at javafx.scene.Scene.resizeRootToPreferredSize(Unknown Source)
at javafx.scene.Scene.preferredSize(Unknown Source)
at javafx.scene.Scene.impl_preferredSize(Unknown Source)
at javafx.stage.Window$9.invalidated(Unknown Source)
at javafx.beans.property.BooleanPropertyBase.markInvalid(Unknown Source)
at javafx.beans.property.BooleanPropertyBase.set(Unknown Source)
at javafx.stage.Window.setShowing(Unknown Source)
at javafx.stage.Window.show(Unknown Source)
at javafx.stage.Stage.show(Unknown Source)
at clock.Boot.start(Boot.java:19)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/19600960.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/18503843.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/27167109.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/2180324.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/3326003.run(Unknown Source)
... 1 more
Exception running application clock.Boot
As I said this Exception only occurs in some runs and not always. It happens mostly when I change the formatting pattern of detailLevel = 4 but remains for another run after changing it back again. I am guessing this might actually have something to do with Eclipse but I can't debug the code either because the Exception gets thrown in the stage.show call which I absolutely do not understand. What's the cause of this random Exception and how can I fix it?
In JavaFX, as in most other GUI toolkits, there is one specific thread which handles all UI related operations. An application must not update the UI outside of this thread. If the UI needs to be updated from another thread, there are usually APIs available which ensure that code is executed in the context of the UI thread. In case of JavaFX, see the Concurrency in JavaFX Tutorial for more information.
In your case, the simplest solution would be to make sure that your setText() call is executed on the JavaFX application thread, not on the thread associated with your Task:
...
Platform.runLater(() -> setText(nowTextual));
...
There are also other APIs available in JavaFX to do animations, which can be used to call a handler method at specific time intervals - that would remove the Thread.sleep() call from your loop. See Timeline for more information.

Java null pointer exception java fx

I wrote a code which gives a Java null pointer exception in JavaFX8. There are two classes: one that does a processing to an image Mat and another one that shows the image processed, in a different window. The code is:
public class ImagineComprimata
{
public Mat imComp;
#FXML
private ImageView imagineCompresata;
#FXML
private Button bSalveaza;
#FXML
private TextArea campRC;
#FXML
private TextArea campEMP;
#FXML
public void salveaza(ActionEvent eveniment)
{
//this will be edited later
}
public void arata() throws IOException
{
Stage scena = new Stage();
FXMLLoader fl = new FXMLLoader(getClass().getResource("/fxml/ImagineComprimata.fxml"));
AnchorPane p = (AnchorPane) fl.load();
Scene s = new Scene(p, 742, 495);
scena.setScene(s);
scena.showAndWait();
}
public void setMat(Mat imagine)
{
this.imComp = imagine;
}
public void setImagineCompresata(Image imagine)
{
this.imagineCompresata.setImage(imagine);
}
}
Now in the main window from which i make a call of this class to be displayed on screen:
public class Compresia
{
private Mat imagine;
private int parametru;
#FXML
public Button bSelecteaza;
#FXML
public Button bComprima;
#FXML
public Button bIntrodu;
#FXML
private ImageView foto;
#FXML
private TextField valoarea;
private String denumireFotografie;
private Matrix[] desc;
#FXML
public TextArea campRang;
private int rang;
#FXML
public void comprima(ActionEvent eveniment) throws InterruptedException, IOException
{
if (imagine == null)
{
Notificare.informeaza("Notificare", "Nu ai selectat o imagine!");
}
else
{
double[][] mc = AlgoritmDeCompresie.comprimaImaginea(desc, Util.extragereValori(imagine), parametru);
Mat im = Util.construiesteImagine(mc);
ImagineComprimata ic = new ImagineComprimata();
ic.setMat(im);
ic.setImagineCompresata(Util.conversieMat2Image(im));
ic.arata();
}
}
#FXML
public void cautaFisier(ActionEvent eveniment) throws InterruptedException
{
Matrix[] desc;
Mat im;
String denumire;
FileChooser f = new FileChooser();
File fotoSelectat = f.showOpenDialog(null);
if (fotoSelectat != null)
{
denumire = fotoSelectat.getPath();
im = Highgui.imread(denumire);
Imgproc.cvtColor(im, im, Imgproc.COLOR_BGR2GRAY);
this.desc = AlgoritmDeCompresie.descompune(Util.extragereValori(im));//////
rang = Util.returneazaRangul(Util.extragereValori(im));
imagine = im;
campRang.setText(Integer.toString(rang));
foto.setImage(Util.conversieMat2Image(im));
}
}
#FXML
public void seteazaParametru(ActionEvent eveniment)
{
int p = Integer.parseInt(this.valoarea.getText());
if (p >= this.rang)
{
Notificare.informeaza("Notificare", "Introdu un parametru mai mic decat rangul!");
this.valoarea.setText(null);
}
else
{
this.parametru = p;
Notificare.informeaza("Notificare", "parametru setat :" + p);
}
///////////////
}
public void start() throws Exception
{
Stage scena = new Stage();
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/Compresia.fxml"));
AnchorPane b = (AnchorPane) loader.load();
Scene s = new Scene(b, 955, 586);
scena.setScene(s);
scena.setTitle("Compresie");
scena.setResizable(false);
scena.show();
}
public Matrix[] getDescompunere()
{
return this.desc;
}
public void setDescompunere(Matrix[] descompunere)
{
this.desc = descompunere;
}
}
It gives me an error at setImageCompressed(im) which traces to the setImageCompressed(Mat im). ImageCompressed is the JavaFX controller which shows the image proccessed. Where did I do wrong? The error report looks like this:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Node.fireEvent(Unknown Source)
at javafx.scene.control.Button.fire(Unknown Source)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$149(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
... 49 more
Caused by: java.lang.NullPointerException
at interfata.ImagineComprimata.setImagineCompresata(ImagineComprimata.java:56)
at interfata.Compresia.comprima(Compresia.java:66)
... 58 more
It may be the case that "private ImageView imagineCompresata" instance of ImagineComprimata class is not getting initialized and remains pointing to a null , as a result of which when you are calling this.imagineCompresata.setImage(imagine) a NullPointerException is being raised.

JavaFX transition scene from Controller

I am very new to JavaFX (and java in general) and I have been trying to transition between scenes from an FXML controller. I have tried to look up multiple solutions online however, none of them seem to work.
My main java code:
package main;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class App extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
Scene scene1 = new Scene(root);
primaryStage.setScene(scene1);
primaryStage.setTitle("Login");
primaryStage.show();
}
}
... and my LoginController:
package main;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class LoginController implements Initializable
{
#FXML
private Label loginLabel;
#FXML
private TextField fieldUsername;
#FXML
private PasswordField fieldPassword;
#FXML
public void loginEvent(ActionEvent event) throws Exception{
//This is where I try to change the scene
if (fieldUsername.getText().equals("admin") && fieldPassword.getText().equals("admin")){
Parent parent = FXMLLoader.load(getClass().getResource("Main.fxml"));
Stage primaryStage = new Stage();
Scene scene = new Scene (parent);
primaryStage.setScene(scene);
primaryStage.setTitle("Main Frame");
primaryStage.show();
}
else {
loginLabel.setText("Incorrect Username or Password");
}
}
#Override
public void initialize(URL arg0, ResourceBundle arg1) {}
}
Here is the Error I get:
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
... 48 more
Caused by: javafx.fxml.LoadException:
/F:/Programming/JAVA/Eclipse/Password%204/bin/main/Main.fxml:9
at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.access$700(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(Unknown Source)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(Unknown Source)
at javafx.fxml.FXMLLoader$Element.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader.processStartElement(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at main.LoginController.loginEvent(LoginController.java:34)
... 57 more
Caused by: java.lang.ClassNotFoundException: main.MainController
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 71 more
My way of changing the scene doesn't work. How would I go about this issue?
Thanks in advance.
What is wrong with your solution
Your getResource argument is wrong - you should not use a file path (e.g. F:/) instead you should use something related to your class path.
You may have other errors, I haven't checked, just wanted to note that obvious one.
How to fix it
Easiest solution is to place the Main.fxml in the same directory as LoginController.java and check that, when you compile the program, Main.fxml has been copied by your build system to the same directory as LoginController.class.
For your lookup just use FXMLLoader.load(getClass().getResource("Main.fxml")); (similarly for your Login.fxml).
Sample code
Here is a sample for switching FXML based scenes (your code could be simpler if you want to replace the scene content as a whole rather than parts of the scene like the sample does).

JComboBox cascade

I am trying to design a three cascade JComboBox in JAVA:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class ThreeCascadeJComboBox {
private JComboBox combo1;
private JComboBox combo2;
private JComboBox combo3;
public static void main(String[] args) {
new ThreeCascadeJComboBox();
}
public ThreeCascadeJComboBox() {
JFrame v = new JFrame();
v.getContentPane().setLayout(new FlowLayout());
combo1 = new JComboBox();
loadCombo1();
combo1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
loadCombo2((String) combo1.getSelectedItem());
}
});
combo2 = new JComboBox();
loadCombo2((String) combo1.getSelectedItem());
combo2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
loadCombo3((String) combo2.getSelectedItem());
}
});
combo3 = new JComboBox();
loadCombo3((String) combo2.getSelectedItem());
v.getContentPane().add(combo1);
v.getContentPane().add(combo2);
v.getContentPane().add(combo3);
v.pack();
v.setVisible(true);
v.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
private void loadCombo1() {
combo1.addItem("letters");
combo1.addItem("numbers");
}
private void loadCombo2(String seleccionEnCombo1) {
combo2.removeAllItems();
if (seleccionEnCombo1.equals("letters")) {
combo2.addItem("A");
combo2.addItem("B");
combo2.addItem("C");
} else if (seleccionEnCombo1.equals("numbers")) {
combo2.addItem("1");
combo2.addItem("2");
combo2.addItem("3");
}
}
private void loadCombo3(String seleccionEnCombo2) {
combo3.removeAllItems();
if (seleccionEnCombo2.equals("A")) {
combo3.addItem("A-1");
combo3.addItem("A-2");
combo3.addItem("A-3");
} else if (seleccionEnCombo2.equals("B")) {
combo3.addItem("B-1");
combo3.addItem("B-2");
combo3.addItem("B-3");
} else if (seleccionEnCombo2.equals("C")) {
combo3.addItem("C-1");
combo3.addItem("C-2");
combo3.addItem("C-3");
} else if (seleccionEnCombo2.equals("1")) {
combo3.addItem("1-a");
combo3.addItem("1-b");
combo3.addItem("1-c");
} else if (seleccionEnCombo2.equals("2")) {
combo3.addItem("2-a");
combo3.addItem("2-b");
combo3.addItem("2-c");
} else if (seleccionEnCombo2.equals("3")) {
combo3.addItem("3-a");
combo3.addItem("3-b");
combo3.addItem("3-c");
}
}
}
But I get the next Exception when I select the numbers value in the jcombo1:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at es.mycompany.MyView.ThreeCascadeJComboBox.loadCombo3(ThreeCascadeJComboBox.java:78)
at es.mycompany.MyView.ThreeCascadeJComboBox.access$3(ThreeCascadeJComboBox.java:76)
at es.mycompany.MyView.ThreeCascadeJComboBox$2.actionPerformed(ThreeCascadeJComboBox.java:40)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.contentsChanged(Unknown Source)
at javax.swing.JComboBox.intervalRemoved(Unknown Source)
at javax.swing.AbstractListModel.fireIntervalRemoved(Unknown Source)
at javax.swing.DefaultComboBoxModel.removeAllElements(Unknown Source)
at javax.swing.JComboBox.removeAllItems(Unknown Source)
at es.mycompany.MyView.ThreeCascadeJComboBox.loadCombo2(ThreeCascadeJComboBox.java:63)
at es.mycompany.MyView.ThreeCascadeJComboBox.access$1(ThreeCascadeJComboBox.java:62)
at es.mycompany.MyView.ThreeCascadeJComboBox$1.actionPerformed(ThreeCascadeJComboBox.java:30)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.setSelectedItem(Unknown Source)
at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
The exception is thrown because at that point seleccionEnCombo2 is null.
You could add a check for null in the combo2 ActionListener and it will work fine:
if (combo2.getSelectedItem() != null) {
loadCombo3((String) combo2.getSelectedItem());
}
The problem is that the ActionListener for combo1 is triggering an ActionEvent for combo2 which will not have any selected item (as it is empty). You could add a check:
if (combo2.getSelectedItem() != null) {
loadCombo3((String) combo2.getSelectedItem());
}
As the other posts have mentioned, the selected value for the combo is null in some cases. This is occurring because you probably do not realize the ActionListener for combo2 is getting called twice. The first time it gets called is during the call to removeAllElements. This is where the null value is coming from. The second time is what you are assuming in your code to be the only call--this is in response to both population of the combo box as well as user interaction.
When you load the second combo box, that fires the action event for that box (because an action has taken place [actions are not limited to selection]. The 2nd combo box's actionPerformed attempts to load the 3rd combo box based on the 2nd combo box's selection, and there isn't any. That's your null pointer, the non-existent selection from the second combo box.

Categories

Resources