我有一个内存匹配程序,允许用户向其中添加新文件夹和图像。当程序运行时默认的文件夹和图像工作正常。当它选择用户添加的文件夹时,它会添加第一个图像,然后提供NullPointerException
。尝试在java中打开图像文件时发生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());
下一行是代码,其中NullPointerException
是
img = new ImageIcon(this.getClass().getResource(comboFile[ctr].getPath())).getImage();
}
Image dimg = img.getScaledInstance(144, 216, Image.SCALE_SMOOTH);
card.setfrontImage(new ImageIcon(dimg));
// Populate card with db results
cardsList.add(card);
}
}
// Clone list of cards for game
for(int counter = 0; counter < gameDiff && counter < cardsList.size(); counter++){
// Pull out current card from the loop
Card orgCard = cardsList.get(counter);
// Make new card to populate (clone it)
Card cloneCard = new Card();
cloneCard.setfrontImage(orgCard.getfrontImage());
cloneCard.setbackImage(orgCard.getbackImage());
cardsList.add(cloneCard);
}
shuffleCard(cardsList);
createGameScreen(cardsList);
如前所述,当我使用的默认文件夹此代码的工作正常只有在尝试使用用户提供的图像时才会中断。
下面是创建目录代码:
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。
这工作,我最初使用Class.getResource,因为它将获得图像的唯一方式是绝对路径。团队中的一些成员正在使用闪存驱动器,因此这条路不适合每个人。 – user1793408
@ user1793408:是的,所以使用绝对路径 - 没有理由使用'Class.getResource()'。 –