次のコードに問題があります。
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class Fraktalz extends Applet{
private static final long serialVersionUID = 1L;
Graphics grafika;
public void paint(Graphics g){
g.setColor(Color.white);
g.fillRect(0, 0, 640, 480);
g.setColor(Color.black);
smok(200,360,500,120,20);
}
public void smok(int x1, int y1, int x2, int y2, int iteracja){
if (iteracja == 0){
grafika.drawLine(x1,y1,x2,y2);
} else{
int cx = (x1 + x2) / 2;
int cy = (y1 + y2) / 2;
int x3 = cx + (y2 - cy);
int y3 = cy + (x2 - cx);
smok(x1,y1,y3,y3,iteracja-1);
smok(x2,y2,x3,y3, iteracja-1);
}
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Fraktalz();
}
});
}
}
それは私がやったときに私が得たと言いnullPointerException
ますGraphics grafika;
:
Fraktalz fr = new Fraktalz();
Graphics grafika = fr.getGraphics();
エラーが発生しました:
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at java.lang.Class.getClassLoader(Unknown Source)
at java.awt.Component.checkCoalescing(Unknown Source)
at java.awt.Component.<init>(Unknown Source)
at java.awt.Container.<init>(Unknown Source)
at java.awt.Panel.<init>(Unknown Source)
at java.awt.Panel.<init>(Unknown Source)
at java.applet.Applet.<init>(Unknown Source)
at Fraktalz.<init>(Fraktalz.java:6)
at Fraktalz.<init>(Fraktalz.java:9)
at Fraktalz.<init>(Fraktalz.java:9)
誰かがそのエラーで私を助けることができますか?