Help - Object reference not set to an instance of an object??

Last post 08-21-2007 7:06 AM by rednelo. 16 replies.

Sort Posts:

  • Help - Object reference not set to an instance of an object??

    11-09-2006, 11:13 PM
    • Loading...
    • rednelo
    • Joined on 11-09-2006, 10:41 PM
    • North Carolina
    • Posts 114

    I'm having an bit of a problem here.  I'm using Web Developer 2005.  All I want to do is have an invisible object become visible when I click a button.  Pretty simple I'm sure.

    Now, I can do this as long as the object is NOT in a gridview where I'm editing a template, case in point:

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

    1) Button1 example works fine.  The label visual property is set to false. 

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    Label1.Visible =

    True

    End SubClick the button, the label appears.  No problemo.

     

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

    2) When I try the exact same thing on a label located in a gridview where I'm editing an item template of a Details view it won't work.  It tells me that label2 is not declared...

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    label2.Visible =

    "true"

    End Sub

    As you can see, same code, don't work.  So....

    --------

    3) So, I declared it as I did here with calendar1.  However, when I declare it, the old error goes away but now I get...Object reference not set to an instance of an object.  All I'm really trying to make a calendar appear at the click of a button to select a date.   

    Dim calendar1 As Calendar

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    calendar1.Visible = "true"

    End Sub
    1    Partial Class test
    2        Inherits System.Web.UI.Page
    3    
    4    
    5        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    6            Label1.Visible = True
    7        End Sub
    8    
    9        Dim calendar1 As Calendar
    10   
    11       Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    12   
    13           calendar1.Visible = "true"
    14   
    15       End Sub
    16   
    17   
    18   End Class
    19   
    20   
    21   HTMl:
    22   
    23   Language="VB" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %>
    24   
    25   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    26   
    27   <html xmlns="http://www.w3.org/1999/xhtml" >
    28   <head runat="server">
    29       <title>Untitled Page</title>
    30   </head>
    31   <body>
    32       <form id="form1" runat="server">
    33       <div>
    34           <asp:Button ID="Button1" runat="server" Text="Button" />
    35           <asp:Label ID="Label1" runat="server" Text="Label" Visible="False"></asp:Label>
    36           <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HbAdminMaintenance %>" DeleteCommand="DELETE FROM [HbProducts] WHERE [productID] = @productID" InsertCommand="INSERT INTO [HbProducts] ([source], [productName], [marketingDescription], [categoryID], [manufacturerID], [image1], [image2], [image3], [manufacturer_sku], [manufacurerProductLink], [dateEffective], [dateExpires], [dateAdded], [active], [image4], [productInformation]) VALUES (@source, @productName, @marketingDescription, @categoryID, @manufacturerID, @image1, @image2, @image3, @manufacturer_sku, @manufacurerProductLink, @dateEffective, @dateExpires, @dateAdded, @active, @image4, @productInformation)" SelectCommand="SELECT * FROM [HbProducts]" UpdateCommand="UPDATE [HbProducts] SET [source] = @source, [productName] = @productName, [marketingDescription] = @marketingDescription, [categoryID] = @categoryID, [manufacturerID] = @manufacturerID, [image1] = @image1, [image2] = @image2, [image3] = @image3, [manufacturer_sku] = @manufacturer_sku, [manufacurerProductLink] = @manufacurerProductLink, [dateEffective] = @dateEffective, [dateExpires] = @dateExpires, [dateAdded] = @dateAdded, [active] = @active, [image4] = @image4, [productInformation] = @productInformation WHERE [productID] = @productID">
    37               <DeleteParameters>
    38                   <asp:Parameter Name="productID" Type="Int32" />
    39               </DeleteParameters>
    40               <UpdateParameters>
    41                   <asp:Parameter Name="source" Type="String" />
    42                   <asp:Parameter Name="productName" Type="String" />
    43                   <asp:Parameter Name="marketingDescription" Type="String" />
    44                   <asp:Parameter Name="categoryID" Type="Int32" />
    45                   <asp:Parameter Name="manufacturerID" Type="Int32" />
    46                   <asp:Parameter Name="image1" Type="String" />
    47                   <asp:Parameter Name="image2" Type="String" />
    48                   <asp:Parameter Name="image3" Type="String" />
    49                   <asp:Parameter Name="manufacturer_sku" Type="String" />
    50                   <asp:Parameter Name="manufacurerProductLink" Type="String" />
    51                   <asp:Parameter Name="dateEffective" Type="DateTime" />
    52                   <asp:Parameter Name="dateExpires" Type="DateTime" />
    53                   <asp:Parameter Name="dateAdded" Type="DateTime" />
    54                   <asp:Parameter Name="active" Type="Boolean" />
    55                   <asp:Parameter Name="image4" Type="String" />
    56                   <asp:Parameter Name="productInformation" Type="String" />
    57                   <asp:Parameter Name="productID" Type="Int32" />
    58               </UpdateParameters>
    59               <InsertParameters>
    60                   <asp:Parameter Name="source" Type="String" />
    61                   <asp:Parameter Name="productName" Type="String" />
    62                   <asp:Parameter Name="marketingDescription" Type="String" />
    63                   <asp:Parameter Name="categoryID" Type="Int32" />
    64                   <asp:Parameter Name="manufacturerID" Type="Int32" />
    65                   <asp:Parameter Name="image1" Type="String" />
    66                   <asp:Parameter Name="image2" Type="String" />
    67                   <asp:Parameter Name="image3" Type="String" />
    68                   <asp:Parameter Name="manufacturer_sku" Type="String" />
    69                   <asp:Parameter Name="manufacurerProductLink" Type="String" />
    70                   <asp:Parameter Name="dateEffective" Type="DateTime" />
    71                   <asp:Parameter Name="dateExpires" Type="DateTime" />
    72                   <asp:Parameter Name="dateAdded" Type="DateTime" />
    73                   <asp:Parameter Name="active" Type="Boolean" />
    74                   <asp:Parameter Name="image4" Type="String" />
    75                   <asp:Parameter Name="productInformation" Type="String" />
    76               </InsertParameters>
    77           </asp:SqlDataSource>
    78           <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
    79               <Columns>
    80                   <asp:CommandField ShowEditButton="True" />
    81                   <asp:TemplateField HeaderText="dateEffective" SortExpression="dateEffective">
    82                       <EditItemTemplate>
    83                           <asp:Calendar ID="Calendar1" runat="server" 
    84                               SelectedDate='<%# Bind("dateEffective") %>' Visible="False"></asp:Calendar>
    85                           <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("dateEffective") %>'  Visible="False"></asp:TextBox>
    86                           <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" />
    87                       </EditItemTemplate>
    88                       <ItemTemplate>
    89                           <asp:Label ID="Label1" runat="server" Text='<%# Bind("dateEffective") %>'></asp:Label>
    90                       </ItemTemplate>
    91                   </asp:TemplateField>
    92               </Columns>
    93           </asp:GridView>
    94       
    95       </div>
    96       </form>
    97   </body>
    98   </html>
    99   
    
     

     

  • Re: Help - Object reference not set to an instance of an object??

    11-10-2006, 12:04 AM
    • Loading...
    • shados
    • Joined on 07-07-2006, 7:24 PM
    • Posts 1,612

    First, if you declare something, but do not give it a value, it won't be set. So like, Dim blah as Calendar... then you need to assign blah to -something-. In this case its just an empty container, so it won't work

    Your problem however, is that anything inside a template cannot be accessed directly. You need to use the FindControl method.

    Dim mylabel as Label = Ctype(GridView1.FindControl("Label2"),Label);   (I think, my VB is rusty)

    Then myLabel.visible = false;


    Again: anything inside a template must be "dugged out", so to speak. That was your issue.

  • Re: Help - Object reference not set to an instance of an object??

    11-10-2006, 12:32 AM
    • Loading...
    • jessjing
    • Joined on 09-26-2006, 8:03 AM
    • Posts 872

    hi,

    as for the label in GridView

    please do like this

     protected void Button2_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow r in GridView1.Rows)
            {
                Label lb = (Label)(r.Cells[index].FindControl("lb1"));  index is the column that your label control in
                lb.Visible = false;
            }
        }

    as for the Calender, do it like this

       Calendar ca = new Calendar();
            ca.ID = "Calender1";

            form1.Controls.Add(ca);

    btw am using c# , you can change the code into vb

  • Re: Help - Object reference not set to an instance of an object??

    11-10-2006, 7:11 AM
    • Loading...
    • rednelo
    • Joined on 11-09-2006, 10:41 PM
    • North Carolina
    • Posts 114

    I tried it as you did above but it gives me the same message...

    Dim

    mycalendar As Calendar = CType(GridView1.FindControl("Calendar1"), Calendar)

     

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    mycalendar.Visible =

    True

    End Sub

    Object reference not set to an instance of an object.

    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 10: 
    Line 11: 
    Line 12:     Dim mycalendar As Calendar = CType(GridView1.FindControl("Calendar1"), Calendar)
    Line 13:     
    Line 14:     Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)

     

  • Re: Help - Object reference not set to an instance of an object??

    11-10-2006, 7:47 AM
    • Loading...
    • rednelo
    • Joined on 11-09-2006, 10:41 PM
    • North Carolina
    • Posts 114

    Hi.  I tried yours as well. I'm very new to VP and have no C# background... I used the following link to convert it into vb http://www.kamalpatel.net/ConvertCSharp2VB.aspx

    When I tried it your way, lb.Visible = False,  ca.ID = "Calender1", as well as form1.Controls.Add(ca) are not declared...

     Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim r As GridViewRow

    For Each r In GridView1.Rows

    Dim lb As Label = CType((r.Cells(index.FindControl("label2"))

    'index is the column that your label control in, Label)

    lb.Visible =

    False

    Next

    End Sub

    'as for the Calender, do it like Me

    Dim ca As Calendar = New Calendar()

    ca.ID =

    "Calender1"

    form1.Controls.Add(ca)

  • Re: Help - Object reference not set to an instance of an object??

    11-10-2006, 11:47 AM
    • Loading...
    • shados
    • Joined on 07-07-2006, 7:24 PM
    • Posts 1,612

    First, keep in mind it will not be possible for you to access these controls unless the Grid is in the appropriate mode. For example you can't access stuff from the EditTemplate if the grid isn't in edit mode.

    Secondly, CType (from your second example) takes 2 parameters, the second being the -type- of which you are converting.

    So Ctype( myObject, Label) is the same thing as (Label)myObject. If you don't provide the second parameter, you will have issues.

  • Re: Help - Object reference not set to an instance of an object??

    11-12-2006, 8:34 PM
    • Loading...
    • jessjing
    • Joined on 09-26-2006, 8:03 AM
    • Posts 872

    hi,

    I think your tool for converting c# to vb doesn't do a good job for you. It is totally wrong.

    In vb, the code should be this:

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            For Each r As GridViewRow In GridView1.Rows
                DirectCast(r.Cells(4).FindControl("lb"), Label).Visible = False

          Next
        End Sub

    it works fine

     

  • Re: Help - Object reference not set to an instance of an object??

    11-15-2006, 6:38 AM
    • Loading...
    • rednelo
    • Joined on 11-09-2006, 10:41 PM
    • North Carolina
    • Posts 114

    shados,

     I'm in the edit mode so I'm okay there.

    It still didn't "appear" to work after adding the second parameter of CType.

    I say "appear" because one thing I'm finding is strange with this vis web dev. is that some times the code is correct, and it says, that such-n-such is not declared.  it happened yestereday.  I copied some code out of a book, and a variable couldn't be declared.  I then went to the book's source code files, copied and pasted the exact same code, and it works.  I've seen where there's a space in the white space and having deleted it, it works.  I was under the empression that that didn' t matter.  Gremlins!

    I played with your approach for a day.   I'm going to try the other approach again...

    Thanks.

     

     

     

     

  • Re: Help - Object reference not set to an instance of an object??

    11-15-2006, 7:00 AM
    • Loading...
    • rednelo
    • Joined on 11-09-2006, 10:41 PM
    • North Carolina
    • Posts 114

    jessjing,

    Thanks for your help.  I think we're almost there...

    I'm not sure if your code is working vis web developer is finicky. See my post to shados above.

    I added the code as you have it:

    1    <script runat="server">
    2        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.click
    3            For Each r As GridViewRow In GridView1.Rows
    4                DirectCast(r.Cells(4).FindControl("label1"), Label).Visible = "False"
    5            Next
    6        End Sub

    7    </script>
    8   
    9    <html xmlns="http://www.w3.org/1999/xhtml" >
    10   <head runat="server">
    11       <title>Untitled Page</title>
    12   </head>
    13   <body>
    14       <form id="form1" runat="server">
    15       <div>
    16           <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="categoryID" DataSourceID="SqlDataSource1">
    17               <EmptyDataTemplate>
    18                   &nbsp;
    19               </EmptyDataTemplate>
    20               <Columns>
    21                   <asp:CommandField ShowEditButton="True" />
    22                   <asp:BoundField DataField="categoryID" HeaderText="categoryID" InsertVisible="False"
    23                       ReadOnly="True" SortExpression="categoryID" />
    24                   <asp:TemplateField HeaderText="categoryName" SortExpression="categoryName">
    25                       <EditItemTemplate>
    26                           <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("categoryName") %>'></asp:TextBox>
    27                           <asp:Label ID="Label1" runat="server" Text="Label" Visible="true"></asp:Label>
    28                           <asp:Button ID="Button1" runat="server" Text="Button" />
    29                       </EditItemTemplate>
    30                       <ItemTemplate>
    31                           <asp:Label ID="Label1" runat="server" Text='<%# Bind("categoryName") %>'></asp:Label>
    32                       </ItemTemplate>
    33                   </asp:TemplateField>
    34               </Columns>
    35           </asp:GridView>

    (Yes I am connected to a datasource...)

    However, "handles button1.click" gives me an error.  If I don't include it, the error goes away, and nothing happens if I click the botton.  So I guess I need it.

    For some reason, it does not see button1. 

    I get this error when I run it as you have it:

    Compilation Error

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: BC30506: Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

    Source Error:

    Line 3:  
    Line 4:  <script runat="server">
    Line 5:      Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.click
    Line 6:          For Each r As GridViewRow In GridView1.Rows
    Line 7:              DirectCast(r.Cells(4).FindControl("label1"), Label).Visible = "False"

    Any ideas???

     

  • Re: Help - Object reference not set to an instance of an object??

    11-15-2006, 8:50 PM
    • Loading...
    • jessjing
    • Joined on 09-26-2006, 8:03 AM
    • Posts 872

    hi,

    you are wrong here,

    in your html source code, look at this line:  <asp:Button ID="Button1" runat="server" Text="Button" />
    you didn't add any button event on this Button1, how can the complier find the Button1.Click?

    it should be like this    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />

  • Re: Help - Object reference not set to an instance of an object??

    11-16-2006, 8:08 AM
    • Loading...
    • rednelo
    • Joined on 11-09-2006, 10:41 PM
    • North Carolina
    • Posts 114

     As you can see I added it below.  Good Catch (a small but necessary detail!).  However, I still get the same WithEvent error...

    Thats why I made the link to the picture last time.  Button1.Click is not presented a choice for me to choose from, only form1 or gridview1. 

    1    "VB" %>
    2    "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    3    
    4    <script runat="server"&gt;
    5         
    6        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.click
    7            For Each r As GridViewRow In GridView1.Rows
    8                DirectCast(r.Cells(4).FindControl("label1"), Label).Visible = "False"
    9            Next
    10       
    11       End Sub
    12           
    13       
    14   </script>
    15   
    16   <html xmlns="http://www.w3.org/1999/xhtml" >
    17   "server">
    18       
    19   
    20   
    21       "form1" runat="server">
    22       
    23 "GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="categoryID" DataSourceID="SqlDataSource1"> 24 25   26 27 28 "True"> 29 "categoryID" HeaderText="categoryID" InsertVisible="False" 30 ReadOnly="True" SortExpression="categoryID"> 31 "categoryName" SortExpression="categoryName"> 32 33 "TextBox1" runat="server" Text='<%# Bind("categoryName") %>'> 34 "Label1" runat="server" Text="Label" Visible="true"> 35 "Button1" runat="server" OnClick="Button1_Click" Text="Button"> 36 37 38 39 "Label1" runat="server" Text='<%# Bind("categoryName") %>'> 40 41 42 43
     
  • Re: Help - Object reference not set to an instance of an object?? - LABEL WORKS, CALENDAR DOES NOT

    11-30-2006, 8:03 AM
    • Loading...
    • rednelo
    • Joined on 11-09-2006, 10:41 PM
    • North Carolina
    • Posts 114

    As and update.  I did get the label to be found within the template of a gridview and a detailsview.  And once found the button changes its visible property to "False" as desired.  However, when I applied the same code to a calendar, which is what I was