运行此程序时,我会收到此错误代码:
java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(ImageIcon.java:217)
at DebugMe.<init>(DebugMe.java:16)
at DebugMe.main(DebugMe.java:31)
但是编译器本身没有抛出任何错误。 &#34; debug.jpeg&#34; image也在程序运行的同一文件夹中。
import java.awt.*;
import javax.swing.*;
public class DebugMe extends JFrame
{
private String message = "Congratulations, the program is working again.";
Image debug = new ImageIcon(this.getClass().getResource("debug.jpeg")).getImage();
int x; //Initialized x
/**
* Constructor for objects of class DebugMe
*/
public DebugMe()
{
x = 0;
setSize(500, 500);
setVisible(true); //Lowercase t on true
}
public static void main(String[] args)
{
DebugMe myDebugMe = new DebugMe(); //capitalized me in Debugme
int x;
}
public void paint(Graphics g)
{
super.paint(g);
g.drawString("message", 100, 100);
g.drawImage(debug, 100, 200, Color.BLUE, this);
}
}