Uppdate: tried changing the Label field from static to just public. Im still getting the same error.
Im doing my very first Java application and im trying to set some labels to certain price values. (The project is sort of a webshop). But when i use the command label.setText(String) i keep getting the same error.
Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.Label.setText(String)" because "project.AppController.labelProdukt1" is null
The controller code is:
public class AppController {
@FXML private Button VelgKnapp1;
@FXML private TextArea antall;
@FXML public static Label labelProdukt1;
private Stage stage;
private Parent root;
private Scene scene;
//private List<Item> liste = new ArrayList<>();
public void setLabels() {
Cart cart = new Cart();
List<Item> liste = cart.getAvailableItems();
System.out.println(liste);
String price1 = String.valueOf(liste.get(0).getPrice());
System.out.println(price1);
System.out.println(labelProdukt1);
labelProdukt1.setText(price1);
} public void VelgProdukt1(ActionEvent event) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("Produkt.fxml"));
stage = (Stage)((Node)event.getSource()).getScene().getWindow();
scene = new Scene(root);
stage.setScene(scene);
stage.show();
setLabels();
}
the idea here is that when you open the "produkt" scene it will also set the label in that scene to the price of that given item. I used the sysouts as sort of a guideline to see whether the error was because of formatting but when i try to print the price in the console it works perfectly. Im guessing there is some sort of mechanic in java/javafx that im missing but cant see to find the answer online.
PS: if you need one of the fxml files for either the "produkt" or something just let med know.
Any help is appreciated, thank you in advance!