NullPointerException when setting an onClickListener

setonclicklistener null pointer exception
onclicklistener android studio
implementing onclicklistener in android
how to solve java lang nullpointerexception in android
android studio java lang nullpointerexception attempt to invoke virtual method
null pointer exception in android studio

I'm trying to make my own browser, but I'm stuck. I try to send URL with string as data type using intent from my main activity to the web activity. This is my web activity java code:

public class webView extends MainActivity {

private WebView halamanWeb;
private ProgressBar progressBar;
private EditText searchBox;
private ImageButton tombolCari2;
private String Url;

private String getUrl(){
    return Url = "https://" + searchBox.getText().toString();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.surfing_layout);

    searchBox = (EditText) findViewById(R.id.searchBoxAtas);
    tombolCari2 = (ImageButton) findViewById(R.id.tombolCari);
    progressBar = (ProgressBar) findViewById(R.id.bar);
    halamanWeb = (WebView) findViewById(R.id.webLayout);

    Intent test = getIntent();
    final String url = "https://" + test.getStringExtra("key");

    halamanWeb.getSettings().setJavaScriptEnabled(true);
    halamanWeb.getSettings().setDisplayZoomControls(true);
    halamanWeb.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            progressBar.setVisibility(View.VISIBLE);
            progressBar.setProgress(newProgress);
             if (newProgress == 100) {
               progressBar.setVisibility(View.GONE);
            }
            halamanWeb.loadUrl(url);
            halamanWeb.setWebViewClient(new MyWebLaunch());
        }

    });

    tombolCari2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            halamanWeb.getSettings().setJavaScriptEnabled(true); 
            halamanWeb.getSettings().setDisplayZoomControls(true);
            halamanWeb.setWebChromeClient(new WebChromeClient(){
                @Override
                public void onProgressChanged(WebView view, int newProgress){
                    progressBar.setVisibility(View.VISIBLE);
                    progressBar.setProgress(newProgress);
                    if(newProgress == 100){
                        progressBar.setVisibility(View.GONE);
                    }
                }

            });
            halamanWeb.loadUrl(getUrl());
            halamanWeb.setWebViewClient(new MyWebLaunch());
        }
    });

}
private class MyWebLaunch extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return super.shouldOverrideUrlLoading(view, url);
    }
}
}

this is my main activity java code :

public class MainActivity extends AppCompatActivity {

//deklarasi
private EditText searchBox;
private ImageButton tombolCari;
private WebView halamanWeb;
private ProgressBar progressBar;
private String Url;

//deklarasi fungsi
private String getUrl(){
    return Url = "https://" + searchBox.getText().toString();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    searchBox = (EditText) findViewById(R.id.searchBoxAtas);
    tombolCari = (ImageButton) findViewById(R.id.tombolCari);
    progressBar = (ProgressBar) findViewById(R.id.bar);

    tombolCari.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Url = getUrl();
            Intent test = new Intent(v.getContext(), webView.class);
            test.putExtra("key", Url);
            startActivity(test);
        }
    });

}
}

when I run that code, got error code that say:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

Can anyone help me with the intent idea, I want to pass a string URL from my main activity to the web activity and then use that to open the site using the URL. How can I get out from the null pointer on image button? I would like some help! Thanks!

Edit : This is my log when i start the code, it runs but when i input something and i pressed the ImageButton it crashed.

     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at com.example.johno.myapplication.webView.onCreate(webView.java:54)
    at android.app.Activity.performCreate(Activity.java:7136)
    at android.app.Activity.performCreate(Activity.java:7127)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

Check if you have something like below in your surfing_layout.xml:

    <ImageButton
        android:id="@+id/tombolCari"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

If it is there, just check if the ID of that image button is tombolCari. If it is something else, just change it to android:id="@+id/tombolCari"

Android App crash because of Null Pointer exception on setting , Android App crash because of Null Pointer exception on setting onClick listener in Editor class. Forums View. General Started by Newbie jake.***  Stack Overflow Public questions and answers; Teams Private questions and answers for your team; Enterprise Private self-hosted questions and answers for your enterprise; Talent Hire technical talent


You are overriding MainActivity view with WebView activity and you are expecting MainActivity view should be alive.All your Mainactivity UI elements which are present in R.layout.activity_main will be out of scope.To fix this move to activity_main content to surfing_layout.

  Remove setContentView(R.layout.activity_main) and  from MainActivity and make surev 
   surfing_layout has tombolCari imageButton.

OnClick(view v) throws a null pointer exception. Can anyone help , click(view v) is throwing a null pointer exception. have been trying to sort it for hours. but to no avail. I would appriciate if anyone could help me  Home » Android » java – Null pointer Exception on .setOnClickListener java – Null pointer Exception on .setOnClickListener Posted by: admin May 10, 2020 Leave a comment


Now you have two xml one for MainActivity => "activity_main", and the second for webView => "surfing_layout".

I see that you extended MainActivity in webView Class and added the same widget and write it again in webView Class, then if you forget to add ImageButton from activity_main.xml to surfing_layout.xml, or forget to add the same id for ImageButton to surfing_layout.xml

Add this code to surfing_layout.xml

<ImageButton
    android:id="@+id/tombolCari"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

[Solved] Null point exeption at view.setonclicklistener, Look at the error, it is clearly telling you that an object is null, which you are trying to use to set an OnClick listener event. So use your debugger  Null pointer Exception on .setOnClickListener Tag: java , android , nullpointerexception , onclicklistener I am having an issue with a click listener for a login modal submit button.


Tips to fixing a NullPointerException in Java (Android) – FincoApps, To solve the NullPointerException, you simply need to assign a value to the variable involved. If it is an public void onClick(View v) {. //TODO:  But if I don't implement View.OnClickListener and do create a new instance of View.OnClickListener as a parameter of setOnClickListener() method, it does work fine. Method 2: chkCmd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //required code here } });


Null pointer exception When making an intent crash?, NullPointerException: Attempt to invoke virtual method 'void android.widget. View$OnClickListener)' on a null object reference #28 auth object */ private void setupFirebaseAuth(){ Log.d(TAG, "setupFirebaseAuth: setting up firebase auth. February 20, 2020 Java Leave a comment. Questions: I am setting a textview as HTML retrieved from Firebase database. When I am setting the question the I am adding another value called qid to the textview.


java.lang.NullPointerException: Attempt to invoke virtual method , Description: Null pointer exception when inflating MaterialCardView with android:​onClick set in xml Expected behavior: MaterialCardView  When setting an Adapter it Gives a Null Pointer Exception The Line The code is The Line JAVA The null Pointer Exception is given Around mealRating.setAdapter(spinnerAdapter) The Place Where Its Assigned is,, mealRating = ((Spinner) v.findViewById(R.id.rating_spinner));