This is an archived forum post. The information may be outdated. Contact us if you have any questions.

Object reference not set error occurred.

pink wrote on 2011-11-19:
Hi,
I found the error as I attached file. Although I write username and API key,it doesn't work and object reference not set error always occurred.
Pls kindly help me.

Thanks in advance.
Pink
support wrote on 2011-11-21:
Hello,

the screenshot is not readable. Please can you attach either a screenshot in a higher resolution or to post directly your code?
pink wrote on 2011-11-22:
Sry,cos of pic size reduction.
Error caused as below :

protected void Page_Load(object sender, EventArgs e)
{
try
{
FileStream fileStream;

// create an API client instance

pdfcrowd.Client client = new pdfcrowd.Client("myusername", "myAPIKey");

// convert an HTML string and store the PDF into a memory stream
//MemoryStream memStream = new MemoryStream();
//string html = "<head></head><body>My HTML Layout</body>";
//client.convertHtml(html, memStream);

// convert an HTML file
fileStream = new FileStream("file.pdf", FileMode.CreateNew);
client.convertFile("d:/test.html", fileStream); =====> ERROR:Object reference not set to an instance of an object.
fileStream.Close();
int ntokens = client.numTokens();

}
catch (pdfcrowd.Error why)
{
System.Console.WriteLine(why.ToString());
}
support wrote on 2011-11-24:
Here is your code with comments removed:
try 
{ 
  FileStream fileStream; 
  pdfcrowd.Client client = new pdfcrowd.Client("myusername", "myAPIKey"); 

  fileStream = new FileStream("file.pdf", FileMode.CreateNew); 
  client.convertFile("d:/test.html", fileStream); =====> ERROR:Object reference not set to an instance of an object. 
} 
catch (pdfcrowd.Error why) 
{ 
  System.Console.WriteLine(why.ToString()); 
}


It is not clear to me how client can be null. I'm under the impression that new can't return null and could only be aborted by an exception, in which case the client.convertFile(...) line would never be reached.

Please could you debug the code and provide us with more details on what is going on?
khkremer wrote on 2012-07-14:
I am running into the same problem, but i"m trying to convert a URL. The interesting thing is that I can call methods in client to set the header and footer without a problem, but when I call the convertURI method, I get the exception. This only happens when the ASP.NET app is deployed, when I try it in VS2008, it works correctly.

Here is the debug output from the server:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 66: // convert a web page and write the generated PDF to a memory stream
Line 67: MemoryStream Stream = new MemoryStream();
Line 68: client.convertURI(theURL, Stream);
Line 69:
Line 70: // set HTTP response headers

Source File: c:\MemberSites\MemberSites_AspSpider_Org\khkremer\webroot\Default.aspx.cs Line: 68

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
pdfcrowd.Client.do_request(String uri, Stream out_stream, Object data, String content_type) +613
pdfcrowd.Client.call_api(String uri, Stream out_stream, String src) +119
pdfcrowd.Client.convert(Stream out_stream, String method, String src) +39
pdfcrowd.Client.convertURI(String uri, Stream out_stream) +16
_Default.convertPage(String theURL) in c:\MemberSites\MemberSites_AspSpider_Org\khkremer\webroot\Default.aspx.cs:68
_Default.Page_Load(Object sender, EventArgs e) in c:\MemberSites\MemberSites_AspSpider_Org\khkremer\webroot\Default.aspx.cs:120
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +91
System.Web.UI.Control.LoadRecursive() +74
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207
support wrote on 2012-07-17:
@khkremer: thanks for reporting the problem. Please can you try the attached patched pdfcrowd.dll and let us know if it resolves the issue.
khkremer wrote on 2012-07-22:
Thanks. This version does not throw the exception.

Thanks.