为什么红线部分会有空指针异常???
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class DouDongText extends Applet implements Runnable
{
Image image;
Graphics graphics,imagegraphics ;
String message;
private int pointX,pointY;
Color textcolor,bgcolor;
FontMetrics fontmetrics;
Dimension dimension;
Thread thread ;
public void init()
{
graphics = this.getGraphics();
dimension= this.getSize();
image = this.createImage(dimension.width,dimension.height);
imagegraphics = image.getGraphics();
fontmetrics = graphics.getFontMetrics();
message = "抖动一下啦";
pointX = (dimension.width - fontmetrics.stringWidth(message))/2;
pointY = dimension.height - 10;
String param;
if((param = getParameter("TEXTCOLOR")) == null)
textcolor = Color.blue;
else
textcolor = new Color(Integer.parseInt(param));
if((param = getParameter("BACKGROUNDCOLOR")) == null)
bgcolor = Color.pink;
else
bgcolor = new Color(Integer.parseInt(param));
thread = new Thread(this);
}
public void start()
{
if(thread == null)
thread = new Thread(this);
thread.start();
}
public void run()
{
while(thread != null)
{
try
{
thread.sleep(2000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
repaint();
}
System.exit(0); //退出程序
}
public void paint(Graphics g)
{
g.setColor(bgcolor);
g.fillRect(0,0,dimension.width,dimension.height);
g.setColor(textcolor);
g.drawString(message,pointX,pointY);
}
public void update(Graphics g)
{
//待续
}
}
----------------解决方案--------------------------------------------------------
因为image没有赋初值
image本身就是null,调用null的方法当然会异常
正在提交,请稍候
----------------解决方案--------------------------------------------------------
能不能具体说一下到底怎么改阿,这个地方不太懂!,谢谢!
----------------解决方案--------------------------------------------------------
你image赋值啊
你不赋值它就是null,
----------------解决方案--------------------------------------------------------
Image image;
----------------解决方案--------------------------------------------------------