0

App crashes without giving any error. I have attached API with this page when i hit the register button it crashes instead of saving the data into the database. i got 2 java files

java file

public class Subscription_page extends AppCompatActivity {
    TextView label_name, label_user_name ,label_email, label_vendor_type, label_address, label_fb_link, label_insta_link, label_website_link;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subscription_page);
    label_name= (EditText) findViewById(R.id.label_name);
    label_user_name= (EditText) findViewById(R.id.label_username);
    label_email= (EditText) findViewById(R.id.label_email);
    label_vendor_type = (EditText) findViewById(R.id.vendor_type);
    label_address = (EditText) findViewById(R.id.address);
    label_fb_link = (EditText) findViewById(R.id.fb_link);
    label_insta_link = (EditText) findViewById(R.id.insta_link);
    label_website_link= (EditText) findViewById(R.id.website_link);

}

public void subscription_register(View view) {

    String name  = label_name.getText().toString();
    String user_name = label_user_name.getText().toString();
    String email = label_email.getText().toString();
    String vendor_type = label_vendor_type.getText().toString();
    String address = label_address.getText().toString();
    String fb_link = label_fb_link.getText().toString();
    String insta_link = label_insta_link.getText().toString();
    String website_link = label_website_link.getText().toString();

    String method = "register";

    vendor_register_background bg = new vendor_register_background(this);
    bg.execute(name,user_name,email,vendor_type,address,fb_link,insta_link,website_link);
    //finish();

}
}

other file

class vendor_register_background extends AsyncTask <String,Void,String>
{

Context ctx;
vendor_register_background(Context ctx)
{
    this.ctx = ctx;
}

@Override
protected void onPostExecute(String result)
{
    Toast.makeText(ctx,result, Toast.LENGTH_LONG).show();
}


@Override
protected String doInBackground(String... params)
{

    String register_url = "http://192.168.64.2/best/phpapi/register_vendor.php";
    String results="";


        String name = params[0];
        String user_name = params[1];
        String email = params[2];
        String vendor_type = params[3];
        String address = params[4];
        String fb_link = params[5];
        String insta_link = params[6];
        String website_link = params[7];


        try {
            URL url = new URL(register_url);
            HttpURLConnection http = (HttpURLConnection) url.openConnection();
            http.setRequestMethod("POST");
            http.setDoInput(true);
            http.setDoOutput(true);

            OutputStream ops = http.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(ops, "UTF-8"));

            String data = URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" +
                    URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&" +
                    URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8") + "&" +
                    URLEncoder.encode("vendor_type", "UTF-8") + "=" + URLEncoder.encode(vendor_type, "UTF-8") + "&" +
                    URLEncoder.encode("address", "UTF-8") + "=" + URLEncoder.encode(address, "UTF-8") + "&" +
                    URLEncoder.encode("fb_link", "UTF-8") + "=" + URLEncoder.encode(fb_link, "UTF-8") + "&" +
                    URLEncoder.encode("insta_link", "UTF-8") + "=" + URLEncoder.encode(insta_link, "UTF-8") + "&" +
                    URLEncoder.encode("website_link", "UTF-8") + "=" + URLEncoder.encode(website_link, "UTF-8");

            writer.write(data);
            writer.flush();
            writer.close();
            ops.close();


            InputStream ips = http.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(ips, "ISO-8859-1"));
            String line = "";
            while ((line = reader.readLine()) != null) {
                results += line;
            }

            reader.close();
            ips.close();
            http.disconnect();
            return results;


        } catch (MalformedURLException e) {
            results = e.getMessage();
        } catch (IOException e) {
            results = e.getMessage();
        }


        return results;
    }
}

the logcat file

--------- beginning of crash

2020-05-28 23:53:05.839 1904-1904/com.example.bestfiveinc E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.bestfiveinc, PID: 1904 java.lang.IllegalStateException: Could not execute method for android:onClick at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390) at android.view.View.performClick(View.java:5610) at android.view.View$PerformClick.run(View.java:22265) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) at android.view.View.performClick(View.java:5610)  at android.view.View$PerformClick.run(View.java:22265)  at android.os.Handler.handleCallback(Handler.java:751)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6077)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference at com.example.bestfiveinc.Subscription_page.subscription_register(Subscription_page.java:30) at java.lang.reflect.Method.invoke(Native Method)  at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)  at android.view.View.performClick(View.java:5610)  at android.view.View$PerformClick.run(View.java:22265)  at android.os.Handler.handleCallback(Handler.java:751)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6077)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

  • 4
    If your app is crashing, then there should be a stack trace from the crash in the logcat that will help to determine the issue. Have a look at [this Stack Overflow post](https://stackoverflow.com/questions/23353173) and [this developer page](https://developer.android.com/studio/debug/am-logcat) for help in finding that. – Mike M. May 29 '20 at 02:10
  • `Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference at com.example.bestfiveinc.Subscription_page.subscription_register(Subscription_page.java:30)` – Bek May 29 '20 at 04:00
  • That means that at least one of those `TextView`s is null in `subscription_register()`, which means that its `findViewById()` call is returning null, which means that the `R.id` can't be found in the `activity_subscription_page`. Are you sure all of those `TextView`s are in `activity_subscription_page`, and that all of those IDs are correct? It looks like whichever one you're using on line 30 is causing the current crash. – Mike M. May 29 '20 at 04:02
  • `Subscription_page.java:30` throwing NPE. – Bek May 29 '20 at 04:04

0 Answers0