-1

After searching, finally, I find out that I should use an interface to pass a list of objects from an activity to a fragment. before that, I made my object class Perceilable to send in as an argument to fragment, but it seems that it's not a good approach because I have to initiate my fragment for the first time without setting any argument and after my list of the object is ready I can pass it. So now I want to send my list via Interface but I get this error :

java.lang.NullPointerException: Attempt to invoke interface method 'void com.x.x.Interfaces.CurrenciesSync.syncCurrencies(java.util.List)' on a null object reference
    at com.x.x.DashboardActivity$1.onResponse(DashboardActivity.java:124)

here is my interface file codes:

public interface CurrenciesSync {


public void syncCurrencies(List<CurrencyModel> currencies);

}

here is the way I set currencies List in DashboardActivity to syncCurrencies method:

public class DashboardActivity extends AppCompatActivity {
        CurrenciesSync currenciesSync;

currenciesSync.syncCurrencies(currencies); } And how I implement it's method :

public void syncCurrencies(List<CurrencyModel> currencies) {
 // using currencies here
}
Naveed Ahmad
  • 6,627
  • 2
  • 58
  • 83
cup code
  • 49
  • 8

1 Answers1

1

You must initialize the object:

    CurrenciesSync currenciesSync = new CurrenciesSync() {
        @Override
        public void syncCurrencies(List<CurrencyModel> currencies) {
           // your code    
        }
    };
    currenciesSync.syncCurrencies();

This declaration CurrenciesSync currenciesSync; creates a null CurrenciesSync object.

  • You mean in activity when I want to pass the List of objects? but it gives me error `'syncCurrencies(List)' in 'Anonymous class derived from com.x.x.Interfaces.CurrenciesSync' clashes with 'syncCurrencies(List)' in 'com.x.x.Interfaces.CurrenciesSync'; both methods have same erasure, yet neither overrides the other` – cup code Jul 29 '18 at 15:03
  • `currenciesSync` is `null` so `currenciesSync.syncCurrencies(currencies);` throws the exception. Use the code I posted in my answer, with your implementation of `syncCurrencies`. –  Jul 29 '18 at 15:07
  • When I try to replace `currenciesSync.syncCurrencies(currencies);` with your code I get the error in previous comment – cup code Jul 29 '18 at 15:10
  • Remove also the `CurrenciesSync currenciesSync;` declaration, I also made an edit to my answer change List to List –  Jul 29 '18 at 15:13
  • You must also remove the previous code of `public void syncCurrencies(List currencies) {}` –  Jul 29 '18 at 15:14
  • Thank you so much my problem solved but I have another problem now! the interface method is not being executed in Fragment! – cup code Jul 29 '18 at 15:30
  • You can call `currenciesSync.syncCurrencies();` in the same scope where you declared `CurrenciesSync currenciesSync = new CurrenciesSync()` –  Jul 29 '18 at 15:47
  • I already did that but in destination (a Fragment) `@Override public void syncCurrencies(List currencies) { //codes }` // codes does not run – cup code Jul 29 '18 at 15:51
  • Can you post the code, including all declarations? –  Jul 29 '18 at 15:53
  • yes but where can I post them?! – cup code Jul 29 '18 at 15:56
  • If it is a lot of code I suggest to start another question relevant to the new problem –  Jul 29 '18 at 15:57
  • Thank you so much! I'll start another question – cup code Jul 29 '18 at 15:58