Ben
Ben

Reputation: 13

Help needed declaring code-behind controls

OK - back for help sooner than expected.

I've got a c# web project and am having trouble with my controls. I'm trying to do something pretty simple - just changing a couple of labels on a page.

When I try and build the project it errors with a NullReferenceException error, which I think may be down to my not having declared the controls properly.

I'm pretty new to c#, so any help would be greatly appreciated.

The code I am using is:

protected void Page_Load(object sender, EventArgs e)
    {
         string[] allLines = File.ReadAllLines(Server.MapPath("Albums.txt"));
         Album[] Albums = new Album[allLines.Length];
         for (int i = 0; i < allLines.Length; i++)
         {
           string[] lineSplit = allLines[i].Split(',');
           Albums[i] = new Album();
           Albums[i].ID = Convert.ToInt32(lineSplit[0]);
           Albums[i].title = lineSplit[1];
           Albums[i].keyName = lineSplit[2];
       }

        this.albumID.Text = Convert.ToString(Albums[0].ID);
        this.albumTitle.Text = "Hello World";
        this.albumKeyName.Text = Albums[0].keyName;           
   }

and the albumID, albumTitle and albumKeyName controls are defined in the default.designer.cs page created by VS.

Cheers,

Ben

PS - aspx markup:

<body>
<form id="Form1" runat="server">
    <asp:Label ID="albumID" runat="server" Text="Label"></asp:Label>
    <asp:Label ID="albumTitle" runat="server" Text="Label"></asp:Label>
    <asp:Label ID="albumKeyName" runat="server" Text="Label"></asp:Label>
</form>

To try and pinpoint the problem I've removed a load of the code to just leave the following:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        this.albumTitle.Text = "Hello World";          
   }
}

It's erroring on the "Hello World" line with a NullReferenceException - the details below the error say: "Object reference not set to an instance of an object."

Sorry about the array - looks like it was a red herring.

Upvotes: 0

Views: 135

Answers (4)

Ben
Ben

Reputation: 13

Sorted now - still don't know what the error was, but deleted the files and recreated them and it all worked fine.

Chalk it up to a frustrating mystery. :-(

Upvotes: 0

RickNZ
RickNZ

Reputation: 18652

What does your <%@ Page %> directive look like? Are you by any chance using CodeBehind instead of CodeFile?

Upvotes: 0

womp
womp

Reputation: 116987

This looks ok without seeing your markup, as long as the three controls do exist in the markup.

There are a number of places that could be null - Albums[0], lineSplit and allLines are all candidates for nulls.

Upvotes: 0

Brandon
Brandon

Reputation: 70032

Have you set a breakpoint and tried stepping through it?

My suggestion would be to first make sure that the allLines array is not null before accessing the Length property on it.

Another possible cause would be that allLines length was 0, so no Albums were ever created. (Which means Albums[0].ID would be a NullReferenceException).

Upvotes: 1

Related Questions