尝试在java中打开图像文件时获取NullPointerException

尝试在java中打开图像文件时获取NullPointerException,java,nullpointerexception,Java,Nullpointerexception,我有一个内存匹配程序,允许用户向其中添加新文件夹和图像。当程序使用默认文件夹和图像运行时,它工作正常。当它选择用户添加的文件夹时,它将到达添加第一个图像的点,然后给出一个NullPointerException 这是我认为问题所在的代码 ArrayList <Card> cardsList = new ArrayList(); //cboDifficulty.addActionListener(this); String difficulty = cboD

我有一个内存匹配程序,允许用户向其中添加新文件夹和图像。当程序使用默认文件夹和图像运行时,它工作正常。当它选择用户添加的文件夹时,它将到达添加第一个图像的点,然后给出一个
NullPointerException

这是我认为问题所在的代码

    ArrayList <Card> cardsList = new ArrayList();

    //cboDifficulty.addActionListener(this);
    String difficulty =  cboDifficulty.getSelectedItem().toString();

        gameDiff = 0;//initialize gameDiff to 0

    if(difficulty.equals("Beginner")){//Beginnner
    /*place 3 cards and 1 copy of each card (total of 6 cards) on gamescreen*/
        gameDiff = 3;
    }
    else if(difficulty.equals("Easy")){//Easy
    /*place 6 cards and 1 copy of each card (total of 12 cards) on gamescreen*/
        gameDiff = 6;
    }

    String userDir = cboImages.getSelectedItem().toString();
    File filePath = new File(baseDir + "/" + userDir + "/");

    comboFile =  filePath.listFiles();


    List<File> listShuffle = Arrays.asList(comboFile);
    Collections.shuffle(listShuffle);
    for(int tick = 0; tick < listShuffle.size(); tick++){
        comboFile[tick] = listShuffle.get(tick);
    }

    for(int ctr = 0; ctr < comboFile.length; ctr++){
        if(ctr < gameDiff){

            Card card = new Card();
            card.setbackImage(new ImageIcon(cardBackImage));

            Image img = null;

            if(comboFile[ctr].isFile()){
                JOptionPane.showMessageDialog(null, comboFile[ctr].toString());
如前所述,当我使用默认文件夹时,此代码工作正常,只有当它尝试使用用户提供的图像时才会中断

以下是创建目录的代码:

    JFileChooser fc = new JFileChooser();


    int returnVal = fc.showOpenDialog(null);

    fc.setFileSelectionMode(fc.DIRECTORIES_ONLY);
    if (returnVal == fc.APPROVE_OPTION) {
        File selectedFile = fc.getSelectedFile();
        imageFilename = selectedFile.getPath();
        JOptionPane.showMessageDialog(null, "You selected " + imageFilename);
        ImageIcon imageView = new ImageIcon(imageFilename);
        lblIcon.setIcon(imageView);
下面是保存图像的代码

String sveCaption = lblCaption.getText();


    // Convert text in combobox to string...
    String newDir = cboSelectGroup.getSelectedItem().toString();

    // Convert path to string...
    String src = baseDir + "/" + newDir;

    // Create new file...
    File javaFilePath = new File(src, "/" + lblCaption.getText() + ".png");
    File oldImage = new File(imageFilename);
    if (! javaFilePath.exists()){

        JOptionPane.showMessageDialog(null, "Folder/Category, Image and Caption saved!!!");
        //oldImage.renameTo(javaFilePath);
        FileChannel copyFile = new FileInputStream(oldImage).getChannel();
        FileChannel dest = new FileOutputStream(javaFilePath).getChannel();
        dest.transferFrom(copyFile, 0, copyFile.size());
    }
保存操作将在用户选择的文件夹中创建一个新文件。游戏甚至会看到文件,因为它将输入if语句并打印我添加的消息。但在这一点上,它给出了一个例外

我忘了说在异常之前打印的消息显示了预期的路径images/userfolder/userimage.png。

此代码:

this.getClass().getResource(comboFile[ctr].getPath())
。。。依赖于映像作为资源在与执行类相同的类加载器中可用。如果它只是文件系统中的一个文件,而该类位于一个jar文件中,则情况并非如此

如果您只是尝试加载文件,请不要使用
Class.getResource()
-只需将文件名(最好是绝对文件名,以避免任何歧义)传递给
ImageIcon
构造函数。

此代码:

this.getClass().getResource(comboFile[ctr].getPath())
。。。依赖于映像作为资源在与执行类相同的类加载器中可用。如果它只是文件系统中的一个文件,而该类位于一个jar文件中,则情况并非如此

如果您只是尝试加载文件,请不要使用
Class.getResource()
-只需将文件名(最好是绝对文件名,以避免任何歧义)传递给
ImageIcon
构造函数。

此代码:

this.getClass().getResource(comboFile[ctr].getPath())
。。。依赖于映像作为资源在与执行类相同的类加载器中可用。如果它只是文件系统中的一个文件,而该类位于一个jar文件中,则情况并非如此

如果您只是尝试加载文件,请不要使用
Class.getResource()
-只需将文件名(最好是绝对文件名,以避免任何歧义)传递给
ImageIcon
构造函数。

此代码:

this.getClass().getResource(comboFile[ctr].getPath())
。。。依赖于映像作为资源在与执行类相同的类加载器中可用。如果它只是文件系统中的一个文件,而该类位于一个jar文件中,则情况并非如此


如果您只是尝试加载文件,请不要使用
Class.getResource()
-只需将文件名(最好是绝对文件名,以避免任何歧义)传递给
ImageIcon
构造函数。

这样做有效,我最初使用Class.getResource,因为它获取图像的唯一方法是使用绝对路径。团队中的一些成员使用闪存驱动器,因此路径不适用于所有人。@user1793408:是的,所以使用绝对路径-这不是使用
Class.getResource()
的理由。这样做,我最初使用Class.getResource,因为它获取图像的唯一方法是使用绝对路径。团队中的一些成员使用闪存驱动器,因此路径不适用于所有人。@user1793408:是的,所以使用绝对路径-这不是使用
Class.getResource()
的理由。这样做,我最初使用Class.getResource,因为它获取图像的唯一方法是使用绝对路径。团队中的一些成员使用闪存驱动器,因此路径不适用于所有人。@user1793408:是的,所以使用绝对路径-这不是使用
Class.getResource()
的理由。这样做,我最初使用Class.getResource,因为它获取图像的唯一方法是使用绝对路径。团队中的一些成员使用闪存驱动器,因此路径不适用于所有人。@user1793408:是的,所以使用绝对路径-这不是使用
Class.getResource()
的理由。