Showing posts with label debug. Show all posts
Showing posts with label debug. Show all posts

Thursday, March 29, 2012

Object Reference

Hello,

I have a rather large web app that I'm trying to debug and I get this error on multiple pages:

---------------------
Object reference not set to an instance of an object.
---------------------

The error is pointing at my insert statement but there is nothing wrong with the statement. I've also checked to make sure that each field in the web app has a place in the insert statement, verified the names in the DB with the names in the insert statement and made sure that the values in the insert statement are correct with the names in the SQL placeholders (@dotnet.itags.org.variable).

I don't understand this error. Any ideas?

DaleThe error is actually with your .NET code. On your command, I can place a bet that it is nulled/nothing. You must create a new instance of the command. For example:


SqlCommand command = null; // you must have something like this

...

// but this is correct
SqlCommand command =new SqlCommand();


Okay. So if I have it setup as in the following:


Dim DBConnect As New SqlConnection
Dim InsertStatement As New SqlCommand

DBConnect.ConnectionString = ("server=SERVER;database=DATABASE;uid=SA;password=PASSWORD")

InsertStatement.CommandText = "Insert TABLE (Column1, Column2) & _
"Values (@.Column1, @.Column2)"

InsertStatement.Parameters.Add("@.Column1", SqlDatatype, Char, 30).Values=txtColumn1.Text

InsertStatement.Parameters.Add("@.Column2", SqlDatatype, Char, 30).Values=txtColumn2.Text

...

Where/how do I add the "SqlCommand Command = New SqlCommand()"?

Thanks again,
Dale
That code was in C#... you are coding in VB.NET. You have the equivelence over here -- so which line is giving you the problem?
Well, it is specifying the acutal insert statement as the pint of failure.

(** In my last post I fogot to type the " at the end of the statement, I've corrected this below.)


InsertStatement.CommandText = "Insert TABLE (Column1, Column2)" & _

So you typed this online then? If you have then you have just found a "silly" typing error. In your original code, you might have this:

Dim InsertStatement As SqlCommand ' notice the "new" missing

However you must create a new instance of the InsertCommand variable. You have done this in the above post but you could have overread your original code. It should be creating a new instance like here:

Dim InsertStatement As New SqlCommand

Not a problem Dale.
Yes, if fact I just added that to my code to see if that was the issue. check back to see if it worked. Thanks a TON!

Dale

Monday, March 26, 2012

Object reference not set to an instance of an object

HERE'S MY SCRIPT;

<%@dotnet.itags.org. Page Language="VB" Debug="True" CodePage="28599" %>
<%@dotnet.itags.org. Import NameSpace="System.Data" %>
<script runat="server">
Sub Page_Load(Obj As Object,E As EventArgs)
'DataSet Nesnesini Olustur -------
Dim ds as new DataSet("ornek_dataset")
'Tablo Olustur-----------
Dim musteritable as New DataTable("müsteriler")
'Tabloya Sutunlar Ekle--------
musteritable.Columns.Add ("ad",System.Type.GetType("System.String"))
musteritable.Columns.Add ("soyad",System.Type.GetType("System.String"))
musteritable.Columns.Add ("meslek",System.Type.GetType("System.String"))
musteritable.Columns.Add ("yas",System.Type.GetType("System.Int32"))
'Tabloyu DataSete Ekle --------
ds.Tables.Add(musteritable)
'Satir Olusturup, Veri Gir-------
Dim sira As DataRow = musteritable.NewRow
sira("ad")="Zafer"
sira("soyad")="Demirkol"
sira("meslek")="Web Programcýsý"
sira("yas")="33"
musteritable.rows.Add(sira)

Dim sira1 As DataRow = musteritable.NewRow
sira("ad")="Serdar"
sira("soyad")="Benli"
sira("meslek")="Pazarlamacý"
sira("yas")="32"
musteritable.rows.Add(sira1)

Dim sira2 As DataRow = musteritable.NewRow
sira("ad")="Denizhan"
sira("soyad")="Akkaya"
sira("meslek")="Muhasebeci"
sira("yas")="26"
musteritable.rows.Add(sira2)

Dim sira3 As DataRow = musteritable.NewRow
sira("ad")="Levent"
sira("soyad")="Demirkol"
sira("meslek")="Aþcý"
sira("yas")="28"
musteritable.rows.Add(sira3)
'----~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-----
Response.Write(ds.Tables("musteriler").rows(2)("ad") & "<br>")

Dim satir_3 As Datarow
satir_3 = ds.Tables("musteriler").rows(2)
Response.Write(satir_3(0) & "<br>")

Dim siralar As DataRowCollection
siralar=ds.Tables("musteriler").rows
Response.Write(siralar(2)("ad") & "<br>")

Dim tablo_degiskenim As DataTable
tablo_degiskenim = ds.Tables("musteriler")
Response.Write(tablo_degiskenim.rows(2)(0) & "<br>")

End sub
</script
AND THE ERROR PAGE;

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 43: musteritable.rows.Add(sira3)
Line 44: '----~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-----
Line 45: Response.Write(ds.Tables("musteriler").rows(2)("ad") & "<br>")
Line 46:
Line 47: Dim satir_3 As Datarow

Source File: D:\ASP.NET\dataset_olustur2.aspx Line: 45

I COULDN'T SEE THE MISTAKE, CAN YOU HELP ME?
THANX ...When you created the table, you called it "müsteriler".

Then, when you referenced it on Line 45, you called it "musteriler" ... with no accent ...
Oh yeah ! :)
...

I am bad at my native-lang "Turkish" just like I am bad at English ...
ü is a fuckin' letter in our keyboards ...

...

Thanx NewKid2 ...
If you find this happening too often, considerchanging the language of your computer.

Thursday, March 22, 2012

Object reference not set to an instance of an object working in debug mode but not in IIS

Hi have this funny error, working fine when I debug the project in debug mode but has the above error in localhost.

All the page works fine but only one with this error... Anyone know how to solve this problem?

cheers

Hello my friend,

Could you provide some source code and the error details that you get, line number, etc?

Kind regards

Scotty


Hi Scotty,


Sorry for the late reply... I do not know which part of the source code that cause this problem cause when I'm trying to track this error through debug mode, but the program just works fine.

When i try accessing it through the localhost, it just pop up a alert stating the above error...

cheers


hi,

do you have virtual directory in IIS? if no, create it. if yes, provide your full code. so that it will be easy to find out the error.


HiXpy,

Based on my understanding, your web application runs fine with Visual Studio in the debug mode, but when you run it in localhost you get the "Object reference not set to an instance of an object" error message. If I have misunderstood you, please feel free to tell me.

If we run this web application in the localhost on another machine, does it have the same issue?

There are many causes of that this error message will be appear. It is difficult to find the cause without debugger. Please check the code of the page which displays error message and see if there is a mistake or defect in the code.

You can also debug the web application in the localhost with Just-In-Time debugging which is the feature of Visual Studio 2005 Debugger. When a crash occurs with Just-In-Time Debugging enabled, a dialog box appears and then you can choose the Debugger to debug your web application. For more information, seeJust-In-Time Debugging.

I hope this helps.


i agree with the above post you can get this reason because of many reasons which is difficult to find out without debugging, this error is mainly becuase you are using an object without assingin a value or without initializing that object in the code...so check carefully that all the objects are properly intitialized.


Hi Thomas,


Thanks for the feedback, sorry for not getting back... Been quite bz with work so totally forgotten abt this error.. Will try it out and get back... By the way its an application which runs in my office machine, IIS and even server... But when I tried it on my home machine, this error pop up only in one page... Therefore I guess i will need to look in the codes of this particular page...

Thanks all for the feedback

cheers