2014-12-01 57 views
0
//Max Maier, Milan Radovanovic 
package application; 
import javafx.fxml.FXML; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.control.TextField; 
public class Control { 
    private static int anz = 1; 
    int generierteZahl = (int) Math.ceil(Math.random()*100); 
    Label anzversuche; 
    Label zahlenbereich; 
    Button ratenb; 
    TextField eingabe; 
    @FXML void raten(){ 
     if (generierteZahl == Integer.parseInt(eingabe.getText())){ 
      anzversuche.setText(anz + ". Versuche"); 
      ratenb.setDisable(true); 
      zahlenbereich.setText("Richtig geraten!!!"); 
     } 
     else if (generierteZahl > Integer.parseInt(eingabe.getText())){ 
      anz++; 
      anzversuche.setText(anz + ". Versuche"); 
      zahlenbereich.setText(Integer.parseInt(eingabe.getText()) + " ist zu klein"); 
     } 
     else{ 
      anzversuche.setText(anz + ". Versuche"); 
      zahlenbereich.setText(Integer.parseInt(eingabe.getText()) + " ist zu groß"); 
     } 
     anz++; 
    } 
    @FXML void neuerVersuch(){ 
     anz = 1; 
     generierteZahl = (int) Math.ceil(Math.random()*100); 
     zahlenbereich.setText("Noch keine Zahl eingegeben"); 
     anzversuche.setText(anz + ". Versuche"); 
     ratenb.setDisable(false); 
     eingabe.setText(""); 
    } 

} 

這thows一堆如果例外:的InvocationTargetException當使用JavaFX set和get方法

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException 
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source) 
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source) 
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source) 
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source) 
    at javafx.event.Event.fireEvent(Unknown Source) 
    at javafx.scene.Node.fireEvent(Unknown Source) 
    at javafx.scene.control.Button.fire(Unknown Source) 
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source) 
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source) 
    at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source) 
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source) 
    at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source) 
    at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source) 
    at javafx.event.Event.fireEvent(Unknown Source) 
    at javafx.scene.Scene$MouseHandler.process(Unknown Source) 
    at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source) 
    at javafx.scene.Scene.impl_processMouseEvent(Unknown Source) 
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source) 
    at com.sun.glass.ui.View.handleMouseEvent(Unknown Source) 
    at com.sun.glass.ui.View.notifyMouse(Unknown Source) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source) 
    at com.sun.glass.ui.win.WinApplication$$Lambda$37/1109371569.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at sun.reflect.misc.Trampoline.invoke(Unknown Source) 
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at sun.reflect.misc.MethodUtil.invoke(Unknown Source) 
    ... 48 more 
Caused by: java.lang.NullPointerException 
    at application.Control.neuerVersuch(Control.java:34) 
    ... 57 more 

這是一個小猜數遊戲,創建一個隨機數,然後在字符串比較textfield「eingabe」。我明白爲什麼有時會引發NumberFormatException,但調用異常是全新的,我真的很感謝一些幫助。

回答

1

異常說,它在最後:

Caused by: java.lang.NullPointerException 
at application.Control.neuerVersuch(Control.java:34) 

雖然我說不出是什麼線是它的錯誤出現時neuerVersuch被調用,並發現未初始化的領域,可能zahlenbereich

原因是這個字段(也可能是其他字段)沒有被instatiated,因爲它沒有用@FXML註釋。如果thouse在你FXML文件控制,他們有一個fx:id,你應該加上註解:

@FXML Label anzversuche; 
@FXML Label zahlenbereich; 
@FXML Button ratenb; 
@FXML TextField eingabe; 

因此被載入控制器時,他們得到適當的instantited。

+0

非常感謝你,雖然這實際上並不是一個大問題,並在一段時間後自己想清楚了。很高興有人回答:) – 2016-06-03 18:45:06