简体   繁体   English

如何在Java中将Value从一个类传递到另一类?

[英]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.

相关问题 无法将字符串值从一个Java类传递给另一个 - Unable to pass String value from one java class to other 如何将值从一类方法传递到另一类方法。 其他方法中使用了哪一种? - How to pass value from a method of one class to the method of other class. Which is used in some other method? 如何在不重复的情况下将列表中的存储值从一个类传递到另一个类 - how to pass stored values in list from one class to other class without duplication in java 如何在 java 的同一项目中将字符串数组从一个 class 传递到另一个 class - how to pass array of strings from one class to other class with in same project in java 如何将实例值从一个类传递到另一个包含测试注释方法的类 - how to pass the instance value from one class the other class containing test annotated method 如何将 JSpinner 值从 Java class 传递给另一个 Java class - How to pass JSpinner value from Java class to another Java class 如何将值从 HTML 传递到 Java class - How to pass a value from HTML to Java class 如何将Java对象从一个类传递到另一个类 - How to pass Java objects from one class to another class 如何将值从一个 class 的 onOptionsItemSelected 方法传递到另一个 class - How to pass value from onOptionsItemSelected method of one class to another class 如何将值从一个类传递到另一类GUI - How to pass value from one class to another class GUI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM