1


Currently I am working on a project(It' a browser). There are 2 layout files one is activity_main.xml and other is url_load_activity.xml. In activity main file there are 10 image views with android:clickable="true" and they are with OnClickListenersin the MainActivity.java. The other XML contains a WebView and a LinearLayout for search bar, home button. I added

Intent yt = new Intent(MainActivity.this, url_load_activity.class);
startActivity(yt);

these 2 lines to each OnClickListener of the image views in MainActivity.java for switching to url_load_activity.xml from activity_main.xml. It successfully switched for url_load_activity.xml. And then I need to load a URL in the url_load_activity.xml while switching to it but when clicked on that image view with webView.loadUrl("https://www.youtube.com");, the application stops and restarts. In the android studio there shows this error

2021-08-30 14:34:58.781 27973-27973/com.blurredtechnology.browser E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.blurredtechnology.browser, PID: 27973
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.loadUrl(java.lang.String)' on a null object reference
        at com.blurredtechnology.browser.MainActivity$3.onClick(MainActivity.java:67)
        at android.view.View.performClick(View.java:7171)
        at android.view.View.performClickInternal(View.java:7148)
        at android.view.View.access$3500(View.java:802)
        at android.view.View$PerformClick.run(View.java:27409)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7642)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:503)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

Other image views are working well without the webView.loadUrl(""); but after adding webView.loadUrl("")to each the error shows always

Savithu
  • 75
  • 9
  • 3
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) Your `webView` isn't initialised (have you called `findViewById` in `onCreate`?), it is `null`, and you are trying to call some method on it (`loadUrl`) - this is a `NullPointerException` which you are facing – snachmsm Aug 30 '21 at 09:14
  • 1
    Please share the code where you are loading the web view. – iCantC Aug 30 '21 at 11:00
  • @snachmsm, Yes I initialised WebView in `onCreate` but the WebView isn't in the `Main_Activity.xml` it's in `url_load_activity.xml` and I initialised WebView in `MainActivity.java` not in `url_load_activity.java`. – Savithu Aug 31 '21 at 04:08
  • 1
    without code of these Activities its hard to guess whats wrong in your code... – snachmsm Aug 31 '21 at 11:15
  • Please provide enough code so others can better understand or reproduce the problem. – Community Sep 01 '21 at 06:23

1 Answers1

0

You shouldn't give empty string to "webview.loadUrl("Here is should filled with string");".

WebView just trying to open url but when try its exploding because empty string meant nothing to loadUrl function.

You should check string before load or don't give empty string to loadUrl function.