0
 protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);


      f.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String url = "https://www.google.co.uk/";

                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(url));
                    startActivity(i);
                }
            });

//This is what i have so far but it is not loading the URL on the emulator it keeps coming up with:

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

I have tried most things and i cant figure it out for the life of me any help would be appreciated

Shadab Ansari
  • 7,022
  • 2
  • 27
  • 45
Adam
  • 19
  • 1
  • 1
  • 3
  • 1
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Fyodor Volchyok Apr 05 '16 at 22:03

3 Answers3

1

This is because you have not initialised your f button. did you initialise it with f = (Button) findViewById(R.id.yourButton);? It is complaining that your button is null

Joel Min
  • 3,387
  • 3
  • 19
  • 38
1

Use -

(findViewById(R.id.Fire)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = "https://www.google.co.uk/";

                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url));
                startActivity(i);
            }
        });
Shadab Ansari
  • 7,022
  • 2
  • 27
  • 45
1

I have done that (sorry not in initial code), yes it is coming back saying:

android.view.View$OnClickListener)' on a null object reference

ACTUAL CODE

Button f = (Button) findViewById(R.id.Fire);

     f.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String url = "https://www.google.co.uk/";

                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(url));
                    startActivity(i);
                }
            });
Adam
  • 19
  • 1
  • 1
  • 3