2013-07-28 108 views
0

這個小程序是一個簡單的關於我爲一個類項目創建的網站中的開發者頁面。我正在嘗試爲每個不同的JButton顯示圖像和生物。 我在與編譯的問題,我一直在這條線嘗試顯示圖像時出現NullPointerException?

danPic = new ImageIcon(getClass().getResource("pics/danSkaggs.jpg"));

這我假設它會null,因爲根據該目錄也找不到圖像獲得NullPointerException錯誤 我給它。然而,我不明白我可以做什麼不同,我看不出如何編寫目錄的任何問題。該目錄是pics/filename.jpg,並且該文件夾與Java代碼位於同一個包中。

這裏是完整的源代碼。

import java.util.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 

public class Developers extends JApplet{ 
private JButton danButton = new JButton("Dan Skaggs"); 
private JButton brandonButton = new JButton("Brandon Shaw"); 
private JButton jamesButton = new JButton("James Simpson"); 
private JLabel bioLabel = new JLabel("Please click one of the buttons on the left."); 
JPanel centerPanel = new JPanel(new GridLayout(1, 2)); 
JPanel mainPanel = new JPanel(new BorderLayout()); 
JPanel westPanel = new JPanel(new GridLayout(1, 3)); 
private ImageIcon danPic; 
private ImageIcon brandonPic; 
private ImageIcon jamesPic; 
private JLabel dLabel; 
private JLabel bLabel; 
private JLabel sLabel; 



//This array carries the Bios of the group project members 
String[] bio = new String[]{"Insert Bio", 
    "Insert Bio", 
    "Insert Bio"}; 


public Developers(){ 
    mainPanel.add(westPanel, BorderLayout.WEST); 
    westPanel.add(danButton); 
    westPanel.add(brandonButton); 
    westPanel.add(jamesButton); 


    centerPanel.add(bioLabel); 
    mainPanel.add(centerPanel, BorderLayout.CENTER); 
    danButton.addActionListener(new Handler()); 
    brandonButton.addActionListener(new Handler()); 
    jamesButton.addActionListener(new Handler()); 

    danPic = new ImageIcon(getClass().getResource("pics/danSkaggs.jpg")); 
    brandonPic = new ImageIcon(getClass().getResource("pics/brandonShaw.jpg")); 
    jamesPic = new ImageIcon(getClass().getResource("pics/jamesSimpson.jpg")); 

    dLabel = new JLabel (danPic); 
    bLabel = new JLabel (brandonPic); 
    sLabel = new JLabel (jamesPic); 
    centerPanel.add(dLabel); 
    add(mainPanel); 
} 


private class Handler implements ActionListener{ 
    public void actionPerformed(ActionEvent event){ 
     if(event.getSource()== danButton){ 
      bioLabel.setText(bio[0]); 
      centerPanel.add(dLabel); 
     } 
     else if(event.getSource()== brandonButton){ 
      bioLabel.setText(bio[1]); 
      centerPanel.add(bLabel); 
     } 
     else if(event.getSource()== jamesButton){ 
      bioLabel.setText(bio[2]);  
      centerPanel.add(bLabel); 
     } 
    } 
}//end Handler class 
}//end Developer class 

回答

0

所以getResource正在恢復null:你的圖片路徑是錯誤的。請注意,這些是相對路徑(相對於您用getClass獲取的類),因此您可能會丟失使其成爲絕對路徑的初始斜槓。

0

這應該是:

danPic = new ImageIcon(getClass().getClassLoader().getResourceAsStream("danSkaggs.jpg"));

這將從類路徑假設pics目錄是類路徑上加載圖像。

0

getClass().getResource("pics/danSkaggs.jpg")是相對路徑。如果你的類是在一個包com.mydomain它希望找到圖像中/com/mydomain/pics/danSkaggs.jpg

如果類和圖像都在同一個包只需使用getResource("danSkaggs.jpg")。有關更多詳細信息,請檢查this comment示例代碼,演示如何使用類加載資源,或者更好地使用類加載器。

1

改爲使用ImageIO,它會給exception您可以捕獲並相應地進行處理。

或者

danPic = new ImageIcon(getClass().getResource("pics/danSkaggs.jpg")); 

你缺少/圖片是包的名稱,必須使用/使url其他明智圖片將嵌入到父目錄名稱以使mistake.So使用(差路)

danPic = new ImageIcon(getClass().getResource("/pics/danSkaggs.jpg")); 

Exception是第一個,因爲它首先執行更改其他兩個語句正在獲取圖像。

我剛測試過它工作正常。

相關問題