title: Initialize Interface from Fragment implements Interface
body: <p>I 've an <code>Activity</code>(<strong>MainActivity.java</strong>) , a <code>Fragment</code>(<strong>HomeFragment.java</strong>) inside <code>Activity</code>(<strong>MainActivity.java</strong>) and an <code>interface</code>(<strong>WebViewListener</strong>). My <code>Fragment</code> implements <code>interface</code> and has a <code>WebView</code> inside it. </p>
  
  <p>My Interface is:</p>
  
  <p><strong>WebViewListener</strong> </p>
  
  <pre><code>public interface WebViewListener {
      public void interactWithWebView();
  }
  </code></pre>
  
  <p>My Fragment is:</p>
  
  <p><strong>HomeFragment.java</strong></p>
  
  <pre><code>public class HomeFragment extends Fragment implements WebViewListener {
    private WebView webView;
  
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_home, container, false);
        webView = (WebView) v.findViewById(R.id.webView);
        ...
        webView.loadUrl("file:///android_asset/dropdown.html");
        return v;
    }
  
  
    @Override
    public void interactWithWebView() {
      webView.loadUrl("https://www.google.co.in/");
    }
  
  }
  </code></pre>
  
  <p>In My <code>Activity</code> I have a <code>Button</code>, on click of that <code>Button</code> I intialize interface and call interface method. (In interface method I load google url in <code>WebView</code>).</p>
  
  <p>My Activity is:</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          Button btn= (Button ) findViewById(R.id.button1);
          btn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                 // Intialize interface and call interface method...
              }
          });
      }
  }
  </code></pre>
  
  <p><strong>Here is my Question:</strong></p>
  
  <p>In MainActivity on <code>Button</code> click if I intialize <code>WebViewListener</code> interface and call its method like below</p>
  
  <pre><code>WebViewListener webViewListener=new HomeFragment();
  webViewListener.interactWithWebView();
  </code></pre>
  
  <p><code>WebView</code> gives <code>NullPointerException</code> in <code>interactWithWebView()</code> method.</p>
  
  <p>and if I intialize WebViewListener interface and call its method like this:</p>
  
  <pre><code>WebViewListener webViewListener = (WebViewListener) getSupportFragmentManager().findFragmentByTag(fragmentName);
  webViewListener.interactWithWebView();
  </code></pre>
  
  <p><code>WebView</code> load google url pretty much fine in <code>interactWithWebView()</code> method. What is the reason behind it?</p>