Connecting Tech Pros Worldwide Help | Site Map

object reference not set to an instance of an object (file.move)

Member
 
Join Date: Feb 2009
Posts: 35
#1: Mar 11 '09
I'm getting the object reference error somewhere in my code. It didn't exactly tell me where, and I think I've narrowed it down to this line:

Expand|Select|Wrap|Line Numbers
  1. File.Move(tmpDlPath + fileName, dlPath + "\\" + set+ "\\_thumb.jpg");
When I comment out this line, my program runs just fine. Before this line I check to see if both directories exist and if the file exists, but that doesn't seem to help.

Any ideas?
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#2: Mar 11 '09

re: object reference not set to an instance of an object (file.move)


Its hard to say from just one line but...

When Visual Studio breaks it should be giving you a line where it breaks.
Possibility: tmpDlPath + Filename may need a break in between "\\".
Maybe tempDlPath isn't set?
Maybe dlPath isn't set?
Breakpoints along here should help
Member
 
Join Date: Feb 2009
Posts: 35
#3: Mar 12 '09

re: object reference not set to an instance of an object (file.move)


Here's more of what I have:

Expand|Select|Wrap|Line Numbers
  1. if (Directory.Exists(tmpDlPath) && File.Exists(tmpDlPath + fileName) && Directory.Exists(dlPath + "\\" + set))
  2. {
  3.     try
  4.     {
  5.         File.Move(tmpDlPath + fileName, dlPath + "\\" + set+ "\\ThumbNail.jpg");
  6.     }
  7.     catch { }
  8. }
tmpDlPath already has a "\" at the end of it. Visual Studio doesn't give me a specific line where the error occurs.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#4: Mar 12 '09

re: object reference not set to an instance of an object (file.move)


When the program breaks, what line does it break on? What line is highlighted and has the yellow arrow pointing at?
kunal pawar's Avatar
Needs Regular Fix
 
Join Date: Oct 2007
Location: Pune, India
Posts: 297
#5: Mar 13 '09

re: object reference not set to an instance of an object (file.move)


I think it will help you to track problem

Expand|Select|Wrap|Line Numbers
  1. string filePath = dlPath + "\\" + set+ "\\ThumbNail.jpg"; 
  2. if (Directory.Exists(tmpDlPath) && File.Exists(tmpDlPath + fileName) && File.Exists(filePath)) 
  3.     try 
  4.     { 
  5.         File.Move(tmpDlPath + fileName, filePath); 
  6.     } 
  7.     catch (Exception ex)
  8.    { } 
  9. else
  10. {
  11.  MessageBox("Source or destination path is not valid");
  12. }
  13.  
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#6: Mar 13 '09

re: object reference not set to an instance of an object (file.move)


Expand|Select|Wrap|Line Numbers
  1. string filePath = dlPath.TrimEnd('\\') + "\\" + set + "\\Thumbnail.jpg";
  2. // This ensures there is only one "\\" whether or not dlPath already had it on the end or not.
  3.  
Quote:
if (Directory.Exists(tmpDlPath) && File.Exists(tmpDlPath + fileName) && File.Exists(filePath))

Quote:
File.Move(tmpDlPath + fileName, filePath);
If you have confirmed that filePath exists in your if statement then it is not reasonable to expect the move to be successful since their is already a file of that name at the target location.
Member
 
Join Date: Feb 2009
Posts: 35
#7: Mar 17 '09

re: object reference not set to an instance of an object (file.move)


Quote:

Originally Posted by tlhintoq View Post

When the program breaks, what line does it break on? What line is highlighted and has the yellow arrow pointing at?

Originally, it was breaking in my Program.cs file (the namespace is edited out):

Expand|Select|Wrap|Line Numbers
  1. namespace XXXXXXXXX
  2. {
  3.     static class Program
  4.     {
  5.         /// <summary>
  6.         /// The main entry point for the application.
  7.         /// </summary>
  8.         [STAThread]
  9.         static void Main()
  10.         {
  11.             Application.EnableVisualStyles();
  12.             Application.SetCompatibleTextRenderingDefault(false);
  13.             Application.Run(new XXXXXXXXX()); // Breaks at this line. 
  14.         }
  15.     }
  16. }
The error is:
TargetInvocationException was unhandled.
Exception has been thrown by the target of an invocation.

I then added in a try-catch:
Expand|Select|Wrap|Line Numbers
  1. static void Main()
  2. {
  3.     Application.EnableVisualStyles();
  4.     Application.SetCompatibleTextRenderingDefault(false);
  5.     try
  6.     {
  7.         Application.Run(new XXXXXXXXX());
  8.     }
  9.     catch (Exception ex)
  10.     {
  11.         MessageBox.Show(ex.InnerException.Message);
  12.     }
  13. }
And now I just get a MessageBox with "Object Reference Not set to an instance of an object" with no line indication. I guessed at where the error was occurring (in one of my background workers) and narrowed it down to that one File.Move line.
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#8: Mar 17 '09

re: object reference not set to an instance of an object (file.move)


Let's start by changing that behavior so you can see where the actual exception is taking place.



Quote:
And now I just get a MessageBox with "Object Reference Not set to an instance of an object"
Just have to set breakpoints in the area of the failure and look at the variables and see which ones are null.



Set a breakpoint and walk it through one command at a time with the F-10 key.
Member
 
Join Date: Feb 2009
Posts: 35
#9: Mar 17 '09

re: object reference not set to an instance of an object (file.move)


Thank You tlhintoq for showing me that method! I was able to track down the null reference! That is a really handy tool! I appreciate your help very much!
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#10: Mar 17 '09

re: object reference not set to an instance of an object (file.move)


My pleasure... So what was it anyway?
Member
 
Join Date: Feb 2009
Posts: 35
#11: Mar 17 '09

re: object reference not set to an instance of an object (file.move)


When the file is moved over to that directory, a listview control is updated. I check the listview to see if it's already there.

Here is my fixed code:

Expand|Select|Wrap|Line Numbers
  1. ListViewItem toRemove = new ListViewItem();
  2. toRemove = lv_sets.FindItemWithText(sName);
  3. if (toRemove != null)
  4. {
  5.     toRemove.Remove();
  6. }
toRemove was sometimes empty and then calling toRemove.Remove() would produce the error.
Reply