index > Visual C# General > why (Object reference not set to an instance of an object)?

why (Object reference not set to an instance of an object)?

I am using this code but getting "Object reference not set to an instance of an object.".

This is the code and below it is the error message:

private void generate_report()

{

npg_connection = new NpgsqlConnection("Server=www.db.2connectbahrain.com;Port=5432;User Id=jassim_rahma;Password=731004167;Database=billing;");

npg_connection.Open();

// npg_command.CommandText = "Select category_title FROM reports_category ORDER BY category_title";

npg_command.CommandText = "select * from cdr where local_gateway_start_dt between '2006-08-01 00:00:00' and '2006-08-01 23:59:59' and originator_ip = '80.88.247.115' and dest_reg_id = '2connect_pbx' and called_num_e164 in('16500110', '16500199') and error_description != ''";

npg_command.Connection = npg_connection;

MessageBox.Show(npg_command.CommandText);

npg_command.Connection.Open();

npg_reader = npg_command.ExecuteReader();

while (npg_reader.Read())

{

MessageBox.Show(npg_reader.GetValue(0).ToString());

}

npg_command.Connection.Close();

}

and here s the error message:


System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="CDR Report"
StackTrace:
at CDR_Report.main_form.generate_report() in C:\Documents and Settings\jassim\My Documents\Visual Studio 2005\Projects\CDR Report\CDR Report\main_form.cs:line 31
at CDR_Report.main_form.btnGenerate_Click(Object sender, EventArgs e) in C:\Documents and Settings\jassim\My Documents\Visual Studio 2005\Projects\CDR Report\CDR Report\main_form.cs:line 58
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at CDR_Report.Program.Main() in C:\Documents and Settings\jassim\My Documents\Visual Studio 2005\Projects\CDR Report\CDR Report\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()





Jassim Rahma
Jassim Rahma

Which line throws the exception? My guess is that npg_command isn't initialized properly. Where is this variable declared (and initialized)?

Btw: you might want to edit your original post and delete connection string information from it as you're currently exposing a very sensitive information to the rest of the world...

Andrej

Andrej Tozon
Andrej Tozon wrote:

Btw: you might want to edit your original post and delete connection string information from it as you're currently exposing a very sensitive information to the rest of the world...

I second that....!!

Dylan Morley

Yeah, you need to create an instance of the command...

npg_command = new NpgsqlCommand(yourCommandText, CommandType.Text, NpgsqlConnection);

Dylan Morley

In at least one of your data rows column(0) is null.

As a result, object reference, GetValue(0), is not set to an instance of an object.

And so, GetValue(0).ToString() becomes (null).ToString()

where .ToString() does not have an object reference.

Additionally, you only need to open the connection once.

Good luck.

Fasty1

Fasty1 wrote:
And so, GetValue(0).ToString() becomes (null).ToString()

Actually, I believe that in this case, GetValue() would return a DBNull, not null. And DBNull.Value *is* a value, so the statement in question would execute without throwing an exception...

Andrej

Andrej Tozon

Try this:

if (!npg_reader.IsDBNull(0)) MessageBox.Show(npg_reader.GetValue(0).ToString());

And I agree, initiate command properly if you did not and open connection if it is not opened before.

slk
reply 7

You can use google to search for other answers

 

More Articles

• Login indipendent application
• how do I code if radiobutton2 is checked then answer=9;
• how to add timeOut without using thread.sleep
• any example using mobile client factory
• I can't find any C# exercises
• error reporting?
• Workflow & ERP system
• Date Time format from DateTime to String
• What does the "+" operator do in this context?
• cookies
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• how can i add items to the contextMenu w
• File.encrypt method
• Lego Interface Problem PLEASE HELP
• Class sharing duing the web application
• TreeView adding nodes
• CheckedListbox in C# 2005
• Converting from vb toc#
• Help--Setup and Deployment with custom UI
• How can I remove html tags ?
• Converting Double To Integer
• DataGridViewButtonColumn
• How to program a decision tree in C#
• XML
• Accessing .AVI As Embedded Resource
• Dial through modem Comport to ISP

Hot Articles

• need return values from Threads
• Date Time format from DateTime to String
• How can I burn a CD using C#?
• Developing C# with Generics
• winforms - knowning the next control whi
• get duration form WMA & WMV
• rowpriority managment in datatable
• How many threads should a process possess?
• Unable to add an identical cell to a row
• how can I put in a procedure to download
• Where is vsvars32.bat ?
• System.Management.ManagementException: I
• "new components cannot be added whi
• How can include component list in Class
• Avoiding flicker when dragging from one

Recommend Articles

• Using C# with MySQL
• simple multi thread in c#
• Large Collection(s) of RegEx objects, is
• Highlighting text of current cell in dat
• Change CommandText to an existing dataset
• Serializing Derived Classes
• Code Security
• Creating TCP SYN Flood C#
• dragdrop row databound SourceDGV to unbo
• Couple of C# newbie questions
• SQL Server on C# question
• how to convert text string to decimal nu
• Using the Categories Dialog on Pocket PC
• Reading and Writing Excel Spreadsheets U
• Windows Service