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

Hi guys, I get the following error, can you help me to figure out the problem

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

[NullReferenceException: Object reference not set to an instance of an object.]
  mydefault.Button_Click(Object s, EventArgs e) +102
  System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
  System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
  System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
  System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
  System.Web.UI.Page.ProcessRequestMain() +1292

Here is my code

Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Data
Imports System.Data.SqlClient

Public Class mydefault
Inherits Page

Private strConnect As String = "Server=Localhost;uid=datalink;pwd=ezrider;database=Log"

Private strQuery As String = "SELECT * FROM DocTable1 WHERE FREETEXT("

Private DBconnection As SqlConnection
Private cmdSelect As SqlCommand
Private sqldata As SqlDataReader

Protected WithEvents kword As TextBox
Protected WithEvents program As DropDownList
Protected WithEvents paper As DropDownList
Protected WithEvents fieldselect1 As DropDownList
Protected WithEvents ANNO As DropDownList
Protected WithEvents FT As CheckBox
Protected WithEvents recordspage As DropDownList
Protected WithEvents Button1 As Button

Protected Sub Button_Click(s As Object, e As EventArgs)

startConnection()

selectCriterion()
strQuery =strQuery + "@kw )"

cmdselect.Parameters.Add("@kw","smith")

selectOtherCriteria()

cmdSelect = New SqlCommand(strQuery, DBconnection)
cmdselect.ExecuteReader()
showResults()

DBconnection.Close()
End Sub

Private Sub showResults()

while sqldata.Read()
Response.Write("<li>")
Response.Write(sqldata("Title"))
End While
sqldata.Close()
End Sub

Private Sub startConnection()

DBconnection = new SqlConnection ( strConnect )
DBconnection.Open()
End Sub

Private Sub selectCriterion()

Select Case fieldselect1.SelectedValue
case "-"
strQuery =strQuery +" Keywords, "
case "sk"
strQuery =strQuery +" Keywords, "
case "dn"
strQuery =strQuery +" DocNumber, "
case "aln"
strQuery =strQuery +" LastName, "
case "tw"
strQuery =strQuery +" Title, "
case "l"
strQuery =strQuery +" Location, "
End Select
End Sub

Private Sub selectOtherCriteria()

if program.SelectedValue <> "cp" Then
strQuery =strQuery +" AND Program=@prog"
cmdselect.Parameters.Add("@prog",program.SelectedValue )
End If

if paper.SelectedValue <> "cpt" Then
strQuery =strQuery +" AND Paper=@paper"
cmdselect.Parameters.Add("@paper",paper.SelectedValue )
End If

if ANNO.SelectedValue <> "cpy" Then
strQuery =strQuery +" AND ANNO=@anno"
cmdselect.Parameters.Add("@anno",ANNO.SelectedValue )
End If

if FT.Checked Then
strQuery =strQuery +"AND FT='Y'"
Else
strQuery =strQuery +" AND FT='N'"
End If
End Sub

End Class

--------------------------------------------------------------------------------------------

<%@ Page Language="vb" Inherits="mydefault" src="mydefault.vb" Trace="true" Debug="true"%>
<!-- #INCLUDE FILE="banner.asp" -->

<body onLoad=document.aform.keyword.focus(); >
<P>
<table class="TableSet">
<tr><td align="right" valign="top">
<asp:Hyperlink id="SubjectLink" NavigateUrl="subject.asp" Text="Subject Search" Runat="Server"/> | <asp:Hyperlink id="LogOutLink" NavigateUrl="http://www.nova.edu/library/eleclib/login_required.html" Text="Log out" Runat="Server"/></td></tr>
</table>
<center>
<P></P>
<table class="SilverTable">

<form Runat="Server">
<tr><td colspan="2">
<asp:Label
ID="Kwlabel"
Text="Keyword Search"
Font-Bold="True"
ForeColor="navy"
Runat="Server"/>
<br><br>
</td>
</tr>
<tr>
<td height="16" align="left" width="500">
<asp:TextBox
id="kword"
TextMode="SingleLine"
MaxLength="50"
Columns="81"
Runat="Server"/>
<asp:RequiredFieldValidator
ControlToValidate="kword"
Text="You must enter some text!"
Runat="Server"/>

<asp:RegularExpressionValidator
ControlToValidate="kword"
Text="Use only letters, numbers and '"
ValidationExpression="^[a-zA-Z0-9'-.\s]{1,80}"
Runat="Server"/>
</td>
<td width="100" valign="top">
<asp:DropDownList
ID="fieldselect1"
Runat="Server">
<asp:ListItem
Text="Select a search criterion"
Value="-" />
<asp:ListItem
Text="Subject Keywords"
Value="sk" />
<asp:ListItem
Text="Document Number"
Value="dn" />
<asp:ListItem
Text="Author Last Name"
Value="aln" />
<asp:ListItem
Text="Title Keywords"
Value="tw" />
<asp:ListItem
Text="Location"
Value="l" />
</asp:DropDownList>
</td>
</tr>
</table>
<table class="SilverTable">
<td class="DropDownClass">
<asp:DropDownList
ID="program"
Runat="Server">
<asp:ListItem
Text="Choose a program"
Value="cp"/>
<asp:ListItem
Text="CDS"
Value="CDS"/>
<asp:ListItem
Text="CYS"
Value="CYS"/>
<asp:ListItem
Text="DHEL"
Value="DHEL"/>
<asp:ListItem
Text="DOL"
Value="DOL"/>
<asp:ListItem
Text="EDL"
Value="EDL"/>
<asp:ListItem
Text="GEM"
Value="GEM"/>
<asp:ListItem
Text="GTEP"
Value="GTEP"/>
<asp:ListItem
Text="ITDE"
Value="ITDE"/>
<asp:ListItem
Text="MCCA"
Value="MCCA"/>
<asp:ListItem
Text="OL"
Value="OL"/>
<asp:ListItem
Text="PET"
Value="PET"/>
<asp:ListItem
Text="PHE"
Value="PHE"/>
<asp:ListItem
Text="SLP"
Value="SLP"/>
</asp:DropDownList>
</td>
</tr>
<tr>
<td class="DropDownClass">
<asp:DropDownList
ID="paper"
class="fine"
Runat="Server">
<asp:ListItem
Text="Choose a paper type"
Value="cpt"/>
<asp:ListItem
Text="Applied Dissertation"
Value="ad"/>
<asp:ListItem
Text="MARP"
Value="MARP"/>
<asp:ListItem
Text="Practicum"
Value="p"/>
<asp:ListItem
Text="Practicum I"
Value="p1"/>
<asp:ListItem
Text="Practicum II"
Value="p2"/>
</asp:DropDownList>
</td>
<tr>
<td class="DropDownClass">
<asp:DropDownList
ID="ANNO"
class="fine"
Runat="Server">
<asp:ListItem
Text="Choose a publication Year"
Value="cpy"/>
<asp:ListItem
Text="All Years"
Value="ay"/>
<asp:ListItem
Text="2005"
Value="2005"/>
<asp:ListItem
Text="2004"
Value="2004"/>
<asp:ListItem
Text="2003"
Value="2003"/>
<asp:ListItem
Text="2002"
Value="2002"/>
<asp:ListItem
Text="2001"
Value="2001"/>
<asp:ListItem
Text="2000"
Value="2000"/>
<asp:ListItem
Text="1990-1999"
Value="9099"/>
<asp:ListItem
Text="1980-1989"
Value="8089"/>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:CheckBox
ID="FT"
Checked="false"
Text="Limit to full-text documents"
TextAlign="Right"
ForeColor="navy"
Font-Bold="true"
Runat="Server"/>
</td>
<tr>
<td class="DropDownClass">
<asp:DropDownList
ID="recordspage"
Runat="Server">
<asp:ListItem
Text="Number of records displayed per page"
Value="5"/>
<asp:ListItem
Text="5 records per page"
Value="5"/>
<asp:ListItem
Text="10 records per page"
Value="10"/>
<asp:ListItem
Text="25 records per page"
Value="25"/>
<asp:ListItem
Text="50 records per page"
Value="50"/>
</asp:DropDownList>
</td>
</tr>
<TD class="ButtonClass">
<asp:Button
ID="Button1"
Text="Submit"
OnClick="Button_Click"
Runat="Server"/>
</td></tr>
</table>
</center>
</form>
<P>
<center>
<table class="TableSet">
<tbody>
<tr>
<td>
<P class="Head">
Obtaining MARPs (Major Applied Research Projects), Practicums &amp;
Applied Dissertations
<ul>
<li>Available only to NSU students, staff and faculty.</li>
<li>Available only on microfiche from the main library, paper copies
not available.</li>
<li> Local students request microfiche at the Reference Desk.&nbsp;</li>
<li> Distance Program students may request microfiche copies through
Document <asp:hyperLink id="DocDeliveryLink" NavigateUrl="http://www.nova.edu/library/serv/docdel/distance.html" Text="Document Delivery" Runat="Server"/>.
</li>
</ul>
</P>
<p class="Head">NSU Education Students:
<ul>
<li> Approximately 350 NSU papers are indexed through the <b>ERIC</b>
database with full-text available for approximately 200 of them. To
access ERIC, go to <asp:hyperLink id="ElecResourcesLink" NavigateUrl="http://www.nova.edu/library/eleclib/" Text="Electronic Resources" Runat="Server"/>.</li>
<li> See for <asp:hyperLink id="DissertationLink" NavigateUrl="http://www.nova.edu/library/dils/tutorials/education/dissertations/ericdissertations/index.htm" Text="About Dissertations in ERIC" Runat="Server"/>information on locating NSU applied
research in ERIC.</li>
<li> Search the <asp:hyperLink id="ERODlink" NavigateUrl="http://bcol02.ed.gov/Programs/EROD/" Text="Education Resource Organizations Directory" Runat="Server"/>
for resources located in your area.</li>
<li> DHEL students can view <asp:hyperLink id="SampleDissLink" NavigateUrl="http://www.schoolofed.nova.edu/dhel/online_docs.htm" Text="Sample Dissertations and Research Practicums" Runat="Server"/>. &nbsp;</li>
<li> Several programs provide information on writing MARPs, Practicums,
Applied Dissertations and other papers/reports:</li>
<ul>
<li><asp:hyperLink id="GTEP" NavigateUrl="http://www.nova.edu/gtep/practicum/guide.html" Text="GTEP Project Resource Guide (2003)" Runat="Server"/>.&nbsp;</li>
<li><asp:hyperLink id="DHEL" NavigateUrl="http://www.schoolofed.nova.edu/dhel/online_docs.htm" Text="DHEL Online Documents section" Runat="Server"/>. includes:
<ul>
<li>Guide to the Applied Dissertation Process </li>
<li>Form and Style Guide for the Applied Dissertation</li>
<li>Guide to Writing the Practicum Proposal <br>
</li>
</ul>
</li>
</ul>
</ul>
<b><a href="#top"></a></b>
</p>
</td>
</tr>
</tbody>
</table>
</center>




Answer this question

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

  • David_Som

    Thank's Dave,

    it seems that the cause of the error was the statement that you mentioned in your messagge. Now I get a different error:

    Line 33: strQuery =strQuery + "@kw )"
    Line 34:
    Line 35: cmdselect.Parameters.Add("@kw","smith")
    Line 36:
    Line 37: selectOtherCriteria()

    Dave, cuurently I don't use any debugger, thus i only have the stack trace to debug my pages. The company that I work for doesn'T have Microsoft Visual Studio. Yestarday I downloaded Visual Web Developer. Do you know it If so, what do you think of it

    Thank you,

    Christian



  • joltinjoe02

    Ooops.

    To answer your other question, I haven't used Visual Web Developer for quite some time. I remember it being really good for simple 2-tier web applications. If that is all you build, I think you probably have the right tool for the job.

    As you start building more advanced web applications, you will probably want to start building class libraries and a separate business tier for which will you will want the professional version. However, until that time, keep plugging away :)

    Regards,

    Dave



  • DenverCoder

    Did you step through it with the debugger

    The obvious problem I see is that you are not assigning sqldata which means it will be null. Should be:

    sqldata = cmdselect.ExecuteReader()

    But that would make me think your stack trace would show the null exception in showresults().

    Is it that obvious or did your copy and paste just skip that code

    Regards,

    Dave



  • Mutt10R

    Hey Christian,

    You're debugging it old school where YOU are the debugger.

    You are getting an error because you are adding parameters to an SqlCommand object that has not been defined yet.

    This statement

    cmdSelect = New SqlCommand(strQuery, DBconnection)

    needs to occur before this statement:

    cmdselect.Parameters.Add("@kw","smith")

    There may be other errors. You will just have to go through it slowly.

    Regards,

    Dave



  • tfrazier

    Thank's for helping Dave.

    talk to you soon.

    christian



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