0

このアプレットは、クラス プロジェクト用に作成している Web サイトの開発者ページに関する単純なものです。異なる JButton ごとに画像と略歴を表示しようとしています。コンパイルに問題があります。この行で NullPointerException エラーが発生し続けます

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

指定したディレクトリに基づいて画像が見つからないため、nullになると想定しています。しかし、何が違うのか理解できません。ディレクトリの書き方に問題はありません。ディレクトリは 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
4

4 に答える 4

1

代わりにImageIOを使用すると、exceptionキャッチしてそれに応じて処理できるようになります。

または

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

pics is パッケージ名がありません。他の賢明な写真を作成するために使用する必要があります/。間違いを犯すために親ディレクトリ名に埋め込まれます。したがって、(悪い方法)を使用します。/url

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

これExceptionは最初に実行されるため、画像を取得する他の 2 つのステートメントも変更されます。

正常に動作することをテストしました。

于 2013-07-28T12:15:04.743 に答える
0

これは次のようになります。

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

picsこれにより、ディレクトリがクラスパス上にあると仮定して、クラスパスからイメージがロードされます。

于 2013-07-28T11:59:59.547 に答える
0

getClass().getResource("pics/danSkaggs.jpg")相対パスです。あなたのクラスがパッケージにある場合、com.mydomainそれはイメージを見つけることを期待しています/com/mydomain/pics/danSkaggs.jpg

クラスとイメージが同じパッケージにある場合は、getResource("danSkaggs.jpg"). 詳細については、クラスまたはそのクラスローダーを使用してリソースをロードする方法を示すこのコメントサンプル コードを確認してください。

于 2013-07-28T12:01:10.773 に答える
0

だからgetResource戻ってきnullます:あなたの写真へのパスは間違っています。これらは相対パス( でフェッチしたクラスに相対getClass) であるため、絶対パスになる最初のスラッシュが欠落している可能性があることに注意してください。

于 2013-07-28T11:57:52.950 に答える