-1

I use this code:

NewDesign.CardImage.Image.Save(System.IO.Path.GetDirectoryName(NewDesign.SaveFileDialog1.FileName) + "\" + My.Settings.SaveLoc + "_cardimage" + ".png")

My.settings.SaveLoc is literally just a string which contains a filename, for example: test.

It's value is set using:

My.Settings.SaveLoc = System.IO.Path.GetFileName(SaveFileDialog1.FileName)

It worked in the past but suddenly out of nowhere it stopped working.

I used the breakpoints and SaveFileDialog1.FileName is exactly where expected, in this case "R:\My Pictures\Test Folder", also SaveLoc contains the value I expect it to.

But when it's attempting to save the image, it gives me the error of "Object reference not set to an instance of an object" and I honestly don't know what I could've possibly done wrong.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Dominik H
  • 122
  • 7

1 Answers1

1

NewDesign.CardImage.Image is almost certainly not initialized. Perhaps this control can't be accessed that way until the handle is created. Some of the image containers have bad behavior like that.

Incidentally,

NewDesign is probably a form in which you dropped a SaveFileDialog on. The ability to access non-static member by form name is VB6 compatibility function that's best NOT used.

Joshua
  • 40,822
  • 8
  • 72
  • 132
  • What can I do instead? If I use a DIm or ReadOnly variable to reference the NewDesign form I'm only getting errors about stackOverFlowExceptions and what not. – Dominik H Jan 03 '16 at 02:17
  • Update: I created a reference to the form, ran the program, it showed errors, deleted the reference, and it works again. What on earth is going on? Edit: that was a lie, it works in a way that it shows absolutely no errors and just skips the saving image part – Dominik H Jan 03 '16 at 02:19
  • Sounds like a very serious code structure problem. – Joshua Jan 03 '16 at 03:14
  • Oh no, is that the only issue it could be? – Dominik H Jan 03 '16 at 03:18
  • StackoverflowException at New Something is usually really bad. – Joshua Jan 03 '16 at 03:21
  • Hmm I just discovered that they are basically referring to each other, the save image class is referring to NewDesign and the NewDesign class is referring to the save image class simultaneously, that appears to be causing this error. But how would I reference both without this error being caused? – Dominik H Jan 03 '16 at 03:33
  • edit in a lot more so we can see it. – Joshua Jan 03 '16 at 03:36