JavaFX NullPointer Exception Location is required - javafx

I have an FXML file that is in a separate package. this answer is close but it is located in the same package.
the what confuses me is
Where is this path /src/resources/.fxml that some of the answers reference
How do I "copy" my fxml code into it.
This is the line of code that is getting me Nullpointerexception: Location is Required
root = FXMLLoader.load(getClass().getResource("IMSView/AppView.fxml"));
I have tried it with
root = FXMLLoader.load(getClass().getResource("/IMSView/AppView.fxml"));
Still get the same failure.
I am very new to java and I know I am doing something wrong. I could get this project to run when everything was in one file path. Now that I have added packages I cannot get the fxml to load.
Thanks for your time.
EDIT
I am using netbeans
The Main method ins in IMS.java The FXML that I want to load is in a package called IMSview and is name AppView.fxml
Project files
My Code
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ims;
import IMSModel.Product;
import IMSModel.Part;
import IMSView.AddProductController;
import IMSView.AppViewController;
import IMSView.AddPartController;
import java.io.IOException;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
/**
*
* #author DBarnett
*/
public class IMS extends Application {
private Stage primaryStage;
private Stage window;
private BorderPane rootLayout;
private ObservableList<Part> PartData = FXCollections.observableArrayList();
private ObservableList<Product> ProductData = FXCollections.observableArrayList();
public IMS() {
// Add some sample data
// PartData.add(new InHouse(1,"Silly Cow",1,23));
// ProductData.add(new OutSourced(2"dog",2,3));
}
/**
* Returns the data as an observable list of Parts.
*
* #return
*/
public ObservableList<Part> getPartData() {
return PartData;
}
public ObservableList<Product> getProductData() {
return ProductData;
}
#Override
public void start(Stage primaryStage) throws IOException {
window = primaryStage;
Parent root;
root = FXMLLoader.load(getClass().getResource("IMSView/AppView.fxml"));
Scene scene = new Scene(root);
primaryStage.setTitle("IMS");
primaryStage.setScene(scene);
primaryStage.show();
// this.primaryStage = primaryStage;
// this.primaryStage.setTitle("Inventory Management System");
window.setOnCloseRequest(e -> {
e.consume();
closeProgram();
});

Related

Blank window using Javafx

I made a window by using the screen builder program, but after I run it, only a white blank window appears. I think the problem that all variables , which are lblOUTPUT, aren't assigned. I'm a beginner at Scene builder and have been really struggling with this issue because it's my first work.
FXmain
package javaapplication6;
import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* #author ammar
*/
public class FXMain extends Application {
#Override
public void start(Stage stage) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("/javaapplication6/FXML.fxml"));
Scene scene = new Scene(root, 600, 400);
stage.setScene(scene);
stage.setTitle("Kilometer to miles converter");
stage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
FXMLcontroller:
enter code here
package javaapplication6;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
/**
* FXML Controller class
*
* #author ammar
*/
public class FXMLController implements Initializable {
#FXML
private Label lblOUTPUT;
#FXML
private Button btnCONVERT;
#FXML
private TextField txtKm;
/**
* Initializes the controller class.
*/
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Ok, I solved after two hours of struggling, I just click the run button in the NetBeans (looks like a play button) and after that, right-click on the main class, then run the file, and it worked!.

Java fx event handler

Why my code is not running? How am I going to display an image on the interface when clicked "add image"?
import javafx.stage.Stage;
import javafx.scene.*;
import javafx.application.Application;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
public class GUIProject extends Application{
/**
* #param args the command line arguments
*/
public void start (Stage stage) throws Exception
{
Pane bPane = new Pane();
Button btOK = new Button("OK");
OKAction OKact = new OKAction();
btOK.setOnAction(OKact);
Button addImage = new Button("Add Image");
isAdd addimage = new isAdd();
addImage.setOnAction(addimage);
//bPane.getChildren().add(btOK);
bPane.getChildren().add(addImage);
Scene scene = new Scene(bPane);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
// TODO code application logic here
Application.launch(args);
}
}
import java.io.FileInputStream;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
public class Pane extends StackPane {
public void addImage() throws Exception
{
Image logo = new Image(new FileInputStream("logo.jpg"));
ImageView logoView = new ImageView(logo);
this.getChildren().add(logoView);
}
}
import java.io.FileInputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
class isAdd implements EventHandler<ActionEvent>{
public void handle(ActionEvent event)
{
try {
Pane.displayImage();
}
catch (Exception ex) {
System.out.println("Error");;
}
}
}
It keep showing static method cannot with non-static method, how can I solve that?
Is there any website can learn javafx event handler?

how to dynamically set new TreeItems in a TreeView in JavaFX [duplicate]

I am begging with JavaFx, and I realized that I need some help to update a TreeView with some TreeItems in runtime, and it should be updated in the main window.
Here, you can see a screenshot of the two windows:
The bigger is the main window and it calls (by clicking in File >> New Project), new smaller. In the smaller window, I could get the String that is typed and than the enter button is clicked.
The trouble is: How can I show the new items created by the "new project window" (the smaller window in the pic) in the TreeView in the main window(the bigger)?
The treeview is in the left side of the main window.
I hope I was clear.
Here is the code of the controllers of these windows:
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeItem.TreeModificationEvent;
import javafx.scene.control.TreeView;
import javafx.stage.Modality;
import javafx.stage.Stage;
/**
* this class handles with the main window of our LDF Tool
* #author Vinicius
* #version 1.0
*/
public class MainController implements Initializable{
#FXML
TreeView<String> treeView;
#FXML
MenuItem newProject;
private boolean flag = false;
private NewProjectWindowController npwc;
#Override
public void initialize(URL location, ResourceBundle resources) {
}
#FXML
public void newProjectClicked(ActionEvent event){
try{
flag = true;
FXMLLoader fxml = new FXMLLoader(getClass().getResource("newProjectWindow.fxml"));
Parent root = (Parent) fxml.load();
Stage newWindow = new Stage();
newWindow.setTitle("New Project");
newWindow.initModality(Modality.APPLICATION_MODAL);
newWindow.setScene(new Scene(root));
newWindow.show();
} catch (Exception e) {
System.out.println("caiu na exceção");
}
}
/**
* to this method, choose the project's name as argument, and it will be put on the
* tree with the archives that should be created together
* #param projectName
*/
public void doTree(String projectName){
TreeItem<String> root = new TreeItem<>("projectName");
root.setExpanded(true);
//TreeItem<String> folha1 = new TreeItem<String>(projectName + " arquivo 1");
//root.getChildren().add(folha1);
treeView.setRoot(root);
}
The other controller class:
package application;
import java.io.IOException;
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.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class NewProjectWindowController implements Initializable{
#Override
public void initialize(URL location, ResourceBundle resources) {
}
#FXML
Button cancelButton;
#FXML
Button enterButton;
#FXML
TextField textInput;
private String input;
public String getInput(){
return this.input;
}
#FXML
public void cancelButtonClicked(ActionEvent event) {
Stage window = (Stage) this.cancelButton.getParent().getScene().getWindow();
window.close();
}
#FXML
public void enterButtonClicked(ActionEvent event) {
input = hasString();
Stage window = (Stage) this.enterButton.getParent().getScene().getWindow();
window.close();
}
private String hasString(){
if (this.textInput.getText().isEmpty())
return null;
return this.textInput.getText();
}
}
Please, assume that I mapped everything ok in the FXML file.
thanks
#FXML
public void newProjectClicked(ActionEvent event){
try{
flag = true;
FXMLLoader fxml = new FXMLLoader(getClass().getResource("newProjectWindow.fxml"));
Parent root = (Parent) fxml.load();
Stage newWindow = new Stage();
newWindow.setTitle("New Project");
newWindow.initModality(Modality.APPLICATION_MODAL);
newWindow.setScene(new Scene(root));
// showAndWait blocks execution until the window closes:
newWindow.showAndWait();
NewProjectWindowController controller = fxml.getController();
String input = controller.getInput();
if (input != null) {
TreeItem<String> currentItem = treeView.getSelectionModel().getSelectedItem();
if (currentItem == null) currentItem = treeView.getRoot();
currentItem.getChildren().add(new TreeItem<>(input));
}
} catch (Exception e) {
System.out.println("caiu na exceção");
}
}

NullPointerException in changing javaFX scene

I have two scenes in my javaFx project .. the first one Language.fxml has a button which on click changes the scene to allDevices.fxml .. but it throws NullPointerException saying "Location is required" although both of the fxml files are in the same path !!
that's my LanguageController.java
package astrolabe;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
/**
*
* #author Ahmed Fawzy
*/
public class LanguageController implements Initializable {
#FXML
private Button arabic ;
#Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
arabic.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
// TODO Auto-generated method stub
try{
Node node=(Node) event.getSource();
Stage stage=(Stage) node.getScene().getWindow();
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("allDevices.fxml"));/* Exception */
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
});
}
The problem solved by adding the package name before the fxml file name !

Putting an object on a stage when mouse clicked

package javafxapplication36;
/**
* Copyright (c) 2008, 2012 Oracle and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*/
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.VPos;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Lighting;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.util.Duration;
/**
* A sample that demonstrates how to add or remove a change listener on a node
* (for example, a Rectangle node) for some property (for example,
* Rectangle.hover). Once you add a listener, the text field shows the hover
* property change.
*
* #see javafx.beans.value.ChangeListener
* #see javafx.beans.InvalidationListener
* #see javafx.beans.value.ObservableValue
*/
public class ChangeListenerSample extends Application {
public static void main(String[] args) {
launch(args);
}
private void init(Stage primaryStage) {
final Group root = new Group();
primaryStage.setResizable(false);
Scene scene = new Scene(root, 400,80);
primaryStage.setScene(scene);
//rect.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>()
scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override public void handle(MouseEvent event) {
Circle circle = new Circle(event.getSceneX(), event.getSceneY(),30);
circle.setFill(Color.YELLOW);
root.getChildren().add(circle);
}
});
//root.getChildren().add(circle);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}
I am trying to put an circle on stage when mouse is pressed, I am unable to make it happen. I tried setting the root at the beginning, but it doesn't happen.
Help appreciated !
I made just a few edits to your code and it seems to work perfectly !
Note : You need not set the root again inside the scene as you have already did it in the beginning !
Same goes for setting the scene to the Stage !
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
/**
* A sample that demonstrates how to add or remove a change listener on a node
* (for example, a Rectangle node) for some property (for example,
* Rectangle.hover). Once you add a listener, the text field shows the hover
* property change.
*
* #see javafx.beans.value.ChangeListener
* #see javafx.beans.InvalidationListener
* #see javafx.beans.value.ObservableValue
*/
public class ChangeListenerSample extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
final Group root = new Group();
primaryStage.setResizable(false);
Scene scene = new Scene(root, 400,80);
primaryStage.setScene(scene);
//rect.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>()
scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override public void handle(MouseEvent event) {
Circle circle = new Circle(event.getSceneX(), event.getSceneY(),30);
circle.setFill(Color.YELLOW);
root.getChildren().add(circle);
}
});
//root.getChildren().add(circle);
primaryStage.show();
}
}

Resources