2012-04-16 30 views
0

在错误的墙上撞我的头对象引用未设置为对象的实例。 请参阅下面的代码。该问题的行标有<!--- >>>>>>>如何解决未设置为对象实例的对象引用。

string htmlOutput = " "; 

    htmlOutput += "<h3>employee: " + employee.lastName + ", " + employee.firstName + " (" + thisForm.employeeID + ")</h3>" + 
     "<h3>Submitted To: " + thisForm.boss + "</h3>" + 

    if (job1.JRN != "" || job1.JRN != "INVALID") 
    { 
     <!--->>>>>>>htmlOutput += "<hr /><h4>Job 1</h4> " + "<h3>" + database.getjobName(job1.JRN) + " (" + job1.JRN + ")</h3>"; 
     htmlOutput += "<h3>Date of Last Attendance: " + job1.month + " " + job1.day + ", " + job1.year + "</h3>"; 
     htmlOutput += "<h3>Pass/Fail: " + job1.passFail + "</h3>"; 
    } 
+1

你在哪里定义'database'并初始化它? – Servy 2012-04-16 15:34:44

+0

调试你的代码,看代码到达那一行时“数据库”是否真的是一个对象引用。错误代码使得它很清楚什么是错误的,我们需要看到更多的代码来帮助你。 – 2012-04-16 15:37:24

+0

在做这么多字符串连接时,你应该真的使用'StringBuilder'。 – Servy 2012-04-16 15:42:48

回答

2

使用String.Format("{0}{1}", obj1, obj2 ?? "")。并使用调试器,将断点放在此行并检查对象,然后添加验证逻辑。也许问题出在database.getjobName方法代码中。您始终可以使用null coalescing operator。不要像你的例子那样使用字符串连接,这很丑陋,容易出错。

相关问题