2012-05-28 4 views
0

이 질문을 해결하려고 노력하지만 무한 루프가 있으며이 문제를 해결하는 방법을 모릅니다. 나를 안내 해줘. 나는 자바가 처음이다. 터미널에서Applet (Animation)의 랜덤 삼각형 생성

: 도움을

Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException 
     at ThreadApplet.RandGen(ThreadApplet.java:10) 
     at ThreadApplet.paint(ThreadApplet.java:30) 
     at java.awt.Container.update(Container.java:1988) 
     at sun.awt.RepaintArea.updateComponent(RepaintArea.java:255) 

감사합니다!

질문 : 임의로 생성 된 삼각형을 다른 색상으로 표시하는 애플릿을 작성하십시오.

/////////////Applet 

import java.awt.*; 

import java.applet.*; 

public class ThreadApplet extends Applet{ 
    MyThread thread; 
    int x[],y[]; 
    public boolean ctrl=true; 

    public void RandGen(){ 
     for (int i=0; i<3;i++){ 
      x[i]=(int)Math.random()*100; 
      y[i]=(int)Math.random()*100; 
     }  
    } 

    public void start(){ 
     if (thread==null){ 
      thread = new MyThread(this); 
      thread.start(); 
     } 
    } 

    public void stop(){ 
     thread = null; 
    } 

    public void paint(Graphics g){ 
     if (ctrl==true){ 
      g.setColor(Color.blue); 
     } else { 
      g.setColor(Color.red); 
     } 
     RandGen(); 
     g.fillPolygon(x,y,3); 
    } 
} 

public class MyThread extends Thread{ 
    ThreadApplet applet; 

    public MyThread (ThreadApplet applet){ 
     this.applet=applet; 
    } 
    public void run(){ 
     Thread thisThread = Thread.currentThread(); 
     while (this==thisThread){   
      applet.repaint(); 
      try{Thread.sleep(50);} 
      catch(InterruptedException e){} 
     }  
    } 
} 
+0

NullPointerException을 다시 실행 한 경우, throw되는 행을 검사하십시오. 이는 보통 어떤 변수가 널 (null)이 아니어야 하는지를 알려주기 때.입니다. 그래도 문제를 해결할 수 없으면 도움을 받아야합니다. 그 문제를 일으키는 행을 *** 저희에게 알려주십시오 (** 큰 ** 의견을 제시하십시오). 그래서 우리는 힘들어하지 않아도됩니다. 그것이 어느 선인지 짐작하십시오. –

답변

3

당신은 사용하기 전에 배열을 초기화 할 필요 : 예를 들어

int x[] = new int[17]; 

.