2016-09-24 48 views
0

我有一個項目,我要求用戶使用什麼類型的電子設備來告訴他們需要多少個太陽能電池板和電池。在JavaFX的GridPane中從TextArea中獲取文本

爲了收集信息,我使用了一個帶有三列的gridpane,其中有TextArea和可變數量的行。我需要收集gridPane的第二行和第三行中的信息。

我用下面的方法找到了另一個問題。

private void calculer(ActionEvent event) 
{ 
    double ampApp = 0; 
    double heureApp = 0; 
    double wattTotal = 0; 
    double nbJourAuto; 
    double heureSoleil; 
    int nbPanneau; 
    int nbBatterie; 
    String stringTemp; 

    for(int i =1; i < rangee; i++) 
    { 
     stringTemp = getNodeByRowColumnIndex(i, 1, gridApp).toString(); 
     ampApp += parseDouble(stringTemp); 
    } 

    for(int i =1; i < rangee; i++) 
    { 
     stringTemp = getNodeByRowColumnIndex(i, 2, gridApp).toString(); 
     heureApp += parseDouble(stringTemp); 
    } 

(這是唯一的方法的第一部分,我知道有一個缺少 「}」)

這裏是getNodeByRowColumnIndex代碼:

public Node getNodeByRowColumnIndex(final int row, final int column, GridPane gridPane) { 
    Node result = null; 
    ObservableList<Node> childrens = gridPane.getChildren(); 
    for(Node node : childrens) 
    { 
     if(gridPane.getRowIndex(node) == row && gridPane.getColumnIndex(node) == column) 
     { 
      result = node; 
      break; 
     } 
    } 
    return result; 
} 

這個方法我試過,但不斷得到java.lang.NullPointerException

有誰知道爲什麼以及如何解決這個問題?

編輯:

如這裏要求的堆棧跟蹤

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException 
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774) 
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657) 
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) 
at javafx.event.Event.fireEvent(Event.java:198) 
at javafx.scene.Node.fireEvent(Node.java:8411) 
at javafx.scene.control.Button.fire(Button.java:185) 
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182) 
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96) 
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89) 
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) 
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) 
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) 
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) 
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) 
at javafx.event.Event.fireEvent(Event.java:198) 
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757) 
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485) 
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762) 
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:380) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294) 
at java.security.AccessController.doPrivileged(Native Method) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:416) 
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389) 
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415) 
at com.sun.glass.ui.View.handleMouseEvent(View.java:555) 
at com.sun.glass.ui.View.notifyMouse(View.java:937) 
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191) 
at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.reflect.InvocationTargetException 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71) 
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275) 
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769) 
... 48 more 
Caused by: java.lang.NullPointerException 
at systemesolaire.FXMLDocumentController.getNodeByRowColumnIndex(FXMLDocumentController.java:111) 
at systemesolaire.FXMLDocumentController.calculer(FXMLDocumentController.java:81) 
... 58 more 

這裏就是我intialize我的孩子(我用SceneBuilder所以這裏是FXML文件)

<content> 
     <GridPane fx:id="gridApp" prefHeight="141.0" prefWidth="452.0"> 
      <columnConstraints> 
       <ColumnConstraints hgrow="SOMETIMES" maxWidth="226.0" minWidth="10.0" prefWidth="226.0" /> 
       <ColumnConstraints hgrow="SOMETIMES" maxWidth="146.0" minWidth="10.0" prefWidth="123.0" /> 
       <ColumnConstraints hgrow="SOMETIMES" maxWidth="113.0" minWidth="10.0" prefWidth="103.0" /> 
      </columnConstraints> 
      <rowConstraints> 
       <RowConstraints minHeight="40.0" prefHeight="40.0" vgrow="NEVER" /> 
       <RowConstraints minHeight="40.0" prefHeight="40.0" vgrow="NEVER" /> 
       <RowConstraints minHeight="40.0" prefHeight="40.0" vgrow="NEVER" /> 
      </rowConstraints> 
      <children> 
       <Label text=" Nom de l'appareil"> 
       <font> 
        <Font name="Anke" size="18.0" /> 
       </font> 
       </Label> 
       <Label text="Ampérage" GridPane.columnIndex="1"> 
       <font> 
        <Font name="Anke" size="14.0" /> 
       </font> 
       </Label> 
       <Label text="Heures utilisation" GridPane.columnIndex="2"> 
       <font> 
        <Font name="Anke" size="13.0" /> 
       </font> 
       </Label> 
       <TextArea prefHeight="200.0" prefWidth="200.0" promptText="Nom" GridPane.rowIndex="1" /> 
       <TextArea prefHeight="200.0" prefWidth="200.0" promptText="Amp" GridPane.columnIndex="1" GridPane.rowIndex="1" /> 
       <TextArea prefHeight="200.0" prefWidth="200.0" promptText="Heure" GridPane.columnIndex="2" GridPane.rowIndex="1" /> 
       <TextArea prefHeight="200.0" prefWidth="200.0" promptText="Nom" GridPane.rowIndex="2" /> 
       <TextArea prefHeight="200.0" prefWidth="200.0" promptText="Amp" GridPane.columnIndex="1" GridPane.rowIndex="2" /> 
       <TextArea prefHeight="200.0" prefWidth="200.0" promptText="Heure" GridPane.columnIndex="2" GridPane.rowIndex="2" /> 
      </children> 
     </GridPane> 
    </content> 

這裏是我如何添加一行到我的gridPane

@FXML 
private void ajouteRangee(ActionEvent event) 
{ 

    TextArea nom = new TextArea(); 
    TextArea amp = new TextArea(); 
    TextArea heure = new TextArea(); 



    nom.setPromptText("Nom"); 
    amp.setPromptText("Amp"); 
    heure.setPromptText("heure"); 


    gridApp.addRow(rangee, nom, amp, heure); 
    gridApp.layout(); 
    rangee++; 

} 
+0

我們需要堆棧跟蹤知道了什麼錯誤......(在哪裏如何初始化組件?在哪裏添加組件到GridPane ...)我假設這是給NullPointer去那個孩子哈從未被初始化。 – GOXR3PLUS

+0

可能重複[什麼是NullPointerException,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it ) – fabian

+0

我添加了堆棧跟蹤 – MrDjEnjoy

回答

0

我在老師的幫助下得到了答案,如果有人有同樣的問題,我就是這樣解決的。

NullPointerException來的getColumnIndex,解決方案繞過這個問題,但我不知道爲什麼我第一次得到這個異常。

由於已知行和列的數量,因此不需要搜索每個節點的正確值。此外,由於每行兒童的數量相同,因此有一種合理的方式來獲得特定的兒童。具體方法如下:

public TextArea getNodeByRowColumnIndex(final int row, final int column, GridPane gridPane) 
{ 

    ObservableList<Node> childrens = gridPane.getChildren(); 
    TextArea result = (TextArea) childrens.get((row*3)+column); 

    return result; 
} 

的ObservableList得到孩子從一排從左到右排,這樣你就可以在列表中的特定對象與此公式(((的rowIndex + 1)*每行對象的數量)+在其列是你的對象)

然後,你可以通過執行獲得文本區的文字:

stringTemp = getNodeByRowColumnIndex(i, 1, gridApp).getText();