2013-06-27 2 views
-1

문제의 원인을 파악하려고했지만 그 사실을 알 수 없습니다. w.setCandyAmountLabelText (numberofcandies)를 사용하여 클래스의 메서드를 호출합니다. 그러나 그것은 작동하지 않습니다. 다른 클래스에있는 jlabel의 텍스트를 변경하려고합니다. 다음은 오류 및 코드입니다. 오류Java JLabel setText 메서드가 작동하지 않습니다.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
at CandyWindow.setCandyAmountLabelText(CandyWindow.java:111) 
at Counter$1.actionPerformed(Counter.java:32) 
at javax.swing.Timer.fireActionPerformed(Unknown Source) 
at javax.swing.Timer$DoPostEvent.run(Unknown Source) 
at java.awt.event.InvocationEvent.dispatch(Unknown Source) 
at java.awt.EventQueue.dispatchEventImpl(Unknown Source) 
at java.awt.EventQueue.access$200(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.awt.EventQueue$3.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 
at java.awt.EventQueue.dispatchEvent(Unknown Source) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) 
at java.awt.EventDispatchThread.pumpEventsForFilter(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) 

mainclass

public class CandyMain { 

public static void main(String[] args) { 
    CandyWindow window = new CandyWindow("Candy Box"); 
    window.init(); 

} 

CandyWindow 클래스

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.*; 

public class CandyWindow extends JFrame { 

public JPanel mainpanel; 
public JLabel candyamountlabel; 
public JButton eatcandies; 
public JLabel candieseaten; 
public boolean candiesmorethan10 = false; 
public JButton throwcandies; 


Counter counter; 

CandyWindow(String title) { 

    super(title); 

    this.setVisible(true); 
    this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 

} 

CandyWindow() { 

} 

public void init() { 

    counter = new Counter(); 

    // initialized label 

    candyamountlabel = new JLabel("You Have 0 Candies!"); 
    eatcandies = new JButton("Eat All The Candies!"); 
    candieseaten = new JLabel("You Have Eaten 0 Candies!"); 
    throwcandies = new JButton("Throw Candies On Ground!"); 


    //sets visibilty to false 
    candieseaten.setVisible(false); 
    throwcandies.setVisible(false); 




    // add action listener 
    eatcandies.addActionListener(eatcandieslistener); 

    // makes panel 
    mainpanel = new JPanel(); 




    // adds label to panel 
    mainpanel.add(candyamountlabel); 
    mainpanel.add(eatcandies); 
    mainpanel.add(candieseaten); 
    mainpanel.add(throwcandies); 

    // adds panel to jframe 
    this.add(mainpanel); 
    this.setSize(1600, 850); 


    counter.startTimer(); 


} 

ActionListener eatcandieslistener = new ActionListener(){ 

    @Override 
    public void actionPerformed(ActionEvent e) { 

     counter.setCandiesEaten(counter.getNumberOfCandies() + counter.getCandiesEaten()); 
     counter.eatcandies(); 
     candyamountlabel.setText("You Have 0 Candies!"); 
     candieseaten.setText("You Have Eaten " + counter.getCandiesEaten() + " Candies!"); 
     candieseaten.setVisible(true); 

    } 



}; 


public void setThrowCandiesVisible(int y){ 

    if(y == 1){ 

     candiesmorethan10 = true; 

    } 
    if(candiesmorethan10){ 

     throwcandies.setVisible(true); 
     throwcandies.repaint(); 

    } 

} 

public void setCandyAmountLabelText(long g){ 

    candyamountlabel.setText("You Have " + g + " Candies!"); 
    candyamountlabel.repaint(); 


} 

    } 

카운터 클래스

import java.awt.event.*; 
import javax.swing.*; 

public class Counter { 

long numberofcandies = 0; 
long candiespersecond = 0; 
long candieseaten = 0; 

CandyWindow w = new CandyWindow(); 

void startTimer() { 

    Timer t = new Timer(1000, timercount); 
    t.setRepeats(true); 
    t.start(); 

} 

ActionListener timercount = new ActionListener() { 

    @Override 
    public void actionPerformed(ActionEvent arg0) { 

     // setscandiespersecond to one 
     setCandiesPerSecond(1); 

     //increases candies 
     numberofcandies = candiespersecond + numberofcandies; 

     //changes numberofcandieslabel 
     w.setCandyAmountLabelText(numberofcandies); 


     //if candies are more than 10 set throw on ground visible 
     int x = 0; 
     if(numberofcandies > 9){ 
      x = 1; 
      w.setThrowCandiesVisible(x); 

     } 


     //System.out.println("Number of Candies" + getNumberOfCandies()); //test print works 
     //System.out.println("candies eaten" + getCandiesEaten()); //test print works 


    } 

}; 



public long getNumberOfCandies() { 

    return numberofcandies; 

} 

public long getCandiesPerSecond() { 

    return candiespersecond; 
} 

public void setCandiesPerSecond(long g) { 

    candiespersecond = g; 

} 

public void eatcandies(){ 

    numberofcandies = 0; 

} 

public long getCandiesEaten(){ 

    return candieseaten; 

} 

public void setCandiesEaten(long p){ 

    candieseaten = p; 

} 


} 

답변

0

당신의 CandyWindow는의 새로운 카운터 객체를 생성-하지만이 카운터 객체는 생성 될 때 새로운 CandyWindow를 만듭니다 : CandyWindow w = new CandyWindow();. 따라서 카운터는 카운터를 만든 CandyWindow를 참조하지 않습니다. w 대신 레퍼런스

counter = new Counter(this); 

및 CandyWindow 걸리며, 설정 카운터 생성자를 생성 : 같은 카운터와 함께 CandyWindow 전달 시도 NullPointerException이이 경우에 발생

public Counter(CandyWindow w) { 
    this.w = w; 
} 

, Counter 객체가 새로운 CandyWindow에 대한 참조를 가지고 있기 때문에 init() 함수는 호출되지 않습니다. 따라서 'w.setCandyAmountLabelText (numberofcandies);'라는 라인이 호출됩니다. 'onActionPerformed()'에있는 CandyAmountLabelText는이 새로운 CandyWindow의 초기화되지 않습니다.

+0

정말 고마워요, 그 속임수를했습니다! – gman9732

관련 문제