I need to save in Realm a JSON that I've saved in Assets folder.
So I've decided to do it in this way:
  private void putCountryIntoRealm()
{
    Observable.create(new Observable.OnSubscribe<String>()
    {
        public void call(Subscriber<? super String> subscriber)
        {
            try
            {
                InputStream is = getActivity().getAssets().open("countries.json");
                Reader inRead = new InputStreamReader(is);
                Gson gson = new Gson();
                JsonReader reader = new JsonReader(inRead);
                reader.setLenient(true);
                reader.beginArray();
                while (reader.hasNext())
                {
                    CountryObject ct = gson.fromJson(reader, CountryObject.class);
                    country.add(ct);
                    System.out.println("size ct: "+country.size());
                    subscriber.onNext(reader.toString());
                }
                reader.endArray();
                reader.close();
                subscriber.onCompleted();
            }
            catch (IOException e)
            {
             subscriber.onError(e);
            }
        }
    }).subscribeOn(Schedulers.newThread())
      .subscribe(new Subscriber<String>()
      {
          @Override
          public void onCompleted()
          {
              getActivity().runOnUiThread(new Runnable()
              {
                  @Override
                  public void run()
                  {
                      realm.executeTransactionAsync(new Realm.Transaction()
                      {
                          float total = 0;
                          float size = country.size();
                          String progressCount;
                          public void execute(Realm mRealm)
                          {
                              Countries countries = new Countries();
                              int i;
                              System.out.print("countries size: "+size);
                              for (i = 0; i < country.size(); i++)
                              {
                                  total = (i/size * 100);
                                  progressCount = String.format("%.0f",total);
                                  countries.setId(country.get(i).getId());
                                  countries.setCode(country.get(i).getCode());
                                  countries.setName(country.get(i).getName());
                                  countries.setContinent(country.get(i).getContinent());
                                  countries.setKeywords(country.get(i).getKeywords());
                                  countries.setWikipedia_link(country.get(i).getWikiLink());
                                  mRealm.insertOrUpdate(countries);
                                  getActivity().runOnUiThread(new Runnable()
                                  {
                                      @Override
                                      public void run()
                                      {
                                          //Log.d("%",progressCount);
                                          dialog = new ProgressDialog(getActivity());
                                          dialog.setTitle("Salvataggio country in corso");
                                          dialog.setMessage(progressCount+"%");
                                      }
                                  });
                              }
                          }
                      },new Realm.Transaction.OnSuccess() {
                          @Override
                          public void onSuccess()
                          {
                              Log.wtf("va","Ok");
                              if(dialog.isShowing())
                                  dialog.dismiss();
                          }
                      }, new Realm.Transaction.OnError()
                      {
                          @Override
                          public void onError(Throwable error)
                          {
                              error.printStackTrace();
                          }
                      });
                  }
              });
          }
          @Override
          public void onError(Throwable e)
          {
           e.printStackTrace();
          }
          @Override
          public void onNext(String s)
          {
            System.out.println(s);
          }
      });
}
but I have this error:
W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'long com.plenitudesrls.aviocalc.helpful.CountryObject.getId()' on a null object reference
In fact, I print logs and the size in onNext() is 245, in onCompleted() is null.
Why I have this error? I have another case where this works good.
Thanks
EDIT: it works good, the problem was a MalformedJson that cause the crash
