0

フォルダからアイコン画像を抽出しようとしてarraylistいますが、NullPointerExceptionが発生し続けます。小さいバージョンはすでに抽出できますが、小さすぎます。私が取得しようとしているアイコンは、通常のサイズのアイコンです。filePathsアイコンの場所のリストを保持します。エラーが指しているiconBIG.add(...)場所です。NullPointerException

// Global
private ArrayList<Icon> iconBIG = new ArrayList<Icon>();

// Within extractIcon()...
for (String target : filePaths)
    {
        try 
        {
            ShellFolder shell = ShellFolder.getShellFolder(new File(target));
            iconBIG.add(new ImageIcon(shell.getIcon(true)));    
        } 
        catch (FileNotFoundException e) 
        {
            e.printStackTrace();
        }
    }

編集:私はすでにShellFolderを使用するための完全な許可を持っています。

更新:それが言うところnew File(target)(これはアプリケーションの完全なパスのみを保持します)、私が変更した場合

getShellFolder(new File(target)

getShellFolder(new File(C:/foo/bar.lnk)

コードは機能します。すべてを「/」に置き換えることはすでに考えていましたが\、それでも同じエラーが発生する理由がわかりません。

4

1 に答える 1

0

このプライベートパッケージの実行を許可したことを考えると、正常に機能しているようです。JDK6で動作する例を次に示します。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import javax.swing.*;
import sun.awt.shell.ShellFolder;

public class ShowShellIcon {
    private static void createAndShowUI() {
        final JFrame frame = new JFrame("Load Image");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton loadButton = new JButton("Display Image");
        loadButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fc = new JFileChooser(
                        System.getProperty("user.home"));
                if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
                    try {
                        ShellFolder shell = ShellFolder.getShellFolder(fc
                                .getSelectedFile());
                        JOptionPane.showMessageDialog(null,
                                new ImageIcon(shell.getIcon(true)));
                    } catch (FileNotFoundException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });

        frame.add(loadButton);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(
                            UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                    ex.printStackTrace();
                } 
                createAndShowUI();
            }
        });
    }
}
于 2013-03-27T05:17:03.220 に答える