我实例化对象时的对象引用错误

时间:2011-11-22 20:47:21

标签: c# object

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);
    }
}

1 个答案:

答案 0 :(得分:1)

我怀疑您的某个配置设置丢失了。检查它们是否全部存在且正确。请注意,对空值的ToString调用会导致您看到的异常。