-3

I do not know what am I doing wrong.

java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at si.skorajporocena.skorajporocena.PrvaPrijavaActivity$1.onClick(PrvaPrijavaActivity.java:51)

Here is the code:

EditText pass;


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

        pass = (EditText) findViewById(R.id.etPassword);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.floatingActionButton);
        viewPager = (ViewPager) this.findViewById(R.id.pager);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(i==0){
                    if (pass.getText().toString().isEmpty()) { // <-- this is the 51 line
                        Snackbar.make(view, "Niste vnesli aktivacijske kode.", Snackbar.LENGTH_SHORT).
                                setAction("Action", null).show();
                    }
                    else {
                        i=1;
                        viewPager.setCurrentItem(i);
                    }
                }
                else if(i>0) {
                    i++;
                    viewPager.setCurrentItem(i);

                    if(i==3){
                        Intent intent;
                        intent = new Intent(getApplicationContext(),MainActivity.class);
                        startActivity(intent);
                        finish();
                    }
                    //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    //        .setAction("Action", null).show();
                }
            }
        });
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Fisheroid
  • 33
  • 10

4 Answers4

-1

Check that the view R.id.etPassword is present in your layout.

Dawnkeeper
  • 2,844
  • 1
  • 25
  • 41
-1

Your pass variable is null. The most likely cause is a layout error with R.id.etPassword

Dawnkeeper
  • 2,844
  • 1
  • 25
  • 41
-1

I have solved the problem. Problem was that pass = (EditText) findViewById(R.id.etPassword); was outside an if statement where I need variable pass.

Here it is what the code look now:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.floatingActionButton);
        viewPager = (ViewPager) this.findViewById(R.id.pager);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(i==0){
                    pass = (EditText) findViewById(R.id.etPassword);
                    if (pass.getText().toString().isEmpty()) { // <-- this is the 51 line
                        Snackbar.make(view, "Niste vnesli aktivacijske kode.", Snackbar.LENGTH_SHORT).
                                setAction("Action", null).show();
                    }
                    else {
                        i=1;
                        viewPager.setCurrentItem(i);
                    }
                }
Fisheroid
  • 33
  • 10
-1

I think you are using id 'etPassword' from other .xml file. Thats way code compile successfully but run time when click on floating button null pointer exception occurred.