我正在尝试为学校的小程序分配获取背景图像,我收到一个我不明白的错误。如果相关,也使用Ready-to-Program
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.*;
public class Summative extends Applet
implements ActionListener
{
Button bttn1 = new Button ("Formulas");
Button bttn2 = new Button ("Start");
boolean Formulas = false;
boolean Start = false;
URL url;
Image background;
public void init ()
{
setSize (1000, 1000);
try
{
url = getDocumentBase ();
}
catch (Exception e)
{
}
background = getImage(url, "MathMeme.JPEG");
setBackground (Color.white);
bttn1.addActionListener (this);
bttn2.addActionListener (this);
add (bttn1);
add (bttn2);
}
public void paint (Graphics g)
{
g.setColor (Color.white);
g.drawImage (background, 0, 0, this);
/*if (Formulas == true)
{
Dimension d = getSize();
g.setColor(Color.WHITE);
g.fillRect(0, 0, d.width, d.height);
g.drawString ("Enter Formulas here", 100, 100);
}*/
}
public void actionPerformed (ActionEvent evt)
{
if (evt.getSource () == bttn1)
{
Formulas = true;
repaint ();
}
}
}
这是我得到的错误:
java.lang.NullPointerException
at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
at sun.java2d.SunGraphics2D.drawImage(Unknown Source)
at sun.java2d.SunGraphics2D.drawImage(Unknown Source)
at Summative.paint(Summative.java:38)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
我认为空指针异常是当你尝试并访问具有空值的东西,所以我不明白我如何在这个程序中有一个。