Friday, March 16, 2012

Object reference not set to an instance of an object.

I am new to ASP.NET no other programminglanguage skills besides some PHP :D. So this is a pretty shitty error in my opinion. Anyway. I get the error on line 49 wich is:
rptNewsOverzicht.DataSource = dtrNews

My whole error is:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 47: 'Repeater binden aan de datareader
Line 48: Dim rptNewsOverzicht As Repeater
Line 49: rptNewsOverzicht.DataSource = dtrNews
Line 50: rptNewsOverzicht.DataBind()
Line 51:

Source File: c:\inetpub\wwwroot\Quipment_backend\NewsOverzicht.aspx.vb Line: 49

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
Quipment_backend.WebForm2.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\Quipment_backend\NewsOverzicht.aspx.vb:49
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()

This is the entire code of my codebehing page_load sub:

Imports System.Data.SqlClient

Public Class WebForm2
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Verbinding met de database maken
Dim conNews As SqlConnection
Dim ConnectionString As String = "Server=(local);uid=ASPNET_user;pwd=quipment04;database=Quipment;Trusted_Connection=true;"

'Command verwijzen naar de News tabel
Dim cmdSelectNews As SqlCommand
Dim SqlString As String = "SELECT NewsID, Titel, Categorie, PubMainpage, Datum FROM tblNews"

'Datareader die de gegevens gaan bevatten
Dim dtrNews As SqlDataReader

'verbinding leggen en openen
conNews = New SqlConnection(ConnectionString)
conNews.Open()

'Statement uitvoeren op de connectie
cmdSelectNews = New SqlCommand(SqlString, conNews)

'Datareader aanmaken
dtrNews = cmdSelectNews.ExecuteReader()

'Repeater binden aan de datareader
Dim rptNewsOverzicht As Repeater
rptNewsOverzicht.DataSource = dtrNews
rptNewsOverzicht.DataBind()

'Connectie en reader sluiten
dtrNews.Close()
conNews.Close()
End Sub

End Class

Does anybody see what i am doing wrong?

PS. I know I can optimize some things but I am just not that ready for it :DOk, what that error means is that you a referencing a object in memory that has been created. In the line:

Dim rptNewsOverzicht As Repeater

you have allocated space in memory for that type of object, but it hasn't actually be created yet. If you change the line to this, it should work.

Dim rptNewsOverzicht As New Repeater

This allocates the memory as well as creates the object. This is the same as typing this:

Dim rptNewsOverzicht As Repeater
rptNewsOverzicht = New Repeater

Hope this clarifies the error message. The is a correct error message pertaining to the problem in the code. Anytime you get that error message it is because it is trying to access an object that hasn't been created yet. Only the memory has been allocated to it.

0 comments:

Post a Comment