Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: java Null Pointer Exception for choose file

  1. #1

    Default java Null Pointer Exception for choose file

    Tips about getting error java.lang.NullPointerException at NewJFrame.jButton4ActionPerformed(NewJFrame.java:2 66) when executing code

    i think problem is from VBox or openwindow.getScene(). i use a button for brows and select a picture but when push that button openwindow.getScene() Returns the null value i see this error java.lang.NullPointerException at NewJFrame.jButton4ActionPerformed(NewJFrame.java:2 66)
    i use java swing in gui add a jframe and in jframe add a button

    FileChooser filechooser = new FileChooser();
    public void initialize(URL location, ResourceBundle resourcr) {
        filechooser.setInitialDirectory(new File("user.home"));
    }
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        VBox openwindow = new VBox();
     
        System.out.println(openwindow);
     
            Window stage = openwindow.getScene().getWindow();
            filechooser.setTitle("Load Dialog");
            filechooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("Picture File", "*.jpg"),
                    new FileChooser.ExtensionFilter("Picture File", "*.png"),
                    new FileChooser.ExtensionFilter("Picture File", "*.bmp"));
            try {
                File file = filechooser.showOpenDialog(stage);
                filechooser.setInitialDirectory(file.getParentFile());
            } catch (Exception ex) {
     
            }
     
     
    }
    Last edited by cnmeysam; March 21st, 2021 at 06:31 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java Null Pointer Exception for choose file

    i see this error
    What statement was the exception on?
    What variable has the null value?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (March 15th, 2021)

  4. #3

    Default Re: java Null Pointer Exception for choose file

    i think now you can help me

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java Null Pointer Exception for choose file

    I need this information to be able to give you any help:
    What statement was the exception on?
    What variable has the null value?

    What does the API doc say about the values that VBox getScene method will return?

    What package is the VBox class in? (There are no import statements in the posted code)
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (March 15th, 2021)

  7. #5

    Default Re: java Null Pointer Exception for choose file

    import java.io.File;
    import java.net.URL;
    import java.util.ResourceBundle;
    import static javafx.scene.input.DataFormat.URL;
    import javafx.scene.layout.VBox;
    import javafx.stage.FileChooser;
    import javafx.stage.Window;

    VBox openwindow = new VBox();
     
        System.out.println("openwindow = "+openwindow);
        System.out.println("openwindow.getScene() = "+openwindow.getScene());

    openwindow = VBox@601798d5
    openwindow.getScene() = null

    What variable has the null value? openwindow.getScene() Returns the null value

    What package is the VBox class in? (There are no import statements in the posted code) i use java swing in gui add a jframe and in jframe add a button and that is all my code and I am a beginner and I do not know what to add

    this is my project source
    http://uploadb.me/direct/i52dv4bdj6zl/openfile.rar.html

    i need create in java swing openfile dialog for select a picture like this

    https://docs.oracle.com/javafx/2/ui_controls/img/file-chooser-sample.png
    Last edited by cnmeysam; March 15th, 2021 at 08:57 AM.

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java Null Pointer Exception for choose file

    What does the API doc say about the values that VBox getScene method will return?
    When does it return a null value?

    Here is a link to a JavaFX API doc site: https://docs.oracle.com/javase/8/javafx/api/
    If you don't understand my answer, don't ignore it, ask a question.

  9. #7

    Default Re: java Null Pointer Exception for choose file

    after click on my button i see this errors
    Window stage = openwindow.getScene().getWindow();
    return null for mee
     
    run:
    openwindow = VBox@3ab692e9
    openwindow.getScene() = null
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at NewJFrame1.jButton1ActionPerformed(NewJFrame1.java:82)
    	at NewJFrame1.access$000(NewJFrame1.java:20)
    	at NewJFrame1$1.actionPerformed(NewJFrame1.java:45)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    	at java.awt.Component.processMouseEvent(Component.java:6533)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    	at java.awt.Component.processEvent(Component.java:6298)
    	at java.awt.Container.processEvent(Container.java:2236)
    	at java.awt.Component.dispatchEventImpl(Component.java:4889)
    	at java.awt.Container.dispatchEventImpl(Container.java:2294)
    	at java.awt.Component.dispatchEvent(Component.java:4711)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    	at java.awt.Container.dispatchEventImpl(Container.java:2280)
    	at java.awt.Window.dispatchEventImpl(Window.java:2746)
    	at java.awt.Component.dispatchEvent(Component.java:4711)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    	at java.awt.EventQueue.access$500(EventQueue.java:97)
    	at java.awt.EventQueue$3.run(EventQueue.java:709)
    	at java.awt.EventQueue$3.run(EventQueue.java:703)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    	at java.awt.EventQueue$4.run(EventQueue.java:731)
    	at java.awt.EventQueue$4.run(EventQueue.java:729)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
    Last edited by cnmeysam; March 15th, 2021 at 09:44 AM.

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java Null Pointer Exception for choose file

    Did you read the API doc for the getScene method? What did it say about returning a null value?
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (March 15th, 2021)

  12. #9

    Default Re: java Null Pointer Exception for choose file

    i cant find getScene return null in API doc
    can i use javafx FileChooser and stage in java swing?
    i don't now how i must create open file dialog for select a picture like this form https://docs.oracle.com/javafx/2/ui_...ser-sample.png
    in the java swing. in java swing file dialog i can't see what picture i choose i just can see picture names or set picture on a label and see in label pictures one by one after click any picture
    Last edited by cnmeysam; March 15th, 2021 at 09:55 AM.

  13. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java Null Pointer Exception for choose file

    i cant find getScene return null in API doc
    Please copy the contents of the API doc for the getScene method and paste it here so I can see what you are looking at.

    can i use javafx FileChooser and stage in java swing?
    I don't think it is a good idea to mix JavaFX code with Swing code.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #11

    Default Re: java Null Pointer Exception for choose file

    i first post i write all code in my project don't have any code to paste in here
    I even put a sample of the project source for download
    Download File UploadBoy.com Making Your File Sharing Easy!
    All project codes are available in the first post

  15. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java Null Pointer Exception for choose file

    Did you read the API doc for the getScene method? What did it say?

    Don't post links to other sites for code. If you have code that compiles and executes for testing please paste here on the forum.
    If you don't understand my answer, don't ignore it, ask a question.

  16. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (March 15th, 2021)

  17. #13

    Default Re: java Null Pointer Exception for choose file

    Did you read the API doc for the getScene method? What did it say? https://docs.oracle.com/javase/8/jav...tml#getScene--

    i write all my code here in first post you can copy and compile and test it

    solution1= https://docs.oracle.com/javafx/2/ui_...le-chooser.htm
    solution2=http://www.java2s.com/Tutorials/Java/JavaFX/0555__JavaFX_FileChooser.htm
    solution3=http://javatutorialsx.blogspot.com/2013/01/use-javafx-filechooser-to-open-image.html

    this is for open file dialog and file chooser in javafx
    i need use one of them in java swing gui in my button
    i tested all of this and all it work in class and javafx but i cant use in java swing gui form

    --- Update ---

     
    import java.awt.Desktop;
    import java.io.File;
    import java.io.IOException;
    import java.util.List;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.geometry.Insets;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.GridPane;
    import javafx.scene.layout.Pane;
    import javafx.scene.layout.VBox;
    import javafx.stage.FileChooser;
    import javafx.stage.Stage;
     
    public final class FileChooserSample extends Application {
     
        private Desktop desktop = Desktop.getDesktop();
     
        @Override
        public void start(final Stage stage) {
            stage.setTitle("File Chooser Sample");
     
            final FileChooser fileChooser = new FileChooser();
            final Button openButton = new Button("Open a Picture...");
            final Button openMultipleButton = new Button("Open Pictures...");     
     
            openButton.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(final ActionEvent e) {
                        configureFileChooser(fileChooser);
                        File file = fileChooser.showOpenDialog(stage);
                        if (file != null) {
                            openFile(file);
                        }
                    }
                });
     
            openMultipleButton.setOnAction(
                new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(final ActionEvent e) {
                        configureFileChooser(fileChooser);                               
                        List<File> list = 
                            fileChooser.showOpenMultipleDialog(stage);
                        if (list != null) {
                            for (File file : list) {
                                openFile(file);
                                System.out.println(file);
                            }
                        }
                    }
                });
     
     
            final GridPane inputGridPane = new GridPane();
     
            GridPane.setConstraints(openButton, 0, 1);
            GridPane.setConstraints(openMultipleButton, 1, 1);
            inputGridPane.setHgap(6);
            inputGridPane.setVgap(6);
            inputGridPane.getChildren().addAll(openButton, openMultipleButton);
     
            final Pane rootGroup = new VBox(12);
            rootGroup.getChildren().addAll(inputGridPane);
            rootGroup.setPadding(new Insets(12, 12, 12, 12));
     
            stage.setScene(new Scene(rootGroup));
            stage.show();
        }
     
        public static void main(String[] args) {
            Application.launch(args);
        }
     
        private static void configureFileChooser(final FileChooser fileChooser) {      
                fileChooser.setTitle("View Pictures");
                fileChooser.setInitialDirectory(new File(System.getProperty("user.home")));                 
                fileChooser.getExtensionFilters().addAll(
                    new FileChooser.ExtensionFilter("All Images", "*.*"),
                    new FileChooser.ExtensionFilter("JPG", "*.jpg"),
                    new FileChooser.ExtensionFilter("PNG", "*.png")
                );
        }
     
        private void openFile(File file) {
            try {
                desktop.open(file);
            } catch (IOException ex) {
                Logger.getLogger(FileChooserSample.class.getName()).log(
                    Level.SEVERE, null, ex
                );
            }
        }

     
     
    import java.io.File;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.VBox;
    import javafx.stage.FileChooser;
    import javafx.stage.Stage;
     
    public class FileChooserExample extends Application {
     
        public static void main(String[] args) {
            launch(args);
        }
     
        @Override
        public void start(Stage primaryStage) {
            primaryStage.setTitle("JavaFX App");
     
            FileChooser fileChooser = new FileChooser();
     
            fileChooser.getExtensionFilters().addAll(
                    new FileChooser.ExtensionFilter("JPG Files", "*.jpg"),
                    new FileChooser.ExtensionFilter("PNG Files", "*.png")
            );
            Button button = new Button("Select File");
            button.setOnAction(e -> {
                File selectedFile = fileChooser.showOpenDialog(primaryStage);
                System.out.println(selectedFile);
            });
     
            VBox vBox = new VBox(button);
            Scene scene = new Scene(vBox, 960, 600);
     
            primaryStage.setScene(scene);
            primaryStage.show();
        }

    and in this method i cant say just open image file(it is old version of file dialog in java swing )
    jLabel2.setText("");
     
            Frame frame = null;
            FileDialog fd = new FileDialog(frame, "Select Cover Photo");
     
            fd.setIconImage(getIcon());
     
     
     
            fd.setAlwaysOnTop(true);
            fd.setVisible(true);
            if (fd.getFile() != null) {
                String path = new File(fd.getDirectory(), fd.getFile()).getAbsolutePath();
     
                if (new File(path).length() / 1024 > 1536) {
                    JOptionPane.showMessageDialog(null, "picture size can't mor than 1.5 MG");
                } 
                else {
                    System.out.println(path);
                    jLabel2.setText(path);
                    fd.dispose();
                    System.out.println("size= " + new File(path).length() / 1024 + " KB");
                }
            } else {
                System.out.println("No Data");
                fd.dispose();
            }

  18. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java Null Pointer Exception for choose file

    Which code are you having the problems with? You posted 3 separate sections of code.
    If you don't understand my answer, don't ignore it, ask a question.

  19. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (March 16th, 2021)

  20. #15

    Default Re: java Null Pointer Exception for choose file

    if you can add file type in codes and user just can see image files and select just image files in last code. my problem is solves and if you cant! don't different you can Each of the first and second codes that you can use in Java Swing solves my problem
    Last edited by cnmeysam; March 15th, 2021 at 08:47 PM.

  21. #16

    Default Re: java Null Pointer Exception for choose file

    finally i found it i I hope it helps you too
    first use this librarys
    import com.sun.javafx.application.PlatformImpl;
    import java.io.File;
    import javafx.stage.FileChooser;

    and write this codes for your button
    FileChooser fileChooser = new FileChooser();
                fileChooser.setTitle("My File Chooser");
                fileChooser.getExtensionFilters().addAll(
                        new FileChooser.ExtensionFilter("JPG Files", "*.jpg"),
                        new FileChooser.ExtensionFilter("PNG Files", "*.png"));
                 File selectedFile = fileChooser.showOpenDialog(null);
                if (selectedFile != null) {
                    if (selectedFile.exists()) {
                        System.out.println(selectedFile.getPath());
                    }
                } else {
                    System.out.println("No Data");
                }
            });
    Last edited by cnmeysam; March 18th, 2021 at 03:25 AM.

  22. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: java Null Pointer Exception for choose file

    Thank you for sharing the solution.
    If you don't understand my answer, don't ignore it, ask a question.

  23. The Following User Says Thank You to Norm For This Useful Post:

    cnmeysam (March 21st, 2021)

Similar Threads

  1. [SOLVED] Null pointer exception when attempting to load file from inside jar file
    By teslaa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 13th, 2018, 06:47 PM
  2. null pointer exception, please help!
    By tkocher13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 11th, 2014, 10:11 AM
  3. null pointer exception
    By Ramzi89 in forum Object Oriented Programming
    Replies: 1
    Last Post: August 15th, 2012, 01:57 PM
  4. Java Null Pointer Exception
    By destructobob in forum Exceptions
    Replies: 5
    Last Post: December 9th, 2011, 12:53 PM
  5. Null pointer exception
    By Wrathgarr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2010, 12:48 AM