11 Replies - 8625 Views - Last Post: 18 December 2009 - 04:11 PM Rate Topic: -----

#1 MiloCold   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-December 09

Object reference not set to an instance of an object.

Post icon  Posted 18 December 2009 - 12:37 PM

Hi,

I'm new to C# and I'm having a heck of a time trying to execute some sql commands. I understand that this error message that I'm getting ("Object reference not set to an instance of an object.") indicates that somewhere in my code exists an object that is null. The problem is, for life of me, I can't find it!?!?! Even when I debug line-by-line I'm seeing the mysqlCon and mysqlCMD objects defined and initiated properly.

Can someone please point me in the right direction? I'm a tourist in C#-land and, well, I'm lost. :blink:

Thanks in advance!

M. Cold

using System;
using System.Data;
using MySql.Data.MySqlClient;
using MySql.Data.Types;

namespace Flynt
{
  class DbConnection
  {
	private string strProvider = null;
	private string Host = null;
	private string Database = null;
	private string User = null;
	private string Password = null;
	private MySqlConnection mysqlCon = null;
	private MySqlCommand mysqlCMD = null; 

	private void ConnectToLocalhost()
	{
	  strProvider = "Data Source=192.168.1.103;Database=flynt;User ID=test;Password=test";
	  mysqlCon = new MySqlConnection(strProvider);

	}

	private void ConnectToDB()
	{
	  strProvider = "Data Source=" + Host + ";Database=" + Database + ";User ID=" + User + ";Password=" + Password;
	  mysqlCon = new MySqlConnection(strProvider);
	}

	public void FileToDatabase(string FilePath){

	  ConnectToLocalhost();
	  try
	  {
		mysqlCon.Open();
		string SqlStmt = "LOAD DATA LOCAL INFILE '" + FilePath + "' INTO TABLE temp_members IGNORE 1 LINES";
		mysqlCMD = new MySql.Data.MySqlClient.MySqlCommand(SqlStmt, mysqlCon);
		mysqlCMD.ExecuteNonQuery();
		
		mysqlCon.Close();
	  }
	  catch (Exception ex)
	  {
		string ErrMessage = ex.Message;
		Console.WriteLine(ErrMessage);
		Console.ReadLine();
		mysqlCon.Close();
	  }
	  
	}

  }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Object reference not set to an instance of an object.

#2 MentalFloss   User is offline

  • .
  • member icon

Reputation: 591
  • View blog
  • Posts: 1,543
  • Joined: 02-September 09

Re: Object reference not set to an instance of an object.

Posted 18 December 2009 - 12:51 PM

private void ConnectToDB()
	{
	  strProvider = "Data Source=" + Host + ";Database=" + Database + ";User ID=" + User + ";Password=" + Password;
	  mysqlCon = new MySqlConnection(strProvider);
	}



Host, Database, User, Password...


By the way, don't set your strings to null like that. Just use String.Empty if anything. Then this kinda stuff won't happen.
Was This Post Helpful? 0
  • +
  • -

#3 MiloCold   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-December 09

Re: Object reference not set to an instance of an object.

Posted 18 December 2009 - 01:02 PM

Thank you for the quick reply! The only thing is that that function isn't being used. I'm gonna use it later...

The ConnectToLocalhost() function is being used.

Thanks for the tip on the nulls!

M.Cold
Was This Post Helpful? 0
  • +
  • -

#4 lesPaul456   User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Object reference not set to an instance of an object.

Posted 18 December 2009 - 01:15 PM

Quote

Thank you for the quick reply! The only thing is that that function isn't being used. I'm gonna use it later...

The ConnectToLocalhost() function is being used.

Thanks for the tip on the nulls!


It's the same problem: strProvider is set to null, and I don't see where you initialize it.

When you want to find what object is null, set a breakpoint and step through. When you get to the line causing the error, open the "Locals Window", and browse to through your objects. If an object is null, it will say so.
Was This Post Helpful? 0
  • +
  • -

#5 MiloCold   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-December 09

Re: Object reference not set to an instance of an object.

Posted 18 December 2009 - 01:20 PM

Sorry, strProvider is initialized in the ConnectToLocalhost function.

I have stepped through it and neither mysqlCon or mysqlCMD are null. :crazy:

mysqlCMD.Transaction is null, could that be it?

This post has been edited by MiloCold: 18 December 2009 - 01:36 PM

Was This Post Helpful? 0
  • +
  • -

#6 MentalFloss   User is offline

  • .
  • member icon

Reputation: 591
  • View blog
  • Posts: 1,543
  • Joined: 02-September 09

Re: Object reference not set to an instance of an object.

Posted 18 December 2009 - 01:58 PM

Are you sure the null reference is in the section of code you pasted?
When you run it in IDE, it should fail on null reference and pop up a box at the exact location of the null.

Does that not happen?
Was This Post Helpful? 0
  • +
  • -

#7 MiloCold   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-December 09

Re: Object reference not set to an instance of an object.

Posted 18 December 2009 - 02:03 PM

The null reference will pop up once I remove the try/catch block. It does so on the following line.

mysqlCMD.ExecuteNonQuery();



On that line the Locals window shows: mysqlCMD {MySql.Data.MySqlClient.MySqlCommand} (with all of the other expanded data defined)

This post has been edited by MiloCold: 18 December 2009 - 02:04 PM

Was This Post Helpful? 0
  • +
  • -

#8 MentalFloss   User is offline

  • .
  • member icon

Reputation: 591
  • View blog
  • Posts: 1,543
  • Joined: 02-September 09

Re: Object reference not set to an instance of an object.

Posted 18 December 2009 - 02:15 PM

Man, I'm stumped.

Try this:

comment out the
private MySqlCommand mysqlCMD = null; 



Change

public void FileToDatabase(string FilePath){

	  ConnectToLocalhost();
	  try
	  {
		mysqlCon.Open();
		string SqlStmt = "LOAD DATA LOCAL INFILE '" + FilePath + "' INTO TABLE temp_members IGNORE 1 LINES";
		mysqlCMD = new MySql.Data.MySqlClient.MySqlCommand(SqlStmt, mysqlCon);
		mysqlCMD.ExecuteNonQuery();
	   
		mysqlCon.Close();
	  }
	  catch (Exception ex)
	  {
		string ErrMessage = ex.Message;
		Console.WriteLine(ErrMessage);
		Console.ReadLine();
		mysqlCon.Close();
	  }
	 
	}



to:

public void FileToDatabase(string FilePath){

	  ConnectToLocalhost();

	  try
	  {
		mysqlCon.Open();
		string SqlStmt = "LOAD DATA LOCAL INFILE '" + FilePath + "' INTO TABLE temp_members IGNORE 1 LINES";
		using (MySql.Data.MySqlClient.MySqlCommand mysqlCMD = new MySql.Data.MySqlClient.MySqlCommand(SqlStmt, mysqlCon))
			mysqlCMD.ExecuteNonQuery();
	   
		mysqlCon.Close();
	  }
	  catch (Exception ex)
	  {
		string ErrMessage = ex.Message;
		Console.WriteLine(ErrMessage);
		Console.ReadLine();
		mysqlCon.Close();
	  }
	 
	}



I'd also suggest commenting out everything you don't need for now. So you can isolate this problem.
Was This Post Helpful? 0
  • +
  • -

#9 MiloCold   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-December 09

Re: Object reference not set to an instance of an object.

Posted 18 December 2009 - 02:34 PM

Thanks for the suggestion. I tested it, but I'm still getting that null reference error. :(
Was This Post Helpful? 0
  • +
  • -

#10 MentalFloss   User is offline

  • .
  • member icon

Reputation: 591
  • View blog
  • Posts: 1,543
  • Joined: 02-September 09

Re: Object reference not set to an instance of an object.

Posted 18 December 2009 - 02:38 PM

Can you post the whole program? Use a Zip file attachment or something with source. I'll look at it and change everythign to be SqlServer (Since that's what I have) and we'll find this thing.
Was This Post Helpful? 0
  • +
  • -

#11 MiloCold   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-December 09

Re: Object reference not set to an instance of an object.

Posted 18 December 2009 - 03:46 PM

Well, it ended up being a problem with the SQL query.

"LOAD DATA LOCAL INFILE 'Z:\\ConsoleApps\\Flynt\\bin\\flynt_member_files\\file.txt' INTO TABLE temp_members IGNORE 1 LINES"



M. Cold
Was This Post Helpful? 0
  • +
  • -

#12 MentalFloss   User is offline

  • .
  • member icon

Reputation: 591
  • View blog
  • Posts: 1,543
  • Joined: 02-September 09

Re: Object reference not set to an instance of an object.

Posted 18 December 2009 - 04:11 PM

Glad you found it! That was tricky.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1