473,382 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Srange Object error

Hi tere, I have a seperate class called Author:

using System;

namespace DocumentControl
{
/// <summary>
/// Summary description for Author.
/// </summary>
public class Author
{
private string strReport = "";
private string strIssue = "";
private string strAuthors = "";

public Author(string strReport, string strIssue, string strAuthors)
{
this.strReport = strReport;
this.strIssue = strIssue;
this.strAuthors = strAuthors;
}

public string StrReports
{
set
{
strReport = value;
}
get
{
return strReport;
}
}

public string StrIssue
{
set
{
strIssue = value;
}
get
{
return strIssue;
}
}

public string StrAuthor
{
set
{
strAuthors = value;
}
get
{
return strAuthors;
}
}
}
}

I declare the object in the parent class:

private Author objAuthor = null;

I create the object on a Button Event whcih works fine and the values are in
the object.

private void AddAuthor(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
objAuthor = new Author(e.Item.Cells[2].Text, e.Item.Cells[6].Text,
e.Item.Cells[12].Text);
}

Now I try to access the object from a different Button Click event:

private void ButtonAddAuthor_Click(object sender, System.EventArgs e)
{
TextBox3.Text = objAuthor.StrReports;
TextBox4.Text = objAuthor.StrIssue;
TextBox5.Text = objAuthor.StrAuthor;
}

But I get the error: Object reference not set to an instance of an object

Why can't I access the objects content? What is wrong? Thanks a lot for any
feedback

Chris
Nov 17 '05 #1
1 1026
Hi Chris,

The Web page class is not retained between subsequent HTTP requests. When
you first click the button and thus make a postback to the server, your
code-behind class is instantiated, an instance of the Author class gets
initialized in the event handler, but as long as this request is processed,
the whole instance of the code-behind class is recycled.

When you click the other button, a completely new instance of the
code-behind class is created, in which objAuthor is initialized to the null
reference, thus giving you the error.

If you need to retain an instance of a class between requests, you can store
it in the Session object (will work on per-used basis), in the Application
object, or in the Cache. Each scenario has its advantages and drawbacks, I
think you should refer to ASP .NET MSDN docs for more details.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"chris" <ch***@discussions.microsoft.com> wrote in message
news:62**********************************@microsof t.com...
Hi tere, I have a seperate class called Author:

using System;

namespace DocumentControl
{
/// <summary>
/// Summary description for Author.
/// </summary>
public class Author
{
private string strReport = "";
private string strIssue = "";
private string strAuthors = "";

public Author(string strReport, string strIssue, string strAuthors)
{
this.strReport = strReport;
this.strIssue = strIssue;
this.strAuthors = strAuthors;
}

public string StrReports
{
set
{
strReport = value;
}
get
{
return strReport;
}
}

public string StrIssue
{
set
{
strIssue = value;
}
get
{
return strIssue;
}
}

public string StrAuthor
{
set
{
strAuthors = value;
}
get
{
return strAuthors;
}
}
}
}

I declare the object in the parent class:

private Author objAuthor = null;

I create the object on a Button Event whcih works fine and the values are
in
the object.

private void AddAuthor(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
objAuthor = new Author(e.Item.Cells[2].Text, e.Item.Cells[6].Text,
e.Item.Cells[12].Text);
}

Now I try to access the object from a different Button Click event:

private void ButtonAddAuthor_Click(object sender, System.EventArgs e)
{
TextBox3.Text = objAuthor.StrReports;
TextBox4.Text = objAuthor.StrIssue;
TextBox5.Text = objAuthor.StrAuthor;
}

But I get the error: Object reference not set to an instance of an object

Why can't I access the objects content? What is wrong? Thanks a lot for
any
feedback

Chris


Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pkpatel | last post by:
Hi, I keep getting this error every time I try to load crystalreportviewer on a webform with a dataset. Here is the error: -------------------------------------------------------- Server...
2
by: Nithi Gurusamy | last post by:
Dear Group: I have a COM object developed in VB. It makes ADODB calls. When it fails it Raise Error. I am using the COM object in my ASP using Server.CreateObject. Whenever a function call fails...
9
by: Keith Rowe | last post by:
Hello, I am trying to reference a Shockwave Flash Object on a vb code behind page in an ASP.NET project and I receive the following error: Guid should contain 32 digits with 4 dashes...
8
by: mcmg | last post by:
Hi, I have an asp app that works fine on a windows xp machine but does not work on a windows 2000 server. I have the following code in my global.asa: <OBJECT RUNAT=Server SCOPE=SESSION...
0
by: Dirk Försterling | last post by:
Hi all, a few days ago, I upgraded from PostgreSQL 7.2.1 to 7.4, following the instructions in the INSTALL file, including dump and restore. All this worked fine without any error (message). ...
1
by: sorCrer | last post by:
Anyone? Web.config: <?xml version="1.0" ?> <configuration> <system.web> <customErrors mode="Off"/> </system.web> </configuration>
1
by: J. Askey | last post by:
I am implementing a web service and thought it may be a good idea to return a more complex class (which I have called 'ServiceResponse') in order to wrap the original return value along with two...
2
by: Moses | last post by:
Hi All, Is is possible to catch the error of an undefined element while creating an object for it. Consider we are not having an element with id indicator but we are trying to make the object...
11
by: xenoix | last post by:
hey there, im reasonably new to C# and im currently writing a backup application which im using as a learning resource. My PC :- Visual Studio 2005 .NET Framework 2 Component Factory Krypton...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.