0

I need to write a Java program that cycles between 10 different images infinitely using a PREV and NEXT JButton. There is also a quit JButton to exit the program. The image needs to be displayed in the center screen. Each image is a .jpg and is stored in a folder called "images". The file name for each of the images is stored in an array. The pictures have file names accociated to their index #s in the array(0-9).(ex. 0.jpg, 1.jpg, etc...) my code compiles with no error, but none of my JButtons appear on the JFrame nor does the image. While running the application the command prompt returns this:

C:\Users\Mikey\Desktop>java test
0. images/0.jpg
Exception in thread "main" java.lang.NullPointerException
        at javax.swing.ImageIcon.<init>(Unknown Source)
        at test.loadIcons(test.java:31)
        at test.<init>(test.java:47)
        at test.main(test.java:39)

Here is my code:

import java.awt.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import java.net.URL;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.event.*;

public class test extends JFrame implements ActionListener {

private ImageIcon icon;
ImageIcon[] Pictures = new ImageIcon[10]; //array for file names

int clicks = 0; //increment by button clicks
JLabel picHolder = null;

public void loadIcons() { //begin load icons
    int i;
    for (i = 0; i < 10; i++) { //begin for loop
        String nm = String.format("images/%d.jpg",i);
        System.out.printf("%d. %s\n",i,nm);
        URL imageURL = getClass().getClassLoader().getResource(nm);
        icon = new ImageIcon(imageURL);
        Image o = icon.getImage();
        System.out.printf("%s\n",o.toString());
        Pictures[i] = icon;
    } //end for loop
} //end load icons

public static void main(String[] n) {
    test o = new test();
}
public test() {
    setSize(800,500);
    setVisible(true);
    setLocationRelativeTo(null);
    setTitle("Mikey's Slideshow");

    loadIcons(); //loods file names into array

    JButton prev = new JButton("PREV");
    JButton next = new JButton("NEXT");
    JButton quit = new JButton("QUIT");

    JPanel p_middle = new JPanel();
        picHolder = new JLabel((Icon)Pictures[0]);
        p_middle.add(picHolder);
        p_middle.setPreferredSize(new Dimension(400,200));

    JPanel p_bottom = new JPanel();

    JLabel l1 = new JLabel("Go Broncos!");

    p_bottom.setLayout(new BorderLayout());
    p_bottom.add(quit,BorderLayout.SOUTH);
    p_bottom.add(l1,BorderLayout.NORTH);
    p_bottom.add(prev,BorderLayout.LINE_START);
    p_bottom.add(next,BorderLayout.LINE_END);

    quit.addActionListener(this);
    prev.addActionListener(this);
    next.addActionListener(this);

    add(p_middle,BorderLayout.CENTER);
    add(p_bottom,BorderLayout.SOUTH);
    add(l1,BorderLayout.NORTH);
}

public void updatePic(int e) { //begin update pic
        System.out.printf("%s\n",Pictures[e]);
        picHolder.setIcon((Icon)Pictures[e]);
} //end update pic

public void actionPerformed(ActionEvent e) {
    String a = e.getActionCommand();
    if (a.equals("QUIT")) {System.exit(0);} //system exits 
    if (a.equals("NEXT")) {clicks++; if (clicks>9) {clicks=0;} updatePic(clicks);} //next picture
    if (a.equals("PREV")) {clicks--; if (clicks<0) {clicks=9;} updatePic(clicks);} //previous picture
}
} //end class 

Thank you in advance!

MIKEY
  • 1
  • 1
  • 3
  • 1
    Do you know how your classpath is being set? The expression `getClass().getClassLoader().getResource(nm)` is returning null, which means that the image files are not being found. Them not being on your classpath would explain how they're not being found. – Ben M. Sep 12 '15 at 02:48
  • Just to build on Ben's response, you'd want the image files to actually sit within a Java package on your project if you'd want to access it in this manner. Instead of reading from a specific file location on the computer, you look for one that's local to the project. This is very useful as it allows you to carry all of your dependencies around with the code. – Mapsy Sep 12 '15 at 02:56
  • Sorry I'm a little new to Java. I have set my path, but not my class path. How do i change my class path? – MIKEY Sep 12 '15 at 03:17

1 Answers1

0

It is pretty clear from the stacktrace that the problem is in these two lines:

    URL imageURL = getClass().getClassLoader().getResource(nm);
    icon = new ImageIcon(imageURL);

First, the stacktrace implies that imageURL is null. (We can't see the Java library line number to be absolutely sure, but there is no other plausible explanation.)

So how can that be? Well if you look at the javadocs for getResource you will see that it returns a null if it cannot find the resource. Hence ... the root cause of your problem is that (at least) one of the "/images/.jpg" images:

  • is missing from the JARs etc on the classpath, or
  • has a different resource pathname.

(It is also possible that the resource exists on the application's classpath, but not in the classpath of the classloader that loaded this class. However, that scenario has some complicated preconditions.)

See also: https://stackoverflow.com/a/24347569/139985

Community
  • 1
  • 1
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I'm almost positive it has the correct resource path name, unless I don't understand how the resource path name works. How do I check the class path? Would this explain why my other three JButtons aren't appearing? – MIKEY Sep 12 '15 at 03:27