-1

When I Call the public void inside My form, where my public form is declared, It works fine. But when i call it from another class, it doesnt work..? I have done like so in my class.

otherForm oForm = new otherForm();
oForm.thisvoid();

In the public void, I'm loading a Url in my webView. And as I said before, calling it from my Form, where it is declared, it works. So there can't really be something wrong with the function in itself.

This is my public void

public void thisvoid()
    {
        try
        {
            webview.LoadUrl("http://localhost:5000);
        } catch (Exception ee)
        {
            Console.WriteLine("Error");
            Console.WriteLine(ee);
        }
    }

The error is:

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

Dave
  • 2,684
  • 2
  • 21
  • 38
Bob Jensen
  • 37
  • 3
  • 8
  • 1
    By "it does not work" do you mean "does not compile" or "does not display form and the like"? – GrayCat Nov 03 '18 at 11:17
  • The function does not work, as it is supposed to, but it DOES work as it's supposed to, when i call it from the form, where it is declared. – Bob Jensen Nov 03 '18 at 11:18
  • is webview object properly created in the constructor? – Dave Nov 03 '18 at 11:19
  • Does it write anything to the console? (In catch block) – GrayCat Nov 03 '18 at 11:21
  • @GrayCat Yes it does. – Bob Jensen Nov 03 '18 at 11:25
  • But what does it write? Object reference not set to an instance of an object? – GrayCat Nov 03 '18 at 11:26
  • right now: System.NullReferenceException: Object reference not set to an instance of an object – Bob Jensen Nov 03 '18 at 11:35
  • 1
    So the problem occurs at `webview.LoadUrl("http://localhost:5000);` check your webview object. – Luthfi Nov 03 '18 at 11:46
  • I kinda figured that out. But there is not a problem when i just call it in my FOrm1 where it is declared, so it must be a problem with connecting it together – Bob Jensen Nov 03 '18 at 11:59
  • You have initialized the webview somewhere else in the Form1. This initialization is not called if you create the object somewhere else (outside this form). Initialize the object in your thisvoid method or in the object constructor. – Dave Nov 03 '18 at 12:08
  • Maybe it doesn't work because you are invoking it from another thread. – roozbeh S Nov 03 '18 at 12:11
  • @Dave Could you give an example? – Bob Jensen Nov 03 '18 at 12:21
  • in the "thisvoid" method, point the "webview" to its location before you use it, for example webview = new WebView();. This is very basic stuff and if you don't know about it, please read more about the OOP. – Dave Nov 03 '18 at 12:32
  • i already have that..? the WebView webview = new WebView(); – Bob Jensen Nov 03 '18 at 12:33
  • Yes, but you don't have it in the thisvoid method or the class constructor. So when you invoke the method from "somewhere else", the webview == null. – Dave Nov 03 '18 at 12:34
  • Ahh, so, where should i put "object webview" ?, in the form where i declared my public void, the webview is in that form. – Bob Jensen Nov 03 '18 at 12:43
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – BJ Myers Nov 05 '18 at 05:50

1 Answers1

2

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:

  1. Runtime encounters var otherForm = new otherForm();
  2. It searches for (in this case) parameterless constructor
  3. It executes statements in the constructor
  4. Among those there is creation of webview object
  5. 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

GrayCat
  • 1,749
  • 3
  • 19
  • 30