1

For one of my school projects, I'm building my own app using Android Studio. This will be my first app. I'm also new to Android Studio and have just started learning Java. I have built some games using python.

Since I'm new to Java and app development the app is fairly basic. It provides the user with mathematical rules and formulas which they can use to study for tests and such.

I've done research on this issue and I've noticed that everyone has different code which is why I was curious to see if I could get some answers as to what the problems are with my code.

I'm currently stuck on this error when I try to run my app:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: mcshreker.learnthemalllta, PID: 2876
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{mcshreker.learnthemalllta/mcshreker.learnthemalllta.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                      at android.app.ActivityThread.-wrap11(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5417)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.FloatingActionButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
                      at mcshreker.learnthemalllta.MainActivity.onCreate(MainActivity.java:120)
                      at android.app.Activity.performCreate(Activity.java:6237)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Here is my code (MainActivity.Java):

package mcshreker.learnthemalllta;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;

import static mcshreker.learnthemalllta.R.layout.activity_main;
import static mcshreker.learnthemalllta.R.layout.activity_main3;
import static mcshreker.learnthemalllta.R.layout.content_main;

@SuppressWarnings("ALL")
public class MainActivity extends AppCompatActivity {

    private Button button;
    public Button button2;
    private Button button4;
    private Button button5;
    public Button button3;
    public Button button7;


    private void init6(){
        button = (Button)findViewById(R.id.button7);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent toy6 = new Intent(MainActivity.this,Main8Activity.class);
                startActivity(toy6);
            }
        });
    }

    private void init5(){
        button = (Button)findViewById(R.id.button3);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent toy5 = new Intent(MainActivity.this,Main7Activity.class);
                startActivity(toy5);
            }
        });
    }

    private void init4(){
        button = (Button)findViewById(R.id.button2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent toy4 = new Intent(MainActivity.this,Main5Activity.class);
                startActivity(toy4);
            }
        });
    }

    private void init3(){
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent toy3 = new Intent(MainActivity.this,Main4Activity.class);
                startActivity(toy3);
            }
        });
    }



    private void init2(){
        button5 = (Button)findViewById(R.id.button5);
        button5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent toy2 = new Intent(MainActivity.this,Main3Activity.class);

                startActivity(toy2);
            }
        });
    }

    private void init(){
        button4 = (Button)findViewById(R.id.button4);
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent toy = new Intent(MainActivity.this,Main2Activity.class);

                startActivity(toy);
            }
        });
    }


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

        init();
        init2();
        init3();
        init4();
        init5();
        init6();
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
Julia Path
  • 2,356
  • 15
  • 21
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – denvercoder9 Nov 13 '16 at 20:02
  • Did you delete the Floating Action Button that is included in the activity by default? – Luke Joshua Park Nov 13 '16 at 20:03

1 Answers1

1

This may not be the exact problem, but if you actually read the error, it tells you that fab is null, which indicates to me that you probably deleted the floating action button that is present in your application.

Try removing this portion of code:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                .setAction("Action", null).show();
    }
});
Luke Joshua Park
  • 9,527
  • 5
  • 27
  • 44