实例化对象时出现对象引用错误

本文关键字:对象引用 错误 对象 实例化 | 更新日期: 2023-09-27 18:15:23

private void PerformWork(object state)
    {
        try
            {
                MMS.MMSService.Console.Program _program = new MMS.MMSService.Console.Program();
                _program.OUIProcess();
            }
            catch (Exception ex)
            {
                eventLog1.WriteEntry(ex.Message);
            }
        }

我得到_program的对象引用错误,我无法弄清楚。请帮我一下好吗?

代码如下:

public class Program
{
    // The type of connection to use, this can be:-
    // MQC.TRANSPORT_MQSERIES_BINDINGS for a server connection.
    // MQC.TRANSPORT_MQSERIES_CLIENT for a non-XA client connection
    // MQC.TRANSPORT_MQSERIES_XACLIENT for an XA client connection
    // MQC.TRANSPORT_MQSERIES_MANAGED for a managed client connection
    const String connectionType = MQC.TRANSPORT_MQSERIES_CLIENT;
    // Define the name of the queue manager to use (applies to all connections)
    static String qManager = ConfigurationManager.AppSettings["qManager"].ToString();
    // Define the name of your host connection (applies to client connections only)
    static String hostName = ConfigurationManager.AppSettings["hostName"].ToString();
    // Define the name of the channel to use (applies to client connections only)
    static String channel = ConfigurationManager.AppSettings["channel"].ToString();
    public Program()
    {
        // The type of connection to use, this can be:-
        // MQC.TRANSPORT_MQSERIES_BINDINGS for a server connection.
        // MQC.TRANSPORT_MQSERIES_CLIENT for a non-XA client connection
        // MQC.TRANSPORT_MQSERIES_XACLIENT for an XA client connection
        // MQC.TRANSPORT_MQSERIES_MANAGED for a managed client connection
        const String connectionType = MQC.TRANSPORT_MQSERIES_CLIENT;
        // Define the name of the queue manager to use (applies to all connections)
        String qManager = ConfigurationManager.AppSettings["qManager"].ToString();
        // Define the name of your host connection (applies to client connections only)
        String hostName = ConfigurationManager.AppSettings["hostName"].ToString();
        // Define the name of the channel to use (applies to client connections only)
        String channel = ConfigurationManager.AppSettings["channel"].ToString();
    }
    static Hashtable init()
    {
        Hashtable connectionProperties = new Hashtable();
        connectionProperties.Add(MQC.TRANSPORT_PROPERTY, connectionType);
        connectionProperties.Add(MQC.HOST_NAME_PROPERTY, hostName);
        connectionProperties.Add(MQC.CHANNEL_PROPERTY, channel);
        return connectionProperties;
    }
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    public void OUIProcess()
    {
        try
        {
            NRTManager ouiManager = new NRTManager();
            Hashtable connectionProperties = init();
            // Create a connection to the queue manager using the connection
            // properties just defined
            MQQueueManager qMgr = new MQQueueManager(qManager, connectionProperties);
            // Set up the options on the queue we want to open
            int openOptions = MQC.MQOO_INPUT_SHARED  | MQC.MQGMO_BROWSE_FIRST;
            // Now specify the queue that we want to open,and the open options
            MQQueue system_default_local_queue =
            qMgr.AccessQueue(ConfigurationManager.AppSettings["queue"].ToString(), openOptions);
            // First define a WebSphere MQ message buffer to receive the message
            MQMessage retrievedMessage = new MQMessage();
            retrievedMessage.MessageId = MQC.MQMI_NONE;
            // Set the get message options
            MQGetMessageOptions gmo = new MQGetMessageOptions(); //accept the defaults
            // Get the message off the queue
            system_default_local_queue.Get(retrievedMessage, gmo);
            // Prove we have the message by displaying the UTF message text
            string msgText = retrievedMessage.ReadString(retrievedMessage.DataLength);
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(msgText);
            ouiManager.WriteToOUIdb(0, "Message Pulled", doc, ConfigurationManager.ConnectionStrings["OUI"].ConnectionString, ConfigurationManager.ConnectionStrings["MMS"].ConnectionString);
            //Close the queue
            system_default_local_queue.Close();
            // Disconnect from the queue manager
            qMgr.Disconnect();
        }
        //If an error has occurred in the above,try to identify what went wrong.
        //Was it a WebSphere MQ error?
        catch (MQException ex)
        {
            Logging logger = new Logging();
            logger.WriteToLog(1, ex.ToString(), null, null, ConfigurationManager.ConnectionStrings["MMS"].ConnectionString);
        }
        catch (System.Exception ex)
        {
            Logging logger = new Logging();
            logger.WriteToLog(1, ex.ToString(), null, null, ConfigurationManager.ConnectionStrings["MMS"].ConnectionString);
        }
    }//end of start
    public static void ValidationHandler(object sender, ValidationEventArgs e)
    {
        //WriteToLog(1, e.ToString(), null, null);
        //Console.WriteLine(e.Message);
    }
}

实例化对象时出现对象引用错误

我怀疑您的一个配置设置丢失了。检查它们是否都存在并且正确。注意,对null值的ToString调用将导致您所看到的异常。