如何将值从一个类传递到Java中的其他类?

时间:2014-06-25 05:49:29

标签: java javafx

我正在尝试从Class1向Class2发送字符串值,但我得到的是Nullpointer Exceiption 我在class2中导入了class1。

第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是Class 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)

1 个答案:

答案 0 :(得分: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();
    }