我知道这个问题已被回答了太多次,但无论我尝试解决方案,我都无法解决这个问题。
我想使用jpeg图像作为背景,但无论我尝试过,我都无法解决它。
以下是我的最终包结构:
images/
-- bg.jpeg
org/
-- Main.java (used for test)
public class Main {
BufferedImage img;
public static void main(String[] args) {
Main main = new Main();
main.load();
}
public void load(){
try {
ClassLoader cl = this.getClass().getClassLoader();
System.out.println("CL:"+cl);
InputStream url = getClass().getClassLoader().getResourceAsStream("/images/bg.jpg");
System.out.println("URL:"+url);
this.img = ImageIO.read(url); // Null argument exception
} catch (IOException ex) {
Logger.getLogger(BoardView.class.getName()).log(Level.SEVERE, null, ex);
}
}}
CL:sun.misc.Launcher$AppClassLoader@15663a2
URL:null
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1348)
at org.Main.load(Main.java:32)
at org.Main.main(Main.java:24)
我正在使用JDK7和Maven项目。
答案 0 :(得分:3)
你的图像路径有点正确...
使用getClassLoader()
时,您不会在/
前使用额外的images
。
getClass().getClassLoader().getResourceAsStream("images/bg.jpg");
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Main {
BufferedImage img;
public static void main(String[] args) {
Main main = new Main();
main.load();
}
public void load() {
try {
ClassLoader cl = this.getClass().getClassLoader();
System.out.println("CL:" + cl);
InputStream url = getClass().getClassLoader().getResourceAsStream("resources/stackoverflow5.png");
System.out.println("URL:" + url);
this.img = ImageIO.read(url); // Null argument exception
JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(img)), "No ClassLoader", JOptionPane.PLAIN_MESSAGE);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
如果你_don't不使用getClassLoader()
,那么你做需要它
getClass().getResourceAsStream("/images/bg.jpg");
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Main {
BufferedImage img;
public static void main(String[] args) {
Main main = new Main();
main.load();
}
public void load() {
try {
ClassLoader cl = this.getClass().getClassLoader();
System.out.println("CL:" + cl);
InputStream url = getClass().getClass().getResourceAsStream("/resources/stackoverflow5.png");
System.out.println("URL:" + url);
this.img = ImageIO.read(url); // Null argument exception
JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(img)), "With ClassLoader", JOptionPane.PLAIN_MESSAGE);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
尽可能简单地说明州here
.getClass().getResource(fileName)
时,它会考虑
fileName的位置与调用的位置相同
类。.getClass().getClassLoader().getResource(fileName)
时
从根注意:我的文件结构类似于您的
ProjectRoot
resources
stackoverflow5.png
mypackage
Main.java
答案 1 :(得分:2)
你可以试试这个
Image img = Toolkit.getDefaultToolkit().getImage(
YourClassName.class.getResource("/images/bg.jpg"));
答案 2 :(得分:1)
图像的输入流为null
。可能,你正在寻找图像的错误路径。或者没有读取权限
答案 3 :(得分:0)
首先检查图像的路径,您可以尝试不使用/
它可以解决您的问题。
答案 4 :(得分:0)
我回来添加一个答案,因为在我最后一次再次显示问题后解决了我的问题,我再也无法加载图像了。我很沮丧,但在搜索了一下后,我找到了另一个明确的答案
我们的想法是在构建项目后找到目标文件夹中创建的资源,从那里可以找到要加载的资源的路径。
这是答案的链接:
How to correctly get image from 'Resources' folder in NetBeans