朋友们,刚开始学习java,遇到了一个例程,在eclipse里调试的,不知道该怎么改,帮帮忙。
程序如下:
package jcomponent;//LabelDemo.java: Use label to display images
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;public class LabelDemo extends JFrame implements ActionListener
{
  // Declare an ImageIcon array. There are total 52 images
  private ImageIcon[] imageIcon = new ImageIcon[52];  // The current image index
  private int currentIndex = 0;  // Buttons for browsing images
  private JButton jbtPrior, jbtNext;  // Label for displaying images
  private JLabel jlblImageViewer = new JLabel();  final int TOTAL_NUMBER_OF_IMAGES = 52;  // Main Method
  public static void main(String[] args)
  {
    LabelDemo frame = new LabelDemo();
    // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.setVisible(true);
  }  // Default Constructor
  public LabelDemo()
  {
    setTitle("Label Demo");    // Load images into imageIcon array
    for (int i=1; i<=52; i++)
    {
      imageIcon[i-1] = new ImageIcon(
        this.getClass().getResource("images\\L1.gif"/*"images" + "/L" + i + ".gif"*/));
    }    // Show the first image
    jlblImageViewer.setIcon(imageIcon[currentIndex]);    // Set center alignment
    jlblImageViewer.setHorizontalAlignment(SwingConstants.CENTER);
    //jlblImageViewer.setVerticalAlignment(SwingConstants.CENTER);    // Panel jpButtons to hold two buttons for browsing images
    JPanel jpButtons = new JPanel();
    jpButtons.add(jbtPrior = new JButton());
    jbtPrior.setIcon(new ImageIcon(
      this.getClass().getResource("images/left.gif")));
    jpButtons.add(jbtNext = new JButton());
    jbtNext.setIcon(new ImageIcon(
      this.getClass().getResource("images/right.gif")));    // Add jpButton and the label to the frame
    getContentPane().add(jlblImageViewer, BorderLayout.CENTER);
    getContentPane().add(jpButtons, BorderLayout.SOUTH);    // Register listeners
    jbtPrior.addActionListener(this);
    jbtNext.addActionListener(this);
  }  // Handle ActionEvent from buttons
  public void actionPerformed(ActionEvent e)
  {
    if (e.getSource() == jbtPrior)
    {
      // Make sure index is nonnegative
      if (currentIndex == 0) currentIndex = TOTAL_NUMBER_OF_IMAGES;
      currentIndex = (currentIndex - 1)%TOTAL_NUMBER_OF_IMAGES;
      jlblImageViewer.setIcon(imageIcon[currentIndex]);
    }
    else if (e.getSource() == jbtNext)
    {
      currentIndex = (currentIndex + 1)%TOTAL_NUMBER_OF_IMAGES;
      jlblImageViewer.setIcon(imageIcon[currentIndex]);
    }
  }
}错误提示如下:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at jcomponent.LabelDemo.<init>(LabelDemo.java:43)
at jcomponent.LabelDemo.main(LabelDemo.java:28)
这个例外怎么处理,该怎么写?

解决方案 »

  1.   

    在你的类路径下有images\\L1.gif这个文件吗?
    可能是找不到这个文件images\\L1.gif
      

  2.   

    刚开始,我也是这么想的,所以才注释掉原来的代码,但是images文件什么的都在呀
      

  3.   

    你把ImageIcon后面括号里的东西删掉只留下imageIcon[i-1] = new ImageIcon( 
            "文件路径"); 试试看,我没有试,不知道对错~
      

  4.   

    可以的我试过。将所有
    this.getClass().getResource("images/right.gif");都直接改成“文件路径”,去掉this.getClass().getResource("")
      

  5.   

     //LabelDemo.java: Use label to display images 
    import java.awt.*; 
    import java.awt.event.*; 
    import javax.swing.*; 
    import java.lang.*; public class LabelDemo extends JFrame implements ActionListener 

      // Declare an ImageIcon array. There are total 52 images 
      private ImageIcon[] imageIcon = new ImageIcon[52];   // The current image index 
      private int currentIndex = 0;   // Buttons for browsing images 
      private JButton jbtPrior, jbtNext;   // Label for displaying images 
      private JLabel jlblImageViewer = new JLabel();   final int TOTAL_NUMBER_OF_IMAGES = 52;   // Main Method 
      public static void main(String[] args) 
      { 
        LabelDemo frame = new LabelDemo(); 
        // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setSize(500, 500); 
        frame.setVisible(true); 
      }   // Default Constructor 
      public LabelDemo() 
      { 
        setTitle("Label Demo");     // Load images into imageIcon array 
        for (int i=1; i <=52; i++) 
        { 
          imageIcon[i-1] = new ImageIcon( 
            "D:\\L1.gif"); 
        }     // Show the first image 
        jlblImageViewer.setIcon(imageIcon[currentIndex]);     // Set center alignment 
        jlblImageViewer.setHorizontalAlignment(SwingConstants.CENTER); 
        //jlblImageViewer.setVerticalAlignment(SwingConstants.CENTER);     // Panel jpButtons to hold two buttons for browsing images 
        JPanel jpButtons = new JPanel(); 
        jpButtons.add(jbtPrior = new JButton()); 
        jbtPrior.setIcon(new ImageIcon( 
          "D:\\L1.gif")); 
        jpButtons.add(jbtNext = new JButton()); 
        jbtNext.setIcon(new ImageIcon( 
          "D:\\L1.gif"));     // Add jpButton and the label to the frame 
        getContentPane().add(jlblImageViewer, BorderLayout.CENTER); 
        getContentPane().add(jpButtons, BorderLayout.SOUTH);     // Register listeners 
        jbtPrior.addActionListener(this); 
        jbtNext.addActionListener(this); 
      }   // Handle ActionEvent from buttons 
      public void actionPerformed(ActionEvent e) 
      { 
        if (e.getSource() == jbtPrior) 
        { 
          // Make sure index is nonnegative 
          if (currentIndex == 0) currentIndex = TOTAL_NUMBER_OF_IMAGES; 
          currentIndex = (currentIndex - 1)%TOTAL_NUMBER_OF_IMAGES; 
          jlblImageViewer.setIcon(imageIcon[currentIndex]); 
        } 
        else if (e.getSource() == jbtNext) 
        { 
          currentIndex = (currentIndex + 1)%TOTAL_NUMBER_OF_IMAGES; 
          jlblImageViewer.setIcon(imageIcon[currentIndex]); 
        } 
      } 

    你只要确保D盘下有个L1.gif的图片就好了D:\\L1.gif
      

  6.   

    //LabelDemo.java: Use label to display images 
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.SwingConstants;@SuppressWarnings("serial")
    public class LabelDemo extends JFrame implements ActionListener {

    // Declare an ImageIcon array. There are total 52 images
    private ImageIcon[] imageIcon = new ImageIcon[52]; // The current image index
    private int currentIndex = 0; // Buttons for browsing images
    private JButton jbtPrior, jbtNext; // Label for displaying images
    private JLabel jlblImageViewer = new JLabel(); final int TOTAL_NUMBER_OF_IMAGES = 52; // Main Method
    public static void main(String[] args) {
    LabelDemo frame = new LabelDemo();

    // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.setVisible(true);
    } // Default Constructor
    public LabelDemo() {
    setTitle("Label Demo"); // Load images into imageIcon array
    for (int i = 1; i <= 52; i++) {
    imageIcon[i - 1] = new ImageIcon("D:\\L1.gif");
    } // Show the first image
    jlblImageViewer.setIcon(imageIcon[currentIndex]); // Set center alignment
    jlblImageViewer.setHorizontalAlignment(SwingConstants.CENTER);
    // jlblImageViewer.setVerticalAlignment(SwingConstants.CENTER); // Panel jpButtons to hold two buttons for browsing images
    JPanel jpButtons = new JPanel();
    jpButtons.add(jbtPrior = new JButton());
    jbtPrior.setIcon(new ImageIcon("D:\\L1.gif"));
    jpButtons.add(jbtNext = new JButton());
    jbtNext.setIcon(new ImageIcon("D:\\L1.gif")); // Add jpButton and the label to the frame
    getContentPane().add(jlblImageViewer, BorderLayout.CENTER);
    getContentPane().add(jpButtons, BorderLayout.SOUTH); // Register listeners
    jbtPrior.addActionListener(this);
    jbtNext.addActionListener(this);
    } // Handle ActionEvent from buttons
    public void actionPerformed(ActionEvent e) {
    if (e.getSource() == jbtPrior) {
    // Make sure index is nonnegative
    if (currentIndex == 0)
    currentIndex = TOTAL_NUMBER_OF_IMAGES;
    currentIndex = (currentIndex - 1) % TOTAL_NUMBER_OF_IMAGES;
    jlblImageViewer.setIcon(imageIcon[currentIndex]);
    } else if (e.getSource() == jbtNext) {
    currentIndex = (currentIndex + 1) % TOTAL_NUMBER_OF_IMAGES;
    jlblImageViewer.setIcon(imageIcon[currentIndex]);
    }
    }
    }