2014-06-25 65 views
1

我想從Class1發送一個字符串值到Class2,但我得到空指針Exceiption 我已經在class2中導入class1。如何將Value從一個類傳遞給Java中的其他類?

2類代碼 - :

@Override 
public void changed(ObservableValue<? extends String> ov, String t, String t1) { 
    //System.out.println(ov.getValue()); 

    if (ov.getValue().toString().length() > 0) { 

     String txtdata = (textbox.getText()).trim(); 
     textVal = txtdata; 

     System.out.println("Getting textbox value in AutoFillTextBoxSkin.java"); 
     System.out.println("[*" + txtdata + "*]"); 
     //System.out.println("printing textVal check in skin->"+textVal); 

     String put = custCtrl.txtVal2(txtdata); 
    } 
} 

這裏custCtrl是1類對象和txtVal2()是方法接收字符串數據(下面示例代碼)

1類代碼 - :

public String txtVal2(String a) 
{ 
    System.out.println("Getting text box value===>"+a); 
    return "ok"; 
} 

錯誤 - :

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException 
    at np.com.ngopal.control.AutoFillTextBoxSkin.changed(AutoFillTextBoxSkin.java:374) 
    at np.com.ngopal.control.AutoFillTextBoxSkin.changed(AutoFillTextBoxSkin.java:35) 
    at com.sun.javafx.binding.ExpressionHelper$Generic.fireValueChangedEvent(ExpressionHelper.java:347) 
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80) 
    at javafx.scene.control.TextInputControl$TextProperty.fireValueChangedEvent(TextInputControl.java:1116) 
    at javafx.scene.control.TextInputControl$TextProperty.markInvalid(TextInputControl.java:1120) 
    at javafx.scene.control.TextInputControl$TextProperty.invalidate(TextInputControl.java:1060) 
    at javafx.scene.control.TextInputControl$TextProperty.access$200(TextInputControl.java:1032) 
    at javafx.scene.control.TextInputControl$1.invalidated(TextInputControl.java:130) 
    at com.sun.javafx.binding.ExpressionHelper$SingleInvalidation.fireValueChangedEvent(ExpressionHelper.java:135) 
    at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:80) 
    at javafx.scene.control.TextField$TextFieldContent.insert(TextField.java:82) 
    at javafx.scene.control.TextInputControl.replaceText(TextInputControl.java:464) 
    at com.sun.javafx.scene.control.skin.TextFieldSkin.replaceText(TextFieldSkin.java:580) 
    at com.sun.javafx.scene.control.behavior.TextFieldBehavior.replaceText(TextFieldBehavior.java:199) 
    at com.sun.javafx.scene.control.behavior.TextInputControlBehavior.defaultKeyTyped(TextInputControlBehavior.java:251) 
    at com.sun.javafx.scene.control.behavior.TextInputControlBehavior.callAction(TextInputControlBehavior.java:149) 
    at com.sun.javafx.scene.control.behavior.BehaviorBase.callActionForEvent(BehaviorBase.java:222) 
    at com.sun.javafx.scene.control.behavior.TextInputControlBehavior.callActionForEvent(TextInputControlBehavior.java:137) 
    at com.sun.javafx.scene.control.behavior.BehaviorBase$1.handle(BehaviorBase.java:136) 
    at com.sun.javafx.scene.control.behavior.BehaviorBase$1.handle(BehaviorBase.java:133) 
    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.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:204) 
    at javafx.scene.Scene$KeyHandler.process(Scene.java:3949) 
    at javafx.scene.Scene$KeyHandler.access$2100(Scene.java:3896) 
    at javafx.scene.Scene.impl_processKeyEvent(Scene.java:2036) 
    at javafx.scene.Scene$ScenePeerListener.keyEvent(Scene.java:2493) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:170) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$KeyEventNotification.run(GlassViewEventHandler.java:123) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleKeyEvent(GlassViewEventHandler.java:197) 
    at com.sun.glass.ui.View.handleKeyEvent(View.java:517) 
    at com.sun.glass.ui.View.notifyKey(View.java:927) 
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
    at com.sun.glass.ui.win.WinApplication.access$300(WinApplication.java:39) 
    at com.sun.glass.ui.win.WinApplication$4$1.run(WinApplication.java:112) 
    at java.lang.Thread.run(Thread.java:745) 
+0

是NPE所在的位置。張貼錯誤 –

+0

你在哪裏得到的NPE? – Jens

+1

你的'textbox'或者'custCtrl'可以是null..Debug it in Eclipse .. –

回答

0

你可能得到NPE在這裏:

if (ov.getValue().toString().length() > 0) { 

     String txtdata = (textbox.getText()).trim(); 

試圖檢查空指針第一:

if (null != ov && null != ov.getValue() && ov.getValue().toString().length() > 0) { 
    if(null != (textbox) && null != textbox.getText(){ 
       String txtdata = (textbox.getText()).trim(); 
    } 
相關問題