简体   繁体   English

具有单例侦听器的简单示例上的空指针异常

[英]null pointer exception on simple example of listener with singleton

i have here a vary simple example of using a singleton with a custom listener. 我在这里有一个使用自定义侦听器的单例的简单示例。 i had a simmilar problem with some other code and decided to make this example. 我在使用其他代码时遇到了类似的问题,因此决定制作此示例。 it is showing the same null pointer problem as the other more complex code. 它显示了与其他更复杂的代码相同的空指针问题。

null pointer is on this line: 空指针在此行上:

  listener.onPass(sendString);

any idea how to fix the problem, and why it is happening? 任何想法如何解决问题,以及为什么会发生?

three classes shown here, StartSelector.java is used to start the other class, Singleton.java class has a listener interface and a singleton, it sends a short text message to the other class, Singleton2.java, this other class implements the callback method and receives the text message String. 这里显示了三个类,StartSelector.java用于启动另一个类,Singleton.java类具有一个侦听器接口和一个Singleton,它向另一个类Singleton2.java发送一条短信,该另一个类实现了回调方法并接收文本消息String。

starts the Singleton class 开始单例课程

 public class StartSelector {

      public static void main(String args[]) {
      Singleton.getInstance();
      } // end main

 }

sends off String message by using custom listener 通过使用自定义侦听器发送字符串消息

 public class Singleton {

 private OnPassStringListener listener;
 private static Singleton instance;

 public static Singleton getInstance(){
      if(instance == null){
               instance = new Singleton();
           }
      }
   return instance;
 }

 private Singleton(){
     doIt();
 }

 private void doIt(){
     transmitString("test string from singleton class");
 }

 public void transmitString(String sendString) {
     listener.onPass(sendString);   <-- NULL POINTER EXCEPTION HERE
 }

 public interface OnPassStringListener {
     public void onPass(String stringSend);
 }    

 } // end class Singleton

receives the String message from the other class and implements the callback method for the interface 从其他类接收String消息,并为接口实现回调方法

public class Singleton2 implements Singleton.OnPassStringListener {

   public Singleton2() {
       System.out.println("singleton2 class started");
   }

    @Override
    public void onPass(String stringSend) {
        System.out.println("message received: " + stringSend);
    }

} // end class singleton2

You haven't initialized the listener in that code. 您尚未在该代码中初始化侦听器。 A NullPointerException just means that the object that you're attempting to use doesn't point to a valid object. NullPointerException仅意味着您尝试使用的对象没有指向有效的对象。 Either initialize the value when you define it, or in the class constructor. 在定义值时或在类构造函数中初始化值。

private OnPassStringListener listener = new Singleton2();

or 要么

private Singleton() {
    listener = new Singleton2();
    doIt();
}
public static Singleton getInstance(){
      if(instance == null){
               instance = new Singleton();
           instance.setListener(new Singleton2());
           }
      }
   return instance;
 }

Two options. 两种选择。 Either listener is null, or NullPointerException is thrown by some code INSIDE onPass(). 侦听器为null,或者某些代码INSIDE onPass()引发NullPointerException。 Need to see stack dump to know which one it is. 需要查看堆栈转储以了解它是哪一个。 (Edit. Never mind, I missed the text of Singleton2. So yes, it's definitely the former one. Your listener at the first singleton is never initialized. You want to initialize it, and/or check for null. (编辑。没关系,我错过了Singleton2的文本。所以是的,它肯定是前一个。您的第一个Singleton的listener器永远不会初始化。您想对其进行初始化,和/或检查是否为null。

Technically, it might be a good programming style to decouple Singleton and Singleton2. 从技术上讲,解耦Singleton和Singleton2可能是一种很好的编程风格。 In the Observer pattern (at which the word 'listener' hints), the listenee does not know its listener (other than the very generic fact that it knows it has a listener). 在观察者模式(单词“ listener”暗示)中,被监听者不知道其监听器(除了非常普遍的事实,即它知道一个监听器)。 So, if you directly refer to Singleton2 from Singleton1, maybe call it something other than Listener. 因此,如果您直接从Singleton1引用Singleton2,则可以将其命名为Listener以外的名称。

Your listener is null. 您的听众为空。 Change this line 更改此行

private OnPassStringListener listener;

to

private OnPassStringListener listener = new Singleton2();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM