0

將字符串從一個類傳遞到其他類中的函數時出現NullPointerException。我不完全明白什麼是錯的。我也試圖確定我傳遞的字符串是否爲null。但它不是空的。在兩個類中傳遞字符串時出現錯誤JavaFX

下面是異常指出的行和AMCHelpEditor中的函數的位置(此代碼中的錯誤位於最後一行:原始代碼的125行):

String s = AMCImage.getSmellFromAMC(file); 
System.out.print(s); 
System.out.print(s.isEmpty()); 
System.out.print(SMCImage.getSmellFromAMC(f)); 
GridPane grid = sm.AMCEdit(s); 

另外,AMCEdit在AMCEditor功能碼是:

public GridPane AMCEdit(String s){ 
    amc = am.readAllAmcName(); 
    AMCBean sb2 = new AMCBean(); 
    AMCAccessor sa = new AMCAccessor(); 
    sb2 = sa.getAllAmcInfoById(s); 
    String name = sb2.getAmcName(); 
    gridPane.setPadding(new Insets(20, 0, 20, 20)); 
    gridPane.setHgap(7); gridPane.setVgap(7); 

    Label amc1 = new Label("Select AMC :"); 
    GridPane.setHalignment(smell1, HPos.RIGHT); 


    cb = new ChoiceBox(); 
    cb.setItems(FXCollections.observableArrayList(amc)); 
    cb.getSelectionModel().select(name); 



    cb.valueProperty().addListener(new ChangeListener() { 
     @Override 
     public void changed(ObservableValue observable, Object oldValue, 
      Object newValue) { 
      text =cb.getValue().toString(); 
      xyz = sm.getAllAmcInfoByName(text); 
      descptn = xyz.getAmcDesc().toString(); 
      frmla = xyz.getAmcChemFormula().toString(); 
      am = xyz.getAmcId(); 
      setAmc(am); 
      tfdesc = new TextField(descptn);   
      tffrml = new TextField(frmla); 
      } 
    }); 


    gridPane.add(amc1, 0, 0); 
    gridPane.add(cb, 1, 0); 

     gridPane.getColumnConstraints().addAll(
     new ColumnConstraints(150), 
     new ColumnConstraints(200) 
    ); 
      gridPane.getRowConstraints().addAll(
      new RowConstraints(30), 
      new RowConstraints(), // scale 
      new RowConstraints(30), 
      new RowConstraints(), 
      new RowConstraints(30), 
      new RowConstraints(), 
      new RowConstraints(30) 

    ); 

    return gridPane; 

} 

示出爲系統輸出的錯誤是:

java.lang.NullPointerException 
    at com.smell.component.AMCEditor.AMCEdit(AMCEditor.java:125) 
    at com.smell.component.AMCHelpEditor.getTab(AMCHelpEditor.java:74) 
    at com.smell.modules.ImageApi$1$1$1.handle(ImageApi.java:100) 
    at com.smell.modules.ImageApi$1$1$1.handle(ImageApi.java:96) 
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:69) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170) 
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) 
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53) 
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:28) 
    at javafx.event.Event.fireEvent(Event.java:171) 
    at javafx.scene.Node.fireEvent(Node.java:6863) 
    at javafx.scene.control.Button.fire(Button.java:179) 
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:193) 
    at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:336) 
    at com.sun.javafx.scene.control.skin.SkinBase$4.handle(SkinBase.java:329) 
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:64) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:217) 
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:170) 
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:38) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:37) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) 
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:35) 
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:92) 
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:53) 
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:33) 
    at javafx.event.Event.fireEvent(Event.java:171) 
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3324) 
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3164) 
    at javafx.scene.Scene$MouseHandler.access$1900(Scene.java:3119) 
    at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1559) 
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2261) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:228) 
    at com.sun.glass.ui.View.handleMouseEvent(View.java:528) 
    at com.sun.glass.ui.View.notifyMouse(View.java:922) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29) 
    at com.sun.glass.ui.win.WinApplication$2$1.run(WinApplication.java:67) 
    at java.lang.Thread.run(Thread.java:722) 
'201''201'false'201'Exception in thread "JavaFX Application Thread" jfxsa-run: 

系統輸出符合預期(請參閱錯誤的最後幾行)。如果字符串不爲空然後

請您指出錯誤或建議替代它嗎?

回答

0

我建議,與其檢查空字符串,把一個空檢查你的SM對象

sm.AMCEdit(S);

這裏您SM對象可能不會被初始化,然後試圖調用未初始化的SM的方法將導致NPE

所以一定要確保你已經initialzed它。

+0

sm對象永遠不會爲null ..它始終是初始化的..我通過調試檢查它以及... –

相關問題