Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am having the above error don't know what it is causing it my code is the following

VB
Protected Sub DataList_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs)
        'file and attachments to determine
        Dim fileName As String = String.Empty
        Dim attachment1 As String = ""
        Dim attachment2 As String = ""
        Dim attachment3 As String = ""
        'if the event item is the DataList header template, do apropriate linking/label changing
        If (e.Item.ItemType = ListItemType.Header) Then
            Dim acadYearIndex As Integer = ddAcadYear.SelectedValue
            Dim OurString As String = ConfigurationManager.ConnectionStrings("ConnectionStringSenate").ConnectionString
            Dim sqlSelect As String = "select AY_YEARS from ACAD_YEAR where AY_ID =" + ddAcadYear.SelectedValue
            Using OurConnection As New OracleConnection(OurString)
                Using OurCommand As New OracleCommand(SQLSelect, OurConnection)
                    OurConnection.Open()
                    Dim yearRange As String = OurCommand.ExecuteScalar()
                    Dim years As Array = yearRange.Split("-")
                    Dim yearLabel As Label = CType(e.Item.FindControl("year"), Label)
                    If (e.Item.ClientID.Contains("fallFiles") Or e.Item.ClientID.Contains("summerFiles")) Then
                        yearLabel.Text = years(0)
                    Else
                        yearLabel.Text = years(1)
                    End If
                End Using
            End Using
        End If
				
				Dim fileLink As HyperLink
				Dim lbl As Label
		try
        'if the event item is the DataList item template, do apropriate linking/label changing
        'System.Diagnostics.Debug.WriteLine("##### PreFileCheck: " + e.Item.ItemType.ToString)
        If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
            fileName = e.Item.DataItem("EC_ID").ToString() & "_" 
						fileName &= Date.Parse(e.Item.DataItem("DOC_DATE").ToString()).ToString("d").Replace("/", "")
						
            attachment1 = fileName + "_1"
            attachment1 = "docs/" + attachment1 + MC.getExtension(attachment1, "docs/")
			
            attachment2 = fileName + "_2"
            attachment2 = "docs/" + attachment2 + MC.getExtension(attachment2, "docs/")
			
            attachment3 = fileName + "_3"
            attachment3 = "docs/" + attachment3 + MC.getExtension(attachment3, "docs/")
VSF_Addition.Text = Attachment1	& "<br />"
VSF_Addition.Text &= Attachment2	& "<br />"
VSF_Addition.Text &= Attachment3	& "<br />"

            System.Diagnostics.Debug.WriteLine("##### new file name: " + fileName)
			
            'check if main file exists
            If File.Exists(Server.MapPath("docs/" + fileName + ".pdf")) Then
                System.Diagnostics.Debug.WriteLine("**File exists: " + fileName)
                'cast sender to HyperLink
                fileLink = CType(e.Item.FindControl("fileTitleLink"), HyperLink)
                fileLink.NavigateUrl = "docs/" + fileName + ".pdf"
            End If
			
            'integer used to dynamicly number the attachments displayed
            Dim attachmentCount As Integer = 1
            'check for attachments
            'check for attachment 1
            If File.Exists(Server.MapPath(attachment1)) Then
                System.Diagnostics.Debug.WriteLine("**File exists: " + attachment1)
                'cast sender to HyperLink
                fileLink = CType(e.Item.FindControl("attachment1Link"), HyperLink)
                'set navigation url
                fileLink.NavigateUrl = attachment1
                fileLink.Text = "Attachment " + attachmentCount.ToString()
                attachmentCount += 1
                'find attachment 1 label and display it
                lbl = CType(e.Item.FindControl("showAttach1"), Label)
                lbl.Visible = "true"
            End If
			
            'check for attachment 2
            If File.Exists(Server.MapPath(attachment2)) Then
                System.Diagnostics.Debug.WriteLine("**File exists: " + attachment2)
                'cast sender to HyperLink
                fileLink = CType(e.Item.FindControl("attachment2Link"), HyperLink)
                'set navigation url
                fileLink.NavigateUrl = attachment2
                fileLink.Text = "Attachment " + attachmentCount.ToString()
                attachmentCount += 1
                'find attachment 2 lable and display it
                lbl = CType(e.Item.FindControl("showAttach2"), Label)
                lbl.Visible = "true"
            End If
			
            'check for attachment 3
            If File.Exists(Server.MapPath(attachment3)) Then
                System.Diagnostics.Debug.WriteLine("**File exists: " + attachment3)
                'cast sender to HyperLink
                fileLink = CType(e.Item.FindControl("attachment3Link"), HyperLink)
                'set navigation url
                fileLink.NavigateUrl = attachment3
                fileLink.Text = "Attachment " + attachmentCount.ToString()
                'find attachment 3 label and display it
                lbl = CType(e.Item.FindControl("showAttach3"), Label)
                lbl.Visible = "true"
            End If
        End If
Catch ex As Exception
		LBL_ERROR_MSG.Text &= String.Format("ItemType:{0}<br />", e.Item.ItemType.ToString())
		LBL_ERROR_MSG.Text &= String.Format("filename:{0}<br />", fileName)
		LBL_ERROR_MSG.Text &= "__________ ERROR __________<br />" & ex.message.ToString() & "<br />"
end try				
    End Sub

Public Function getExtension(ByVal fileName As String, ByVal dir As String) As String
       'System.Diagnostics.Debug.WriteLine(Server.MapPath("../docs/"))
       Dim di As DirectoryInfo = New DirectoryInfo(HttpContext.Current.Server.MapPath(dir))
       Dim ext As Array = di.GetFiles(fileName + "*")
       If ext.Length > 0 Then
           Return CType(ext(0), FileInfo).Extension.ToString()
       End If
       'no file extension woas found, return .error
       Return ".err"
   End Function


does anybody where is the problem...??
Posted
Updated 4-Sep-15 11:50am
v2
Comments
Afzaal Ahmad Zeeshan 4-Sep-15 17:44pm    
Something is null, debug your application.
linabunny 4-Sep-15 17:46pm    
__________ ERROR __________
Object reference not set to an instance of an object.
ItemType:Item
filename:135_9222010
__________ ERROR __________
Object reference not set to an instance of an object.
ItemType:Item
filename:147_392011
__________ ERROR __________
Object reference not set to an instance of an object.
ItemType:AlternatingItem
filename:146_322011
__________ ERROR __________
Object reference not set to an instance of an object.
ItemType:Item
filename:145_222011
__________ ERROR __________
Object reference not set to an instance of an object.
ItemType:AlternatingItem
filename:144_1262011
__________ ERROR __________
Object reference not set to an instance of an object.
ItemType:Item
filename:143_1122011
__________ ERROR __________
Object reference not set to an instance of an object.
ItemType:AlternatingItem
filename:142_152011
__________ ERROR __________
Object reference not set to an instance of an object.
ItemType:Item
filename:151_5112011
__________ ERROR __________
Object reference not set to an instance of an object.
ItemType:AlternatingItem
filename:150_4272011
__________ ERROR __________
Object reference not set to an instance of an object.
ItemType:Item
filename:149_462011
__________ ERROR __________
Object reference not set to an instance of an object.
ItemType:AlternatingItem
filename:148_3302011
__________ ERROR __________
Object reference not set to an instance of an object.
linabunny 4-Sep-15 17:47pm    
This is the output that i get
Afzaal Ahmad Zeeshan 4-Sep-15 17:49pm    
You should set some breakpoints and see what causes the error. Then you will be able to solve it.
linabunny 4-Sep-15 17:58pm    
thats what i did with the try and catch

We can't run your code because it depend on external files that we don't have.
So we can only give you hints about what to do.

Your Try/Catch structure prevent you from seeing where is the error.
Put the Try/Catch in remark and fire your code.
It will throw an error and propose the debugger.
When in debugger, note where is the error, view the local variables, the offending variable do not contain what you expect.
 
Share this answer
 
You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requires this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object".

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx,
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx.

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx.

Good luck,
—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900