简体   繁体   English

在Java中另一个类中访问静态变量时发生NullPointerException

[英]NullPointerException when accessing static variable in another class in Java

I have trying to access a static variable of class A in class B, however I am getting NullPointerException. 我试图访问类B中的类A的静态变量,但是我遇到了NullPointerException。 The code is : 代码是:

public class OutgoingMessage {
    public static SMPPSession session;
    public static void main(String [] args)
    {
         session = new SMPPSession();

    }
  }

And

public class SendSMS {
    public static void main(String [] args)
    {
      if(OutgoingMessage.session.getSessionState().toString().equals("Connected"))//Line 44 
        {
        }
    } 
}

The error reads 错误读取

Exception in thread "main" java.lang.NullPointerException
        at SendSMS.main(SendSMS.java:44)

Any idea what am I missing ? 知道我想念什么吗?

Thanks 谢谢

Satya 萨蒂亚

Sure - you're using OutgoingMessage.session , which will be null unless you've also run OutgoingMessage.main . 当然-您正在使用OutgoingMessage.session ,除非您运行OutgoingMessage.main否则它将为null。 It's not like main methods get invoked automatically everywhere - that's just the entry point for the application. 并不是像在任何地方都自动调用main方法一样,这只是应用程序的入口。

I suggest that instead of changing this to use a static initializer or something like that, you try to work to avoid static variables. 我建议不要尝试使用静态初始化程序或类似方法,而应尝试避免使用静态变量。

Why would it make sense for OutgoingMessage to have a static session variable? 为什么OutgoingMessage具有静态session变量才有意义? I'd expect the two to work together, not one be composed of the other... for example, I could imagine: 我希望两者能够协同工作,而不是由另一个构成……例如,我可以想象:

SMPPSession session = new SMPPSession();
session.send(outgoingMessage);

or even: 甚至:

SMPPSession session = new SMPPSession();
outgoingMessage.sendInSession(session);

OutgoingMessage.session is null at the point where you are calling OutgoingMessage.session在您呼叫的位置为null

if(OutgoingMessage.session.getSessionState()

Which results in NullPointerException . 结果为NullPointerException

Make sure OutgoingMessage class main method is executed before making if(OutgoingMessage.session.getSessionState() 确保在执行if(OutgoingMessage.session.getSessionState()之前执行OutgoingMessagemain方法

Unless you use OutgoingMessage as main class, it doesn't make sense to define main method there, which confuses more. 除非您将OutgoingMessage用作主类,否则在此处定义main方法是没有意义的,这会造成更多混乱。

You can add OutgoingMessage.main(args); 您可以添加OutgoingMessage.main(args); before: 之前:

if(OutgoingMessage.session.getSessionState().toString().equals("Connected")) //Line 44 ...

您在OutgoingMessage中缺少静态的getter方法,并且只需要一个main方法。

The problem is when you use OutgoingMessage.session it is not yet initialized. 问题是,当您使用OutgoingMessage.session它尚未初始化。 To correctly initialize it do: 要正确初始化它,请执行以下操作:

public class OutgoingMessage {
    public static SMPPSession session = new SMPPSession();
  }

You haven't initialized your static variable.. That's why.. 您尚未初始化静态变量。这就是原因。

Actually you have done that in main() , but that will really not affect your output.. As your main will never be run.. You can initialize it at the place of declaration only.. 实际上,您已经在main()完成了该操作,但这实际上不会影响您的输出。.因为您的main将永远不会运行。.您只能在声明的位置对其进行初始化。

public static SMPPSession session = new SMPPSession();

Before main method.. But it doesn't make sense to have it as static variable.. 在主方法之前..但是将其作为静态变量没有任何意义。

You should declare it as instance variable and initialize it for each instance you create using a Constructor .. 您应该将其声明为实例变量,并为每个使用Constructor创建的实例初始化它。

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

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