title: java.lang.NullPointerException in Android application
body: <p>I am fairly new to java and I have been trying to learn, so I made a webview app for my website that I have published and a few people have reported an error as listed below:</p>
  
  <pre><code>java.lang.NullPointerException
  at com.alexriggs.android.mafiastreetwars.MafiaStreetWarsActivity$MyWebViewClient.shouldOverrideUrlLoading(MafiaStreetWarsActivity.java:87)
  at android.webkit.CallbackProxy.uiOverrideUrlLoading(CallbackProxy.java:227)
  at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:334)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:130)
  at android.app.ActivityThread.main(ActivityThread.java:3704)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:507)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
  at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>No I know enough to know that java.lang.NullPointerException means that a variable is empty but being used as not empty. And after looking back at my code, it should never be empty and I cannot reproduce it. So here is my code, can anyone figure out what is wrong?  And please remember that I am new to Java, so please use as much detail as possible!</p>
  
  <pre><code>package com.alexriggs.android.mafiastreetwars;
  
  import com.alexriggs.android.mafiastreetwars.R;
  
  import android.app.Activity;
  import android.app.AlertDialog;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.net.Uri;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.KeyEvent;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.webkit.WebView;
  import android.webkit.WebViewClient;
  import android.widget.Toast;
  
  public class MafiaStreetWarsActivity extends Activity {
      /** Called when the activity is first created. */
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.menu, menu);
          return true;
      }
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
              case R.id.exit:{
                  Toast.makeText(this, "Thanks For playing Mafia Street Wars!", Toast.LENGTH_LONG).show();
                  finish();
                  break;
              }
              case R.id.select_and_copy:
              {
                  Toast.makeText(getApplicationContext(), "Select Text by dragging over the text you want to copy.", Toast.LENGTH_LONG).show();
                  selectAndCopyText(myWebView);
                  return true;
              }
          }
          return true;
      }
      private void selectAndCopyText(WebView view) {
          try
          {
              KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
                                                      KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
              shiftPressEvent.dispatch(view);
          }
          catch (Exception e)
          {
              Log.e("dd", "Exception in emulateShiftHeld()", e);
          }
  
      }
      final Activity activity = this;
      WebView myWebView;
  
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          AlertDialog alertDialog = new AlertDialog.Builder(this).create();
          alertDialog.setTitle("Remember...");
          alertDialog.setMessage("This app is just for mobile access. This game is meant to be played on the computer at MafiaStreetWars.com");
          alertDialog.setButton("Thanks for the reminder!", new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int which) {
                // here you can add functions
             }
          });
          alertDialog.setIcon(R.drawable.icon);
          alertDialog.show();
          myWebView = (WebView) findViewById(R.id.webview);
          myWebView.getSettings().setJavaScriptEnabled(true);
          myWebView.setWebViewClient(new MyWebViewClient());
          myWebView.getSettings().setBuiltInZoomControls(true);
          myWebView.getSettings().setLoadWithOverviewMode(true);
          myWebView.getSettings().setUseWideViewPort(true);
          myWebView.getSettings().setPluginsEnabled(true);
          Toast.makeText(getApplicationContext(), "Page is loading... Please wait!", Toast.LENGTH_LONG).show();
          myWebView.loadUrl("http://mafiastreetwars.com/?pk_campaign=Android&amp;pk_kwd=MainApp");
      }
      private class MyWebViewClient extends WebViewClient {
  
  
          public boolean shouldOverrideUrlLoading(WebView view, String url) {
              if (Uri.parse(url).getHost().equals("mafiastreetwars.com")) {
                  return false;
              }  
              // Otherwise, give the default behavior (open in browser)
              Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
              startActivity(intent);
              return true;
          }
          }
      @Override
      public boolean onKeyDown(int keyCode, KeyEvent event) {  
          if ((keyCode == KeyEvent.KEYCODE_BACK) &amp;&amp; myWebView.canGoBack()) {  
              myWebView.goBack();  
              return true;  
          }  
          return super.onKeyDown(keyCode, event);  
      }  
  }
  </code></pre>