7 Replies - 314 Views - Last Post: 25 June 2012 - 08:27 AM Rate Topic: -----

#1 paperclipmuffin  Icon User is offline

  • Disassembling...
  • member icon

Reputation: 13
  • View blog
  • Posts: 944
  • Joined: 16-April 09

Object Reference Confusion

Posted 24 June 2012 - 07:41 PM

Hi guys,

I get an "Object reference not set to an instance of an object" error when running this code (not compiling), even though I've declared the variable only the line before, and there are no other variables with conflicting names. The ZipEntry class is from the Ionic.Zip library. The constructor doesn't take any arguments either.
ZipEntry iwd = new ZipEntry();
iwd.FileName = txtOut.text + "\\" + iwdname + ".iwd"; // This line gives the error



Thanks.

This post has been edited by paperclipmuffin: 24 June 2012 - 07:45 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Object Reference Confusion

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Object Reference Confusion

Posted 24 June 2012 - 07:48 PM

Could you show a bit more code?

Also, have you defined iwdname or txtOut?

This post has been edited by atraub: 24 June 2012 - 08:02 PM

Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2112
  • View blog
  • Posts: 3,230
  • Joined: 21-June 11

Re: Object Reference Confusion

Posted 24 June 2012 - 07:57 PM

View Postpaperclipmuffin, on 25 June 2012 - 04:41 AM, said:

I get an "Object reference not set to an instance of an object" error when running this code (not compiling), even though I've declared the variable only the line before


You're using three variables on the line on the question. You've initialized one of them on the line before. You didn't show us anything about the other two, so one of them might very well be null.
Was This Post Helpful? 0
  • +
  • -

#4 paperclipmuffin  Icon User is offline

  • Disassembling...
  • member icon

Reputation: 13
  • View blog
  • Posts: 944
  • Joined: 16-April 09

Re: Object Reference Confusion

Posted 24 June 2012 - 10:49 PM

txtOut.Text is the text of a Form Textbox, and iwdname is a string passed in function arguments. Both are checked for being (string)null before they are used.

if (txtOut.Text != (string)null && iwdName != (string)null)
{
    // rest of the function


This post has been edited by paperclipmuffin: 24 June 2012 - 10:50 PM

Was This Post Helpful? 0
  • +
  • -

#5 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Object Reference Confusion

Posted 24 June 2012 - 11:26 PM

iwd.FileName = txtOut.text + "\\" + iwdname + ".iwd"; // This line gives the error

If that's the exact line that's giving the error, it's because of the txtOut variable. Should be
iwd.FileName = txtOut.Text + "\\" + iwdname + ".iwd"; // This line gives the error

Was This Post Helpful? 0
  • +
  • -

#6 paperclipmuffin  Icon User is offline

  • Disassembling...
  • member icon

Reputation: 13
  • View blog
  • Posts: 944
  • Joined: 16-April 09

Re: Object Reference Confusion

Posted 24 June 2012 - 11:52 PM

View PostMomerath, on 25 June 2012 - 12:26 AM, said:

iwd.FileName = txtOut.text + "\\" + iwdname + ".iwd"; // This line gives the error

If that's the exact line that's giving the error, it's because of the txtOut variable. Should be
iwd.FileName = txtOut.Text + "\\" + iwdname + ".iwd"; // This line gives the error

Exactly the same, it's not case sensitive in my IDE.
Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4480
  • View blog
  • Posts: 7,803
  • Joined: 08-June 10

Re: Object Reference Confusion

Posted 25 June 2012 - 08:14 AM

C# is case sensitive, regardless of IDE. The compiler for the language is case sensitive.

Regardless, the only way you're going to solve this is to put a breakpoint on your line, and inspect each variable you're using on that line. Inspect iwd, txtOut, and iwdname. If any are null, that's why you have the error. Use the debugger.
Was This Post Helpful? 1
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5509
  • View blog
  • Posts: 11,814
  • Joined: 02-June 10

Re: Object Reference Confusion

Posted 25 June 2012 - 08:27 AM

"Object reference not set to the instance of an object"
Is well covered in this article:
What does this error message mean?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1