0

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:

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)

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!

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&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) && myWebView.canGoBack()) {  
            myWebView.goBack();  
            return true;  
        }  
        return super.onKeyDown(keyCode, event);  
    }  
}
2
  • 5
    Please learn to use a debugger. It will tell you where your problem is. Commented Oct 8, 2013 at 14:16
  • @AdamArold Like I said, I cannot reproduce the problem.. Commented Oct 8, 2013 at 14:44

3 Answers 3

1

It's probably due to Uri.parse(url).getHost() returning null. Check this before you call equals().

Sign up to request clarification or add additional context in comments.

Comments

1

Line in question if (Uri.parse(url).getHost().equals("mafiastreetwars.com")) {

Looks like your url is null, or the parse result is null but that is less likely. Check to see if things are null before using them!

Comments

1

I suggest you to do it a little bit more nullsave:

if (url==null)
   return false;
Uri lUri = Uri.parse(url);
if ("mafiastreetwars.com".equals(lUri.getHost())) {
            return false;
} 

that solves your technical problem but maybe not youre business case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.