B_CooperA
B_CooperA

Reputation: 659

Cannot output the contents of an EditText element inside onClick method

I'm relatively new at developing applications in Android Studio or Java and recently ran into a problem I just can't figure out. For now, all I'm trying to achieve is to output the content of the EditText field after a Button is been clicked.

Since I will most likely add more buttons to the Activity later on, I thought that it would be more handy to use a generic onClick where you can separate different button actions inside the switch statement.

Here's a working example in which the onClickListener which does not use a generic onClick method:

package com.example.user.myapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    private EditText testText;
    private Button testButton;

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

      // initialize UI elements
      final EditText testText = (EditText) findViewById(R.id.testText);
      testButton = (Button) findViewById(R.id.testButton);

      // set onClick listener 
      testButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
          // it will print the content of testText as long as the "testText" variable is declared as final
          System.out.println(testText.getText().toString().trim());
        }
      });
    }
}

Now, if I try to use a generic onClick method, I will suddenly receive a following error:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference

Here's the code that that causes the error referred above:

package com.example.user.myapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends Activity implements View.OnClickListener {

    private EditText testText;
    private Button testButton;

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

        // initialize the UI elements
        EditText testText = (EditText) findViewById(R.id.testText);
        Button testButton = (Button) findViewById(R.id.testButton);

        // set onClick Listener
        testButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.testButton:                       
                // this will result in an error                                       
                System.out.println(testText.getText().toString().trim());
                break;
            default:
                break;
    }
}

Am I missing something here?

Upvotes: 0

Views: 59

Answers (4)

Lovekush Vishwakarma
Lovekush Vishwakarma

Reputation: 3169

You have declare EditText 2 time, one is globally and another one is inside the onCreate method, and when you are using Edittext outside the onCreate, you are getting global variable which is not initialize, thats why you are getting this error. use this,

public class MainActivity extends Activity implements View.OnClickListener {

    private EditText testText;
    private Button testButton;

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

        // initialize the UI elements
        testText = (EditText) findViewById(R.id.testText);
        testButton = (Button) findViewById(R.id.testButton);

        // set onClick Listener
        testButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.testButton:                       
                // this will result in an error                                       
                System.out.println(testText.getText().toString().trim());
                break;
            default:
                break;
    }
} 

Upvotes: 0

maciekjanusz
maciekjanusz

Reputation: 4775

You never initialize the private EditText testText; because you use a local variable instead of referencing the class field in the following call:

final EditText testText = (EditText) findViewById(R.id.testText);

The onClick(View v) is a method of anonymous class implementing the OnClickListener interface and this method references the uninitialized field variable testText.

To fix this, remove the type declaration of a variable before calling findViewById():

this.testText = (EditText) findViewById(R.id.testText)

Upvotes: 1

tahsinRupam
tahsinRupam

Reputation: 6405

Decalre editText globally:

private EditText testText;

Get the view in onCreate():

testText = (EditText) findViewById(R.id.testText);

Use it in the onClick:

System.out.println(testText.getText().toString().trim());

Upvotes: 1

gmetax
gmetax

Reputation: 3973

on your second code you have defined testText twice

that will work

package com.example.user.myapp;

import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText;

public class MainActivity extends Activity implements View.OnClickListener {

    private EditText testText;
    private Button testButton;

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

        // initialize the UI elements
        testText = (EditText) findViewById(R.id.testText);
        testButton = (Button) findViewById(R.id.testButton);

        // set onClick Listener
        testButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.testButton:                       
                // this will result in an error                                       
                System.out.println(testText.getText().toString().trim());
                break;
            default:
                break;
    }
}

Upvotes: 2

Related Questions