[英]How to pass Value from one class to other class in Java?
I am trying to send a string value from Class1 to Class2 but I am getting Nullpointer Exceiption I have imported class1 in class2. 我正在尝试将一个字符串值从Class1发送到Class2,但是我得到了Nullpointer Exceiption,我已经在class2中导入了class1。
Class 2 Code -: 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);
}
}
Here custCtrl is Class 1 object and txtVal2() is method receiving string data (sample code below) 这里custCtrl是1类对象,而txtVal2()是接收字符串数据的方法(下面的示例代码)
Class 1 Code -: 第1类代码-:
public String txtVal2(String a)
{
System.out.println("Getting text box value===>"+a);
return "ok";
}
Error -: 错误-:
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)
You are possibly getting NPE here: 您可能在这里获得NPE:
if (ov.getValue().toString().length() > 0) {
String txtdata = (textbox.getText()).trim();
trying checking for null pointers first: 首先尝试检查空指针:
if (null != ov && null != ov.getValue() && ov.getValue().toString().length() > 0) {
if(null != (textbox) && null != textbox.getText(){
String txtdata = (textbox.getText()).trim();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.