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!