]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/commitdiff
Add a ternary operator to unformattedUrl.get() to remove a warning about possible...
authorSoren Stoutner <soren@stoutner.com>
Thu, 29 Oct 2015 20:57:19 +0000 (13:57 -0700)
committerSoren Stoutner <soren@stoutner.com>
Thu, 29 Oct 2015 20:57:19 +0000 (13:57 -0700)
app/src/main/java/com/stoutner/privacybrowser/Webview.java

index 02613821fa21e4b2e3f4601e91e68828b93135fe..1fd1554828b983ada48c87c2f924fc93529d48b1 100644 (file)
@@ -217,7 +217,6 @@ public class Webview extends AppCompatActivity {
         String unformattedUrlString = urlTextBox.getText().toString();
         URL unformattedUrl = null;
         Uri.Builder formattedUri = new Uri.Builder();
         String unformattedUrlString = urlTextBox.getText().toString();
         URL unformattedUrl = null;
         Uri.Builder formattedUri = new Uri.Builder();
-        String scheme;
 
         // Check to see if unformattedUrlString is a valid URL.  Otherwise, convert it into a Duck Duck Go search.
         if (Patterns.WEB_URL.matcher(unformattedUrlString).matches()) {
 
         // Check to see if unformattedUrlString is a valid URL.  Otherwise, convert it into a Duck Duck Go search.
         if (Patterns.WEB_URL.matcher(unformattedUrlString).matches()) {
@@ -234,16 +233,12 @@ public class Webview extends AppCompatActivity {
                 e.printStackTrace();
             }
 
                 e.printStackTrace();
             }
 
-            if (unformattedUrl.getProtocol() != null) {
-                scheme = unformattedUrl.getProtocol();
-            } else {
-                scheme = "http";
-            }
-
-            final String authority = unformattedUrl.getAuthority();
-            final String path = unformattedUrl.getPath();
-            final String query = unformattedUrl.getQuery();
-            final String fragment = unformattedUrl.getRef();
+            // The ternary operator (? :) makes sure that unformattedUrl.get() does not cause a null pointer exception.
+            final String scheme = unformattedUrl != null ? unformattedUrl.getProtocol() : null;
+            final String authority = unformattedUrl != null ? unformattedUrl.getAuthority() : null;
+            final String path = unformattedUrl != null ? unformattedUrl.getPath() : null;
+            final String query = unformattedUrl != null ? unformattedUrl.getQuery() : null;
+            final String fragment = unformattedUrl != null ? unformattedUrl.getRef() : null;
 
             formattedUri.scheme(scheme).authority(authority).path(path).query(query).fragment(fragment);
             formattedUrlString = formattedUri.build().toString();
 
             formattedUri.scheme(scheme).authority(authority).path(path).query(query).fragment(fragment);
             formattedUrlString = formattedUri.build().toString();
@@ -254,7 +249,6 @@ public class Webview extends AppCompatActivity {
             formattedUrlString = "https://duckduckgo.com/?q=" + encodedUrlString;
         }
 
             formattedUrlString = "https://duckduckgo.com/?q=" + encodedUrlString;
         }
 
-        // Place formattedUrlString back in the address bar and load the website.
         mainWebView.loadUrl(formattedUrlString);
 
         // Hides the keyboard so we can see the webpage.
         mainWebView.loadUrl(formattedUrlString);
 
         // Hides the keyboard so we can see the webpage.