NullReferenceException

System.NullReferenceException: Object reference not set to an instance of an object. at article.article.addarticle()

ASPX file:

void add(Object Sender, EventArgs E)
{
if (butimage.PostedFile != null)
{
strTemp = DateTime.Now.ToString();
strTemp = strTemp.Replace(":","");
strTemp = strTemp.Replace(" ","");
strTemp = strTemp.Replace("-","");
strImage = "D:/c/upload/" + Session["name"].ToString() + strTemp + ".jpg";
butimage.PostedFile.SaveAs(strImage);
}
article art = new article();
string temp_subject;
temp_subject = Server.HtmlEncode(subject.Text);
temp_subject = temp_subject.Replace("'","`");
art.subject = temp_subject;
string temp_author;
temp_author = Server.HtmlEncode(author.Text);
temp_author = temp_author.Replace("'","`");
art.author = temp_author;
string temp_abs;
temp_abs = Server.HtmlEncode(abs.Text);
temp_abs = temp_abs.Replace("'","`");
art.abs = temp_abs;
string temp;
temp = Server.HtmlEncode(content.Text);
temp = temp.Replace("\r","<br>");
temp = temp.Replace("'","`");
temp = temp.Replace(" ","&nbsp;&nbsp;");
art.content = temp;
art.ontime = System.DateTime.Now.ToString();
art.image = strImage;

if (art.addarticle())
{
articlecolumn artcol = new articlecolumn();
artcol.articleid = art.articleid;
artcol.colid = labcolid.Text;
if (artcol.addarticlecolumn())
{
Response.Redirect("articleshow.aspx?colid=" + labcolid.Text);
}
else
{
labinfo.Text = artcol.errInfo;
}
}
else
{
labinfo.Text = art.ErrInfo;
}
}

CS file:

public bool addarticle()
{
try
{
DBClass db = new DBClass();
db.ConnStr = ConfigurationSettings.AppSettings["cstr_article"];
db.Sql = "insert into article(subject,author,abstract,content,ontime,image) values ('"+_subject+"','"+_author+"','"+_abstract+"','"+_content+"','"+_ontime+"','"+_image+"')";
db.exeSql();
db.Sql = "select max(articleid) as articleid from article";
db.readerData();
if (db.dr.Read())
{
_articleid = db.dr["articleid"].ToString();
}
db.clear();
return true;
}
catch (System.Exception e)
{
errInfo = e.ToString();
return false;
}
}

[2741 byte] By [Tom_Liu] at [2007-12-24]
# 1

any ASP.NET related questions should be posted over at the ASP.NET forums, since the site is dedicated for ASP.NET:

http://forums.asp.net

Just a suggestion, check through the debugger (step into it) to see what the value is of art and see where it's going wrong. This should get you started into pinpointing whats causing the problem

Thanks!

ahmedilyas at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# Language...
# 2
Ok, thks.
Tom_Liu at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# Language...
# 3

I have just browsed the site you recommended. But I find that my question hardly fit into any proper column.

On the contrary, this question belongs more to C# than to ASP.NET. It is basically a C# program. Because what puzzled me is the Exception, I just could not figured out why this exception occured. Essentially this belongs to C# problem, right?

Do you still think I can not raise question here?

Tom_Liu at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# Language...
# 4
I understand where you are coming from...it's just one of those unfortunate things. Have you stepped through the debugger to see whats happening to the values and at what point the object becomes null perhaps?
ahmedilyas at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# Language...
# 5

Unfortuanately, I am beginner in C#.

I haven't yet learned how to use debugger. I do not use Visual Studio, instead, I always used DreamWeaver, and I write all code myself.

I will try debugging myself.

Thanks for your advice.

Tom_Liu at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# Language...
# 6

Can anybody help me checking this exception?

Many thanks.

Tom_Liu at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# Language...
# 7
Put here the class definition of 'article'.

Regards.

overpeer at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# Language...
# 8

using System;
using System.IO;
using System.Configuration;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using database;
using article;

namespace article
{
public class article
{
private string _articleid;
private string _subject;
private string _author;
private string _abstract;
private string _content;
private string _ontime;
private string _image;
private string errInfo;

public string articleid
{
get{return _articleid;}
set{_articleid = value;}
}
public string subject
{
get{return _subject;}
set{_subject = value;}
}
public string author
{
get{return _author;}
set{_author = value;}
}
public string abs
{
get{return _abstract;}
set{_abstract = value;}
}
public string content
{
get{return _content;}
set{_content = value;}
}
public string ontime
{
get{return _ontime;}
set{_ontime = value;}
}
public string image
{
get {return _image;}
set {_image = value;}
}
public string ErrInfo
{
get{return errInfo;}
}

public bool addarticle()
{
try
{
DBClass db = new DBClass();
db.ConnStr = ConfigurationSettings.AppSettings["cstr_article"];
db.Sql = "insert into article(subject,author,abstract,content,ontime,image) values ('"+_subject+"','"+_author+"','"+_abstract+"','"+_content+"','"+_ontime+"','"+_image+"')";
db.exeSql();
db.Sql = "select max(articleid) as articleid from article";
db.readerData();
if (db.dr.Read())
{
_articleid = db.dr["articleid"].ToString();
}
db.clear();
return true;
}
catch (System.Exception e)
{
errInfo = e.ToString();
return false;
}
}

public bool deletebyarticleid()
{
try
{
DBClass db = new DBClass();
db.ConnStr = ConfigurationSettings.AppSettings["cstr_article"];
db.Sql = "delete from article where articleid=" + _articleid;
db.exeSql();
db.Sql = "delete from articlecolumn where articleid=" + _articleid;
db.exeSql();
db.clear();
return true;
}
catch
{
return false;
}
}

public bool querybyarticleid()
{
try
{
DBClass db = new DBClass();
db.ConnStr = ConfigurationSettings.AppSettings["cstr_article"];
db.Sql = "select * from article where articleid=" + _articleid;
db.readerData();
if (db.dr.Read())
{
_articleid = db.dr["articleid"].ToString();
_subject = db.dr["subject"].ToString();
_author = db.dr["author"].ToString();
_abstract = db.dr["abstract"].ToString();
_content = db.dr["content"].ToString();
_ontime = db.dr["ontime"].ToString();
_image = db.dr["image"].ToString();
}
db.clear();
return true;
}
catch
{
return false;
}
}

public bool updatebyarticleid()
{
try
{
DBClass db = new DBClass();
db.ConnStr = ConfigurationSettings.AppSettings["cstr_article"];
db.Sql = "update article set subject='"+_subject+"', author='"+_author+"', abstract='"+_abstract+"', content='"+_content+"', image='"+_image+"' where articleid=" + _articleid;
db.exeSql();
db.clear();
return true;
}
catch
{
return false;
}
}
}
}

Tom_Liu at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# Language...
# 9

Let me try to describe how you can debug this yourself. Load the source file where the Article class is located into the IDE's editor. Working in the editor, click on the gutter to the left of the first line in some of the key methods, such as AddArticle. By default, a red bar should highlight the line of code next to where you clicked. This red line shows that you have set a breakpoint on a particular line of code.

Now run your program. When one of the methods where you put a breakpoint is called, you should be taken to the line where the breakpoint is set. Now use the F11 key to step through your code. This should allow you to discover the line of code that is causing the exception. Once you have located the offending line of code, you can often determine what is wrong.

Of course, if the exception occurs before you step into your code, then you need to try setting the break point in some other location. Often the exception will provide clues as to which line of code you should set the breakpoint on. Sometimes, of course, you will step all the way through a method, and find that it has no errors in it. That means you just have to keep searching till you find the method that has the problem.

- Charlie

CharlieCalvertMSFT at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# Language...
# 10
Charlie Calvert MSFT wrote:

Load the source file where the Article class is located into the IDE's editor. Working in the editor, click on the gutter to the left of the first line in some of the key methods, such as AddArticle. By default, a red bar should highlight the line of code next to where you clicked. This red line shows that you have set a breakpoint on a particular line of code.

...

Doesn't this require using Visual Studio? Maybe Dreamweaver has an integrated debugger, but it seems doubtful.

sirjis at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# Language...
# 11

Dear Charlie,

Your suggestion is very helpful. Now I am trying to install VS 2005 on my computer, and learn how to debug program on it.

In the past, I only use DreamWeaver to write program. Now I also find that VS 2005 is powerful in develpoing and debugging.

Thank you very much!

Tom_Liu at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# Language...
# 12

Tom,

That sounds great. The Visual Studio Debugger really is very powerful. It has helped me solve problems more times than I can count.

If you are having trouble getting things set up, you might want to create a few default web applications just so you can see how things are usually set up in Visual Studio. Once you see the way the system works, then you should be able to load your own program into the development environment and use the debugger.

- Charlie

CharlieCalvertMSFT at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# Language...
# 13

Charlie,

You are really a nice person, I am very grateful for your kind help and advice.

Many thanks to you again!

Tom_Liu at 2007-10-8 > top of Msdn Tech,Visual C#,Visual C# Language...