C# 实例化对象时出现对象引用错误
我得到了一个_程序的对象引用错误,我无法弄清楚。能帮帮我吗 代码如下:C# 实例化对象时出现对象引用错误,c#,object,C#,Object,我得到了一个_程序的对象引用错误,我无法弄清楚。能帮帮我吗 代码如下: private void PerformWork(object state) { try { MMS.MMSService.Console.Program _program = new MMS.MMSService.Console.Program(); _program.OUIProcess();
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);
}
}
公共类程序
{
//要使用的连接类型,可以是:-
//服务器连接的MQC.TRANSPORT_MQSERIES_绑定。
//用于非XA客户端连接的MQC.TRANSPORT_MQSERIES_客户端
//MQC.TRANSPORT_MQSERIES_XACLIENT用于XA客户端连接
//MQC.TRANSPORT_MQSERIES_为托管客户端连接管理
const String connectionType=MQC.TRANSPORT\u MQSERIES\u CLIENT;
//定义要使用的队列管理器的名称(适用于所有连接)
静态字符串qManager=ConfigurationManager.AppSettings[“qManager”].ToString();
//定义主机连接的名称(仅适用于客户端连接)
静态字符串hostName=ConfigurationManager.AppSettings[“hostName”].ToString();
//定义要使用的通道的名称(仅适用于客户端连接)
静态字符串通道=ConfigurationManager.AppSettings[“通道”].ToString();
公共计划()
{
//要使用的连接类型,可以是:-
//服务器连接的MQC.TRANSPORT_MQSERIES_绑定。
//用于非XA客户端连接的MQC.TRANSPORT_MQSERIES_客户端
//MQC.TRANSPORT_MQSERIES_XACLIENT用于XA客户端连接
//MQC.TRANSPORT_MQSERIES_为托管客户端连接管理
const String connectionType=MQC.TRANSPORT\u MQSERIES\u CLIENT;
//定义要使用的队列管理器的名称(适用于所有连接)
字符串qManager=ConfigurationManager.AppSettings[“qManager”].ToString();
//定义主机连接的名称(仅适用于客户端连接)
字符串hostName=ConfigurationManager.AppSettings[“hostName”].ToString();
//定义要使用的通道的名称(仅适用于客户端连接)
字符串通道=ConfigurationManager.AppSettings[“通道”].ToString();
}
静态哈希表init()
{
Hashtable connectionProperties=新的Hashtable();
添加(MQC.TRANSPORT\u属性,connectionType);
添加(MQC.HOST\u NAME\u属性,主机名);
添加(MQC.CHANNEL_属性,CHANNEL);
返回connectionProperties;
}
///
///应用程序的主要入口点。
///
公共void OUIProcess()
{
尝试
{
NRTManager ouiManager=新的NRTManager();
Hashtable connectionProperties=init();
//使用连接创建到队列管理器的连接
//刚定义的属性
MQQueueManager qMgr=新的MQQueueManager(qManager,connectionProperties);
//在要打开的队列上设置选项
int openOptions=MQC.MQOO_INPUT_SHARED | MQC.MQGMO_BROWSE_FIRST;
//现在指定要打开的队列和打开选项
MQQueue系统\默认\本地\队列=
qMgr.AccessQueue(ConfigurationManager.AppSettings[“queue”].ToString(),openOptions);
//首先定义一个WebSphereMQ消息缓冲区来接收消息
MQMessage retrievedMessage=新MQMessage();
retrievedMessage.MessageId=MQC.MQMI_NONE;
//设置获取消息选项
MQGetMessageOptions gmo=new MQGetMessageOptions();//接受默认值
//从队列中取出消息
系统\默认\本地\队列.Get(retrievedMessage,gmo);
//通过显示UTF消息文本来证明我们拥有该消息
string msgText=retrievedMessage.ReadString(retrievedMessage.DataLength);
XmlDocument doc=新的XmlDocument();
doc.LoadXml(msgText);
ouiManager.WriteToOUIdb(0,“消息拉式”,文档,配置管理器.ConnectionString[“OUI”]。ConnectionString,配置管理器.ConnectionString[“MMS”]。ConnectionString);
//关闭队列
system_default_local_queue.Close();
//断开与队列管理器的连接
qMgr.Disconnect();
}
//如果上述操作中出现错误,请尝试找出错误所在。
//这是WebSphere MQ错误吗?
捕获(MQException-ex)
{
日志记录器=新日志记录();
logger.WriteToLog(1,例如ToString(),null,null,ConfigurationManager.ConnectionString[“MMS”].ConnectionString);
}
catch(System.Exception-ex)
{
日志记录器=新日志记录();
logger.WriteToLog(1,例如ToString(),null,null,ConfigurationManager.ConnectionString[“MMS”].ConnectionString);
}
}//开始结束
公共静态void ValidationHandler(对象发送方,ValidationEventArgs e)
{
//WriteToLog(1,例如ToString(),null,null);
//控制台写入线(e.Message);
}
}
我怀疑您的一个配置设置丢失。检查它们是否都存在且正确。请注意,对空值调用ToString
将导致您看到的异常。能否发布准确的错误消息和异常的完整堆栈跟踪?我没有定义构造函数。所以它是默认值。@OBL您有字段初始值设定项吗?静态构造函数?有那样的事吗?程序
有什么有趣的地方吗?它是远程代理吗?任何形式的代理人等等?@Marc你能告诉我们如何故意恶意吗?我想向团队介绍Null构造函数
模式。@Chris请参阅-忽略Null的
,这是一个骗局。它适用于常规的new
,Activator.CreateInstance
,以及T:new()
约束。但是,由于还有静态字段,因此我们应该期望它引发类型初始化异常
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);
}
}