Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
After trying the following code,
private void readHtmlFiles_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Iframe.Children.Remove(documentViewer1);
WebBrowser browser = new WebBrowser();
browser.Width = 700;
Uri uri = new Uri("inequalities.htm",UriKind.Relative);
Stream source = System.Windows.Application.GetResourceStream(uri).Stream;
browser.NavigateToStream(source);
Iframe.Children.Add(browser);
}

it told me Object reference not set to an instance of an object.

the file "inequalities.htm" is in the debug folder where the .exe file is located.

I changed the UriKind to Absolute and copied the file to a folder in the root directory of my solution folder, change the Uri to "html/inequalities.htm" and gave me this

Invalid URI: The format of the Uri could not be determined.

What should i do or what am I doing wrong?
Posted
Comments
Sergey Alexandrovich Kryukov 15-Sep-14 10:43am    
Please, in what line?
—SA
matmape 15-Sep-14 10:48am    
Its on the line
Stream source = System.Windows.Application.GetResourceStream(uri).Stream;

1 solution

This exception is totally unrelated to using UriKind. The line you used should work (I cannot guarantee it gives you the correct URI, of course :-). Please see below.)

You did not show where the exception with the message "Object reference not set to an instance of an object" is thrown.

Not to worry. This is one of the very easiest cases to detect and fix. It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which requi8res this member/variable to be non-null, but in fact it appears to be null. Simply execute it under debugger, it will stop the execution where the exception is thrown. Put a break point on that line, restart the application and come to this point again. Evaluate all references involved in next line and see which one is null while it needs to be not null. After you figure this out, fix the code: either make sure the member/variable is properly initialized to a non-null reference, or check it for null and, in case of null, do something else.

Please see also: want to display next record on button click. but got an error in if condition of next record function "object reference not set to an instance of an object"[^].

Sometimes, you cannot do it under debugger, by one or another reason. One really nasty case is when the problem is only manifested if software is built when debug information is not available. In this case, you have to use the harder way. First, you need to make sure that you never block propagation of exceptions by handling them silently (this is a crime of developers against themselves, yet very usual). The you need to catch absolutely all exceptions on the very top stack frame of each thread. You can do it if you handle the exceptions of the type System.Exception. In the handler, you need to log all the exception information, especially the System.Exception.StackTrace:
http://msdn.microsoft.com/en-us/library/system.exception.aspx[^],
http://msdn.microsoft.com/en-us/library/system.exception.stacktrace.aspx[^].

The stack trace is just a string showing the full path of exception propagation from the throw statement to the handler. By reading it, you can always find ends. For logging, it's the best (in most cases) to use the class System.Diagnostics.EventLog:
http://msdn.microsoft.com/en-us/library/system.diagnostics.eventlog.aspx[^].

[EDIT #1]

Besides, there are no cases when hard-coded file paths can be useful. Please see my past answers:
http://www.codeproject.com/Questions/454593/How-to-find-my-programs-directory
How to find my programs directory (executable directory),
How to find my programs directory (current directory, "special folders"),
How to access a file from html folder of root directoty[^].

For files, use "file://" URI scheme. Please see: http://en.wikipedia.org/wiki/URI_scheme[^].

[EDIT #2, after OP's clarification]

If you need to load a document from embedded resource to browser, please see this answer:
http://stackoverflow.com/questions/1254605/ms-webbrowser-embedded-html-resource-res-protocol[^].

Good luck,
—SA
 
Share this answer
 
v3
Comments
matmape 15-Sep-14 10:45am    
Its on the line
Stream source = System.Windows.Application.GetResourceStream(uri).Stream;
Sergey Alexandrovich Kryukov 15-Sep-14 11:08am    
Okay, System.Windows.Application.GetResourceStream(uri) should be null (did you check it up under the debugger?).
And then, you try to dereference it by accessing the member Stream, which gives you the said exception.
Check it up; make sure the resource stream is not null. This is not how resource streams are accessed. Why taking this stream, by the way? What is the expected type of the data in the resource?
—SA
matmape 15-Sep-14 12:04pm    
a html content
the uri contained the specified resource name in my case "inequalities.htm"
matmape 15-Sep-14 12:05pm    
sorry for the late reply
was thinking the page reloads by itself
Sergey Alexandrovich Kryukov 15-Sep-14 21:15pm    
Please see EDIT#2...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900