Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9146417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:44:45+00:00 2026-06-17T10:44:45+00:00

In the below code I am getting a Null Reference Exception error while using

  • 0

In the below code I am getting a Null Reference Exception error while using "Names.Add(string)" method. But when I use a static class or a static object of the NamedDB class it works fine. Can any one explain how object instances work in ASP.Net web applications ? Why am I getting errors in nonstatic objects while static objects are working fine ?

public partial class _Default : System.Web.UI.Page
{

   NameDB Names; //Creating Object

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Names = new NameDB();//Instantiation
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {

        Names.Add("dss");//Object Reference is null

        GridView1.DataSource = Names.GetName();
        GridView1.DataBind();
    }
}


    //NamedDB class

    public class NameDB
    {
        List<string> Names=new List<string>();

        public void Add(string item)
        {
            Names.Add(item);
        }

        public string Remove(string item)
        {

            Names.Remove(item);
            return item;
        }

        public List<string> GetName()
        {

            return Names;
        }

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:44:46+00:00Added an answer on June 17, 2026 at 10:44 am

    You do not have instances of global object which are instantiated before post back. Instantiate the object again in Button1_Click

    Now You would be expecting the collection to hold data between post back but you wont get it until you store it some where like database, files,session, viewstate or cache for data. Session and ViewState are not meant for storing that type of data though.

    protected void Button1_Click(object sender, EventArgs e)
    {
        Names = new NameDB();//Instantiation
        Names.Add("dss");//Object Reference is null
        GridView1.DataSource = Names.GetName();
        GridView1.DataBind();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting a Value cannot be null error from the code below: @Html.Label(material.ExtendedGroup)
I am using the below code, but I am not getting that why it
I am getting a null reference exception while trying to navigate back to the
IN JAVA CODE IN JSP as below i m getting null value for fieldnoOfRecords.
When i run the below code I am getting error like ex = {The
I am getting an compilation error not a statement for the below code. Not
I'm getting this error message with the code below: class Money { public: Money(float
Below is a snippet of my code. The error I am getting is that
HI i am getting null pointer exception while sending image path to another activity
Hi I keep getting this error from the below code, was wondering if anyone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.