This question already has an answer here:
- What is a NullPointerException, and how do I fix it? 12 answers
I want my progressbar to display the values of a method thats returns double values but it doesnt work. But I dont think thats the main problem bc even when I just use a specific number it doesnt work either. Please help me, thanks in advance. That's specific question so I searched cery long without results so if you know a better to do what I want to do or maybe know a website that could help pls link it or tell me.
My Controller for my fxml file(the relevant code is marked):
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ProgressBar;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;
import java.io.IOException;
import java.util.ResourceBundle;
import java.util.ArrayList;
import java.util.Random;
public class Controller {
playercard iceborn = new playercard(3, false, true, 0);
playercard fireball = new playercard(5, true, false, 0);
playercard warmogs = new playercard(0, false, false, 10);
ArrayList<playercard> playerhand = new ArrayList<>();
*******************************
public void add() {
playerhand.add(iceborn);
playerhand.add(fireball);
playerhand.add(warmogs);
}
public playercard output() {
Random rand = new Random();
int xzufallszahl = rand.nextInt(3);
int zufallszahl = xzufallszahl;
return playerhand.get(zufallszahl);
}
*********************************
@FXML
Stage stage;
public Button Startgameb;
@FXML
public ProgressBar enemyhpnow;
@FXML
public void Startgamebaction(ActionEvent event) throws IOException{
***************
add();
**************
Parent root = FXMLLoader.load(getClass().getResource("Menu.fxml"));
Pane root1 = FXMLLoader.load(getClass().getResource("Tutorial.fxml"));
Pane root2 = FXMLLoader.load(getClass().getResource("Game.fxml"));
Scene tutorialscene = new Scene(root1, 900, 600);
Scene gamescene = new Scene(root2, 900, 600);
Image card = new Image("sample/card.jpg");
ImageView firstplayercard = new ImageView();
firstplayercard.setImage(card);
firstplayercard.setFitWidth(120);
firstplayercard.setFitHeight(160);
firstplayercard.setLayoutX(50);
firstplayercard.setLayoutY(430);
ImageView secondplayercard = new ImageView();
secondplayercard.setImage(card);
secondplayercard.setFitWidth(120);
secondplayercard.setFitHeight(160);
secondplayercard.setLayoutX(175);
secondplayercard.setLayoutY(430);
ImageView thirdplayercard = new ImageView();
thirdplayercard.setImage(card);
thirdplayercard.setFitWidth(120);
thirdplayercard.setFitHeight(160);
thirdplayercard.setLayoutX(300);
thirdplayercard.setLayoutY(430);
ImageView fourthplayercard = new ImageView();
fourthplayercard.setImage(card);
fourthplayercard.setFitWidth(120);
fourthplayercard.setFitHeight(160);
fourthplayercard.setLayoutX(425);
fourthplayercard.setLayoutY(430);
ImageView fifthplayercard = new ImageView();
fifthplayercard.setImage(card);
fifthplayercard.setFitWidth(120);
fifthplayercard.setFitHeight(160);
fifthplayercard.setLayoutX(550);
fifthplayercard.setLayoutY(430);
root2.getChildren().addAll(firstplayercard, secondplayercard, thirdplayercard, fourthplayercard, fifthplayercard);
**********************************
firstplayercard.setOnMouseClicked(e ->{
enemyhpnow.setProgress(output().activate());
});
**********************************
Stage stage;
stage=(Stage) Startgameb.getScene().getWindow();
stage.setScene(gamescene);
stage.show();
}
}
That's the class I made the object from which values I want to use:
package sample;
import javafx.fxml.FXML;
import javafx.scene.control.ProgressBar;
public class playercard {
int dmgovertime;
boolean fire;
boolean ice;
int getlife;
int playerhp = 100;
int enemyhp = 300;
public playercard(int dmgovertime, boolean fire, boolean ice, int getlife) {
this.dmgovertime = dmgovertime;
this.fire = fire;
this.ice = ice;
this.getlife = getlife;
}
public double activate(){
enemyhp = enemyhp - dmgovertime + getlife;
double xenemyhp = enemyhp;
System.out.println(enemyhp);
return xenemyhp;
}
}
That's my fxml file (the relevant one):
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="900.0" styleClass="hintergrund1" stylesheets="@style.css" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<ProgressBar fx:id="playerhp" layoutX="100.0" layoutY="20.0" prefHeight="20.0" prefWidth="700.0" progress="0.87" />
<Label layoutX="425.0" layoutY="-14.0" prefHeight="50.0" prefWidth="50.0" text="100 HP" />
<ImageView fitHeight="351.0" fitWidth="373.0" layoutX="273.0" layoutY="40.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@enemy.png" />
</image>
</ImageView>
<Label layoutX="812.0" layoutY="15.0" prefHeight="50.0" prefWidth="66.0" text="1/3" />
<Label layoutX="14.0" layoutY="11.0" prefHeight="59.0" prefWidth="77.0" text="1/5" />
<ProgressBar fx:id="enemyhpnow" layoutX="283.0" layoutY="391.0" prefHeight="31.0" prefWidth="353.0" progress="0.75" />
<Label layoutX="431.0" layoutY="374.0" text="250 HP" />
<ImageView fitHeight="160.0" fitWidth="120.0" layoutX="758.0" layoutY="422.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@carddeck.png" />
</image>
</ImageView>
</children>
</Pane>
My Main:
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("Menu.fxml"));
Scene menuscene = new Scene(root, 900, 600);
primaryStage.setTitle("Project: Spark!");
primaryStage.setScene(menuscene);
primaryStage.show();
}
}
Thats the error I get with enemyhpnow.setProgress(output().activate()); from my Controller:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at sample.Controller.lambda$Startgamebaction$0(Controller.java:100)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
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.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$ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Process finished with exit code 0
And thats the error code when I use enemyhpnow.setProgress(1); also in my Controller:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at sample.Controller.lambda$Startgamebaction$0(Controller.java:100)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
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.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$ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Process finished with exit code 0