Last post Jun 25, 2009 11:07 AM by jbassett
Jun 23, 2009 11:20 AM|jbassett|LINK
I'm trying to pull images from our server but they are written on-the-fly with no extension (like a .jpg or .gif) for instance.
Even if I set the content type like this:
myReq.ContentType = "image/jpg"
...It still fails.
IF I set the content type I get this error:
System.NullReferenceException: Object reference not set to an instance of an object.
If I do not set the content type I get this error:
System.IO.DirectoryNotFoundException: Could not find a part of the path 'd:\imagedirectorytosavein\'.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
...But Regardless I always get an error. If I use an image that has a .jpg or .gif after it....no problem.
Thanks for your help.
Jun 23, 2009 07:21 PM|mbanavige|LINK
please post the code that causes the error.
Jun 23, 2009 07:34 PM|jbassett|LINK
Thanks! Here you go...
Dim b As Byte()
Dim myReq As HttpWebRequest = Nothing
Dim myResp As HttpWebResponse
Dim ExceptionExists As Integer = 0
myReq.ContentType = "image/jpg" ' Sometimes I add this and sometimes I don't
myReq = CType(WebRequest.Create(url), HttpWebRequest) ' THIS SEEMS TO BE THE LINE THAT KILLS IT
myResp = CType(myReq.GetResponse(), HttpWebResponse)
Catch ex As WebException
ExceptionExists = 1
Jun 23, 2009 07:56 PM|mbanavige|LINK
in your example, you're setting the ContentType property of myReq before you have actually created the HttpWebRequest instance, so myReq would be null and that would seem to be the cause of the null reference exception
Jun 25, 2009 11:07 AM|jbassett|LINK
Yep, that was it. Thanks for the help.