According to what you wrote in comments webview
object is not initialized in constructor. Add its initialization to constructor of otherForm
and it should work.
My guess as to why it works when you call it from within otherForm
is that you initialize it somewhere along the way.
EXAMPLE (EDIT):
When you instantiate your otherForm
with a new
keyword from other class e.g
var oF = new otherForm();
what runtime does it searches for parameterless constructor that is special method of a class that is executed upon creation of class instance. It should be named as a class.
Now the second part - the webview
you use is an object - be it control, other form or just some object. It is stored a field of your class. But, unless you create an instance of it, it is null
- meaning that this object does not exist, you only have its potential handle. So you have to create this object like this (class names are completely made up):
public class otherForm
{
WebView webview;
public otherForm()
{
webview = new WebView();
}
}
Of course, the WebView constructor itself might require more parameters that you would need to provide.
So what happens here is:
- Runtime encounters
var otherForm = new otherForm();
- It searches for (in this case) parameterless constructor
- It executes statements in the constructor
- Among those there is creation of
webview
object
- When it later calls
webView
in any method on that instance the object exists and can be used.
What you missed probably was point number 4. Example of how to include it can be found above.
More reads:
Stack Overflow on NullReferenceException
Some examples of constructors