AndroidDev20TX
AndroidDev20TX

Reputation: 69

URL Query Retrofit 2

I am designing a cryptocurrency tracker and this base URL will not work with specific data I need. I read my error is most likely caused by the API URL being called incorrectly.

Error

java.lang.NullPointerException: Attempt to invoke interface method 
'java.lang.Object java.util.List.get(int)' on a null object reference

base URL

https://rest.coinapi.io/ 

http request

GET /v1/symbols?filter_symbol_id={filter_symbol_id}

My code

String BASE_URL = "https://rest.coinapi.io/";

@GET("v1/symbols")
Call<List<Coin>> getCoin(@Query("filter_symbol_id") String filter_symbol_id);
}

Code calling API

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);
    setContentView (R.layout.activity_main);

    final ListView listView = findViewById (R.id.listView);

    Retrofit retrofit = new Retrofit.Builder ()
            .baseUrl (Api.BASE_URL)
            .addConverterFactory (GsonConverterFactory.create ())
            .build ();

    Api api = retrofit.create (Api.class);

    Call<List<Coin>> call = api.getCoin ("filter_symbol_id");

    call.enqueue (new Callback<List<Coin>> () {
        @Override
        public void onResponse(Call<List<Coin>> call, 
Response<List<Coin>> response) {

            List<Coin> coin = response.body ();

            String[] coinName = new String[10];

            for (int i = 0; i < 10; i++) {


                coinName[i] = coin.get (i).getsymbol_id ();


                listView.setAdapter (
                        new ArrayAdapter<String> (
                                getApplicationContext (),
                                android.R.layout.simple_list_item_1,
                                coinName

                        )
                );

            }
        }


        @Override
        public void onFailure(Call<List<Coin>> call, Throwable t) {
            Toast.makeText (getApplicationContext (), t.getMessage (), 
Toast.LENGTH_SHORT).show ();
        }
        });

    }

}

Coin.java

package com.dev20tx.android.cryptohero;

public class Coin {

private String symbol_id;


public Coin(String symbol_id) {
    this.symbol_id = symbol_id;


}

public String getsymbol_id() {
    return symbol_id;
}
}

I found many helpful stackoverflow posts that helped me fetch other data, but this one has me confused. Could someone point me in the right direction?

PROBLEM SOLVED: I used a different URL "v1/assets", which fixed the NullPointerException, updated my code across java files & it works. For some reason the API v1/symbols is not currently working at coinapi.io

Upvotes: 0

Views: 433

Answers (2)

deepak raj
deepak raj

Reputation: 3801

The error must be in your Coin class. So please post Coin.class. I think the NullPointerException is because the GSON can't Serialize the Coin objects, may be @SerializedName("") is missing. Or else Show Coin.class code. (Remember apikey parameter is also missing, so the response will be 401 Unauthorized but it doesn't cause any NullPointerException).

package com.dev20tx.android.cryptohero;

public class Coin {

@SerializedName("symbol_id") //replace with your response field.
private String symbol_id;


public Coin(String symbol_id) {
    this.symbol_id = symbol_id;
}

public String getsymbol_id() {
    return symbol_id;
}
}

Upvotes: 1

Bek
Bek

Reputation: 8471

You're missing api_key

@GET("v1/symbols")
Call<List<Coin>> getCoin(@Query("filter_symbol_id") String filter_symbol_id, @Query("apikey") String apiKey);

For more info visit https://docs.coinapi.io/#authorization

Upvotes: 1

Related Questions