0

I'm trying to create a small program that read images from a file. I'm getting at least 2 errors pointed on the line 25 and 41, would you help me to fix these errors please? Thank You

public RoadSafetyAuthority() {
    initComponents();

    //dispaly fisrt image
    showImage(pos); // Line 25
}

int pos = 0;

 public String[] getImages() {

    File file = new File(getClass().getResource("/Images/Information Signs").getFile());
    String[] imagesList = file.list();
    return imagesList;
}   

    //display   the image by index
    public final void showImage(int index) {

        String[] imagesList = getImages();
        String imageName =  imagesList[index]; // Line 41
        ImageIcon icon = new 
           ImageIcon(getClass().getResource("/Images/Information Signs" + imageName));
        Image image = 
icon.getImage().getScaledInstance(jImageLabel.getWidth(),jImageLabel.getHeight(), Image.SCALE_SMOOTH);

    }


private void jBtnNextActionPerformed(java.awt.event.ActionEvent evt) {                                         

    pos = pos - 1;

    if(pos >= getImages().length) {
        pos = getImages().length - 1;
    }

    showImage(pos);
}                                        

private void jBtnPreviousActionPerformed(java.awt.event.ActionEvent evt) {                                             
    pos = pos -1;

    if(pos < 0) {
        pos = 0;
    }

    showImage(pos);
}               

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at assignment1.RoadSafetyAuthority.showImage(RoadSafetyAuthority.java:41)
at assignment1.RoadSafetyAuthority.<init>(RoadSafetyAuthority.java:25)
at assignment1.RoadSafetyAuthority$3.run(RoadSafetyAuthority.java:157)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 2) *"I'm trying to create a small program that read images from a file."* It seems the 'file' would be a JAR and from that point on, we are dealing with images that are an embedded resource. At that time it's all about `URL` objects and not `File` objects, which means the strategy seen in the above code (e.g. using `File.list()`) will not work as expected. – Andrew Thompson Nov 25 '18 at 06:55
  • BTW - as an experiment (some basic debugging), change `File file = new File(getClass().getResource("/Images/Information Signs").getFile()); ..` to add `.. System.out.println("file exists: " + file.exists());` directly after it. What is the output? – Andrew Thompson Nov 25 '18 at 07:01
  • Caution: The getFile() method of URL *does not* return a valid file name. It just returns the path portion of a URL. Also, resources are not directories and cannot be listed like directories. Consider including a test file that explicitly lists each resource instead. – VGR Nov 25 '18 at 07:06
  • Once I've change this statement File(getClass().getResource("/Images/Information Signs").getFile()); to System.out.println("file exists: " + file.exists()); I've got an error suggesting to create a class "file" or add return value to New variable. – Cesarini013 Nov 25 '18 at 18:25
  • So, instead I've added that "sout" the output is "file exists: false" – Cesarini013 Nov 25 '18 at 18:33
  • It's not a JAR File, its a file objects. – Cesarini013 Nov 25 '18 at 22:59
  • At this stage I got rid of the errors, the JFrame pops up, though the image is still not being displayed on the JLabel. Any thoughts? – Cesarini013 Nov 25 '18 at 23:03
  • 1
    *"Once I've change this statement.."* Tip: Add @VGR (or whoever, the `@` is important) to *notify* the person of a new comment. BTW - I did not suggest to change / remove anything, but just to **add** `System.out.println("file exists: " + file.exists());` **after** creating the file. You should also try to understand the code being written. Consult the JavaDocs for the methods & classes being mentioned. If you do not understand something in the docs, ask. We can explain, but we're not equipped to teach you to check the documentation, or on the very basics that can be picked up from a Java 101. – Andrew Thompson Nov 28 '18 at 03:57

1 Answers1

1

The only thing that can be null at line 41 is an array imagesList.
It means that getImages() at line 40 returned null.
getImages() returns the result from file.list().
Searching for the documentation:

Returns: An array of strings naming the files and directories in the directory denoted by this abstract pathname. The array will be empty if the directory is empty. Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

uli
  • 671
  • 6
  • 15