title: Pass data from firebase to object class
body: <p>The issue I have is there're always Null Pointer Exception when I settext textview, so I cannot setText to my view.</p>
  
  <p>I think the object didn't fill the field from getData method. How to fix this?</p>
  
  <p>I am following this step <a href="https://firebase.google.com/docs/firestore/query-data/listen#listen_to_multiple_documents_in_a_collection" rel="nofollow noreferrer">https://firebase.google.com/docs/firestore/query-data/listen#listen_to_multiple_documents_in_a_collection</a> and this <a href="https://stackoverflow.com/questions/52172309/parcelable-object-passing-from-android-activity-to-fragment">Parcelable object passing from android activity to fragment</a>.</p>
  
  <p>This is the error output from android studio console</p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.my.plkbi, PID: 24337
      java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.my.plkbi.Layanan.getSyarat()' on a null object reference
          at com.my.plkbi.SubMainFragment.onCreateView(SubMainFragment.java:78)
          at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
          at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
          at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
          at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
          at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
          at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
          at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
          at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
          at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
          at androidx.fragment.app.FragmentManagerImpl$2.run(FragmentManagerImpl.java:150)
          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:6119)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
  </code></pre>
  
  <p>EDIT: Add constructor to add object from other object, I follow this <a href="https://stackoverflow.com/questions/869033/how-do-i-copy-an-object-in-java">How do I copy an object in Java?</a>
  This is the object about the field database and Parcelable</p>
  
  <pre><code>public class Layanan implements Parcelable {
      private String Nama;
      private String Syarat;
      private String Langkah;
      private String Lampiran;
  
      public Layanan(){
          //this("connect to internet", "connect to internet", "connect to internet", "connect to internet");
      }
  
  //Constructor to add value from another object
      public Layanan(Layanan another) {
          this.nama = another.nama;
          this.syarat = another.syarat;
          this.langkah = another.langkah;
          this.lampiran = another.lampiran;
          this.deskripsi = another.deskripsi;
      }
  
      protected Layanan(Parcel in) {
          Nama = in.readString();
          Syarat = in.readString();
          Langkah = in.readString();
          Lampiran = in.readString();
      }
  
      public static final Creator&lt;Layanan&gt; CREATOR = new Creator&lt;Layanan&gt;() {
          @Override
          public Layanan createFromParcel(Parcel in) {
              return new Layanan(in);
          }
  
          @Override
          public Layanan[] newArray(int size) {
              return new Layanan[size];
          }
      };
  
      public static Layanan newInstance() {
  
          Bundle args = new Bundle();
  
          Layanan fragment = new Layanan();
          fragment.setArguments(args);
          return fragment;
      }
  
      private void setArguments(Bundle args) {
  
      }
  
      public String getNama() {
          return Nama;
      }
  
      public String getSyarat() {
          return Syarat;
      }
  
      public void setSyarat(String syarat) {
          Syarat = syarat;
      }
  
      public String getLangkah() {
          return Langkah;
      }
  
      public void setNama(String nama) {
          Nama = nama;
      }
  
      public void setLangkah(String langkah) {
          Langkah = langkah;
      }
  
      public String getLampiran() {
          return Lampiran;
      }
  
      public void setLampiran(String lampiran) {
          Lampiran = lampiran;
      }
  
      @Override
      public int describeContents() {
          return 0;
      }
  
      @Override
      public void writeToParcel(Parcel dest, int flags) {
          dest.writeString(Nama);
          dest.writeString(Syarat);
          dest.writeString(Langkah);
          dest.writeString(Lampiran);
      }
  }
  </code></pre>
  
  <p>This is how i get data from Firebase, it's inside in activity</p>
  
  <pre><code>Layanan UP;
  Layanan GU;
  Layanan LS;
  
  
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          if(savedInstanceState == null) {
              //initMF();
              MainFragment homeFragment = MainFragment.newInstance();
  
              Bundle extras = new Bundle();
  
  
              getData("UP");
              Log.d(TAG, "This is the value name of " + UP.getNama()); //Output: This is the value name of null
              extras.putParcelable(UPParcelable, UP);
              if (UP.getSyarat()!=null) Log.d(TAG, "Value Added"); 
              if (UP.getSyarat()==null) Log.d(TAG, "Value not Added"); //Output: Value not Added
  
              homeFragment.setArguments(extras);
              getSupportFragmentManager().beginTransaction().add(R.id.main_activity, homeFragment).commit();
          }
  ...
  ...
      public void getData(final String p){
          mFirebase.collection("panduan_layanan")
                  .whereEqualTo("nama", p)
                  .addSnapshotListener(new EventListener&lt;QuerySnapshot&gt;() {
  
                      @Override
                      public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException e) {
                          if (e != null){
                              Log.w(TAG, "onEvent:error", e);
                              return;
                          }
                          Layanan newLayanan = new Layanan();
                          for (QueryDocumentSnapshot doc : value) {
                              if (doc.get("nama") != null &amp;&amp; doc.get("syarat") != null &amp;&amp; doc.get("langkah") != null &amp;&amp; doc.get("lampiran")!= null){
                                  newLayanan.setNama(doc.getString("nama"));
                                  newLayanan.setLampiran(doc.getString("lampiran"));
                                  newLayanan.setLangkah(doc.getString("langkah"));
                                  newLayanan.setSyarat(doc.getString("syarat"));
                                  Log.d(TAG, "Update data: Success" + p);
                                  Log.d(TAG, "This is the " + p + " syarat" + newLayanan.getSyarat());
                                  if (newLayanan.getSyarat() != null) Log.d(TAG, "new layanan is" + newLayanan.getNama()); //new layanan isUP, it is work too
                              }
                          }
                          if (p.equals("UP")) UP = new Layanan(newLayanan);
                          Log.d(TAG, "the" + UP.getNama());  //Output theUP so it is work in here
                          if (p.equals("GU")) GU = new Layanan(newLayanan);
                          if (p.equals("LS")) LS = new Layanan(newLayanan);
                      }
                  });
      }
  ...
  </code></pre>
  
  <p>Thank you so much</p>