Search code examples
androidgoogle-cloud-firestoreautocompletetextview

how to transfer string from autoCompleteTextView in one activity to others using set and get methods


[HELP]

please help me, what i want to do is, get String data from autoCompleteTextView and set it , then get String data in other activity

This is my AutoCompleteTextView code :

//autocomplete
    //Creating the instance of ArrayAdapter containing list of language names
    final ArrayAdapter<String> adapter = new ArrayAdapter<String>
            (this,android.R.layout.select_dialog_item,dapilAuto);
    //Getting the instance of AutoCompleteTextView
    final AutoCompleteTextView actv= (AutoCompleteTextView)findViewById(R.id.nama_dapil);
    actv.setThreshold(1);//will start working from first character
    actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
    actv.setTextColor(Color.BLACK);
    //ketika di pilih autocompletenya
    actv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String selection = (String)parent.getItemAtPosition(position);
            //String selection = actv.getText().toString();
            Dapil dapil = new Dapil(selection);
            dapil.setName_dapil(selection);

        }
    });

This is my get and set class :

 String name_dapil;



    public Dapil(){

    }

    public Dapil(String name_dapil){
        this.name_dapil = name_dapil;

    }

    public String getName_dapil() {
        return name_dapil;
    }
    public void setName_dapil(String name_dapil) {
        this.name_dapil = name_dapil;
    }

and This is how i get String data and implement it into cloud firestore to get document ID :

     Dapil dapil = new Dapil();
     String mDapil = dapil.getName_dapil();

     loadJumlahKursi(mDapil);




 private void loadJumlahKursi(String mDapil){

        DocumentReference kursiRef = firestoreDB.collection("perhitungansuara").document(mDapil);
        kursiRef.get()
                .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                        if(task.isSuccessful()){
                            DocumentSnapshot doc = task.getResult();
                            StringBuilder fields = new StringBuilder("");
                            fields.append("Jumlah Kursi: ").append(doc.get("jumlah_kursi"));
                            mJumlahKursi.setText(fields.toString());
                        } else {
                            Log.d(TAG, "Gagal mendapatkan data:  ", task.getException());
                        }

                    }
                });
    }

and this is the issues

02-06 18:18:27.700 5902-5902/com.example.erlangga.suaraku E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.erlangga.suaraku, PID: 5902 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.erlangga.suaraku/com.example.erlangga.suaraku.HasilActivity}: java.lang.NullPointerException: Provided document path must not be null. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2219) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5045) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException: Provided document path must not be null. at com.google.android.gms.common.internal.zzbq.checkNotNull(Unknown Source) at com.google.firebase.firestore.CollectionReference.document(Unknown Source) at com.example.erlangga.suaraku.HasilActivity.loadJumlahKursi(HasilActivity.java:157) at com.example.erlangga.suaraku.HasilActivity.onCreate(HasilActivity.java:81) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2163) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2269)  at android.app.ActivityThread.access$800(ActivityThread.java:135)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:136)  at android.app.ActivityThread.main(ActivityThread.java:5045)  at java.lang.reflect.Method.invokeNative(Native Method)  at java.lang.reflect.Method.invoke(Method.java:515)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)  at dalvik.system.NativeStart.main(Native Method) 


Solution

  • You are getting the following Exception:

    java.lang.NullPointerException: Provided document path must not be null
    

    because you are passing to the document() method the mDapil object which has the value of null. The reason is that the following line of code:

    dapil.getName_dapil()
    

    returns null.

    If you want to pass mDapil between activities use an Intent. Here is how can be done.