0

please help me , when I run my program in the browser it return this error :

java.lang.NullPointerException
    at e1.E1.start(E1.java:30)
    at com.sun.javafx.applet.FXApplet2$2.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown....

The error occurs on this line:

Image img = new Image(getClass().getResource("u.png").toExternalForm());

This is the complete code:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;


public class E1 extends Application {

    @Override
    public void start(Stage primaryStage) {
        TextField UserF = new TextField();
        UserF.setPromptText("nom d'utilisateur");
        UserF.setLayoutX(50);
        Image img = new Image(getClass().getResource("u.png").toExternalForm()); // error occurs here
       ImageView pic = new ImageView(img);
        pic.setLayoutX(-100);
        pic.setLayoutY(-60);
        PasswordField  PassF = new PasswordField ();
        PassF.setPromptText(" mot de passe");
        PassF.setLayoutX(50);
        PassF.setLayoutY(50);

        Button login = new Button("Login");
        login.setLayoutX(100);
        login.setLayoutY(100);
       // btn.setMaxSize(100, 200);

       BorderPane Menu= new BorderPane(); 
       Group root = new Group();

       root.getChildren().addAll(UserF,PassF,login,pic);
       Menu.setCenter(root);
       Menu.setStyle("-fx-background-color: linear-gradient(from 25% 25% to 100% 100%,  #6db3f2 , #1e69de)");

        Scene scene;
        scene= new Scene(Menu);


    //    primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}
fabian
  • 80,457
  • 12
  • 86
  • 114
Os M Torvalds
  • 21
  • 1
  • 2
  • 5

1 Answers1

0

Works now , it solved by :

replacing

Image img = new Image(getClass().getResource("u.png").toExternalForm());

by :

Image img = new Image(getClass().getResourceAsStream("/icons/u.png"));
nacho4d
  • 43,720
  • 45
  • 157
  • 240
Os M Torvalds
  • 21
  • 1
  • 2
  • 5
  • Do not post additional information as answer, if it doesn't **answer** the question. [Edit your question](http://stackoverflow.com/posts/34380067/edit) instead. – fabian Dec 20 '15 at 13:17