EditText to String returns Null

Related searches

As title says, when i execute this app, WebView connects to "https://null/" insted of the string i put on the EditText, i cant figure out the problem

MainActivity activity

public class MainActivity extends AppCompatActivity {
EditText web;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    web = (EditText) findViewById(R.id.web);
    String webstr = web.getText().toString();
    Intent int2 = new Intent(MainActivity.this, WebView.class);
    int2.putExtra("123", webstr);
    Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           Intent intent = new Intent(getApplicationContext(), WebView.class);
            v.getContext().startActivity(intent);
        }
    });

  }
}

WebView Activity

public class WebView extends AppCompatActivity {
private android.webkit.WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_view);
    Intent intent = getIntent();
    String web1 = intent.getStringExtra("123");
    webView = findViewById(R.id.webview);
    webView.getSettings().setDomStorageEnabled(true);
    webView.setWebViewClient(new WebViewClient());
    webView.loadUrl("https://" + web1);
    Toast.makeText(WebView.this, "Connected", Toast.LENGTH_SHORT).show();

 } 
}

You need to get the value from your EditText when you click the button, not within onCreate.

Also you're not using the same Intent, so you're not actually passing that value.

Button button = findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getApplicationContext(), WebView.class);
        String webstr = web.getText().toString();
        intent.putExtra("123", webstr);
        v.getContext().startActivity(intent);
    }
});

EditText getText() returns empty string, Please move this. final String Textusername = username.getText().toString(); final String Textpassword = password.getText().toString();. use the findViewByID at the onCreate, you need to cast the view into an EditText object, before using it, thats why it throws:" Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference" – Israel Cruz Sep 28 '15 at 17:18

You call wrong intent and with gettext at the wrong place here below you can see modified code

public class MainActivity extends AppCompatActivity {
EditText web;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = (EditText) findViewById(R.id.web);


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


  String webstr = web.getText().toString();
  Intent int2 = new Intent(MainActivity.this, WebView.class);
  int2.putExtra("123", webstr);
  v.getContext().startActivity(int2);
    }
});

  }
}

Android getText from EditText return NULL, getText() can never return null, because as soon as an EditText is constructed, mText is given a value of an empty string. However, as StinePike pointed out,� If you were going through the debug mode, have in mind that it has two places where it stores values. In my case EditText had mText (Spannable String Builder) and it's sub field (also mText) is the one with ID. The spannable string builder will return value of .toString(). The other one will return value of .getText().

You have placed the statement in 'onCreate()' method. Place it inside the 'Button.onClickListener'.

Does EditText.getText() ever returns null?, I think the lint check is telling you something slightly differently: String value = input. You should do the null check on the return of EditText#getText() instead. on Feb 16, 2015. If the field has no value present it will be set to NULL and if you try to run the toString () method you will receive this error. Check for null prior to retrieving the value: if (mUsername.getText() != null) { myUsernameStr = mUsername.getText().toString(); }

public class MainActivity extends AppCompatActivity {
    EditText web;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        web = (EditText) findViewById(R.id.web);
        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String webstr = web.getText().toString();
                Intent intent = new Intent(MainActivity.this, WebView.class);
                intent.putExtra("123", webstr);
                MainActivity.this.startActivity(intent);
            }
        });

  }
}

getText() from EditText: is it possible to get a 'null'?, usernameField); it is ambiguous which EditText field you are accessing. If you access the wrong one, you will get a null value instead of a string� Similar Threads - EditText return null return the graduated axes of the graph to the center in GraphView beirou , Apr 2, 2020 , in forum: Android Lounge

NullPointerException when using EditText getText().toString , The getText() method of EditText return a object of Editable, not String. In order to retrieve the text of EditText, toString() of Editable can be used. EditText myEditText = (EditText)findViewById(R.id.myedittext);

We have used getText() method to get the text entered in the EditText views. But getText() method returns an Editable instance and therefore we have typecasted it to convert it into String for further use. This can be done by using the toString() method.

But when I pass second argument as "Pramod Deore" i.e. String then it runs successfully. But if I pass username (i.e which is get from EditText) then it throws Exception. Have you tried to print the value you read from the EditText and see what you are getting? Is it null? Is it empty?