DayllCasquejo DayllCasquejo - 4 months ago 284x
Android Question

Android Studio error: "Method getText() must be called from the UI Thread, currently inferred thread is worker

i'm creating a CRUD operation in android studio but i kept getting errors. the error is when i check the LogCat this is what they show me

line 156-158

1907-1931/com.example.casquejo.loginadmin E/AndroidRuntime﹕ FATAL
EXCEPTION: AsyncTask #2
Process: com.example.casquejo.loginadmin, PID: 1907
java.lang.RuntimeException: An error occured while executing
Caused by: java.lang.NullPointerException atcom.example.casquejo.loginadmin.NewProductActivity$CreateNewProduct.doInBackground(
at com.example.casquejo.loginadmin.NewProductActivity$CreateNewProduct.doInBackground(

can someone help me with this or can someone give an idea how to fix this` below is the code for my java class EditProductActivity.class

package com.example.casquejo.loginadmin;

import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

* Created by Casquejo on 9/14/2015.
public class NewProductActivity extends Activity {
private ProgressDialog pDialog;

JSONParser jsonParser = new JSONParser();
EditText inputName;
EditText inputPrice;
EditText inputDesc;

private static String url_create_product = "";

private static final String TAG_SUCCESS = "success";

public void onCreate(Bundle savedInstanceState) {

inputName = (EditText) findViewById(;
inputPrice = (EditText) findViewById(;
inputDesc = (EditText) findViewById(;

Button btnCreateProduct = (Button) findViewById(;
btnCreateProduct.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {
String name = inputName.getText().toString();
String price = inputPrice.getText().toString();
String description = inputDesc.getText().toString();
new CreateNewProduct().execute(name, price,description);

class CreateNewProduct extends AsyncTask<String, String, String> {

protected void onPreExecute() {
pDialog = new ProgressDialog(NewProductActivity.this);
pDialog.setMessage("Creating Product..");


protected String doInBackground(String... args) {

String name = args[0],
price = args[1],
description = args[2];

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("price", price));
params.add(new BasicNameValuePair("description", description));

JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);

Log.d("Create Response", json.toString());

try {
int success = json.getInt(TAG_SUCCESS);

if (success == 1) {
Intent i = new Intent(getApplicationContext(), AllProductsActivity.class);
else {

} catch (JSONException e) {

return null;

protected void onPostExecute(String file_url) {



the ide is referring to

  String name = txtName.getText().toString();
  String price = txtPrice.getText().toString();
  String description = txtDesc.getText().toString();

reading the values shouldn't be a problem, but in order to get rid of this warning/error, you can move it into the onClick and pass the values through execute(). E.g.

btnSave.setOnClickListener(new View.OnClickListener() {

    public void onClick(View arg0) {
        String name = txtName.getText().toString();
        String price = txtPrice.getText().toString();
        String description = txtDesc.getText().toString();
        new SaveProductDetails().execute(name, price, description);

when doInBackground is invoked, you can read those bac, through the former params, String... args. The three dots construct stays for varargs, and varargs can be access like an array, using the [] notation. In the case of the example,

args[0] contains the value of name, args[1] contains the value of price and args[2] contains the value of description.