无法运行程序,onResponse(Retrofit)中出现错误

时间:2019-05-05 13:39:31

标签: android api retrofit

我已尝试基于此tutorial制作应用 现在,我只想从模型类中获取“基础”和“日期”。但是我收到了这个错误。

2019-05-05 15:27:54.338 12623-12623/com.example.test_app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test_app, PID: 12623
java.lang.NullPointerException
    at java.util.Objects.requireNonNull(Objects.java:203)
    at java.util.Arrays$ArrayList.<init>(Arrays.java:3741)
    at java.util.Arrays.asList(Arrays.java:3728)
    at com.example.test_app.MainActivity$1.onResponse(MainActivity.java:54)
    at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:71)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

这是我的代码:

MainActivity

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private ArrayList<currencyExchange> data;
private DataAdapter adapter;

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

    initViews();
}

private void initViews() {
    recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view);
    recyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(layoutManager);
    loadJSON();
}

private void loadJSON() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://api.exchangeratesapi.io/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    RequestInterface request = retrofit.create(RequestInterface.class);
    Call<JSONResponse> call = request.getJSON();
    call.enqueue(new Callback<JSONResponse>() {
        @Override
        public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {

            JSONResponse jsonResponse = response.body();
            data = new ArrayList<>(Arrays.asList(jsonResponse.getCurrency())); <-- here's the error according to logcat
            adapter = new DataAdapter(data);
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onFailure(Call<JSONResponse> call, Throwable t) {
            Log.d("Error",t.getMessage());
        }
    });
 }
}

模型类

public class currencyExchange {
private String base,date,currencyNationality;
private double currencyValue;
Rates rates;

public currencyExchange(String currencyNationality, double currencyValue) {
    this.currencyNationality = currencyNationality;
    this.currencyValue = currencyValue;
}

public void setBase(String base) {
    this.base = base;
}

public void setDate(String date) {
    this.date = date;
}

public void setRates(Rates ratesObject) {
    this.rates = ratesObject;
}

public void setCurrencyNationality(String currencyNationality) {
    this.currencyNationality = currencyNationality;
}

public String getBase() {
    return base;
}

public String getDate() {
    return date;
}

public Rates getRates() {
    return rates;
}

public String getCurrencyNationality() {
    return currencyNationality;
}

}

class Rates {
private double currencyValue;
private double BGN;
private double NZD;
private double ILS;
private double RUB;
private double CAD;
private double USD;
private double PHP;
private double CHF;
private double ZAR;
private double AUD;
private double JPY;
private double TRY;
private double HKD;
private double MYR;
private double THB;
private double HRK;
private double NOK;
private double IDR;
private double DKK;
private double CZK;
private double HUF;
private double GBP;
private double NXN;
private double KRW;
private double ISK;
private double SGD;
private double BRL;
private double PLN;
private double INR;
private double RON;
private double CNY;
private double SEK;

public void setCurrencyValue(double currencyValue) {
    this.currencyValue = currencyValue;
}

public List<currencyExchange> getCurrencyValuesList(){
    List<currencyExchange> currencyValues = new ArrayList<>();
    currencyValues.add(new currencyExchange("BGN",getAUD()));
    currencyValues.add(new currencyExchange("NZD",getNZD()));
    currencyValues.add(new currencyExchange("ILS",getILS()));
    currencyValues.add(new currencyExchange("RUB",getRUB()));
    currencyValues.add(new currencyExchange("CAD",getCAD()));
    currencyValues.add(new currencyExchange("USD",getUSD()));
    currencyValues.add(new currencyExchange("PHP",getPHP()));
    currencyValues.add(new currencyExchange("CHF",getCHF()));
    currencyValues.add(new currencyExchange("ZAR",getZAR()));
    currencyValues.add(new currencyExchange("AUD",getAUD()));
    currencyValues.add(new currencyExchange("JPY",getJPY()));
    currencyValues.add(new currencyExchange("TRY",getTRY()));
    currencyValues.add(new currencyExchange("HKD",getHKD()));
    currencyValues.add(new currencyExchange("MYR",getMYR()));
    currencyValues.add(new currencyExchange("THB",getTHB()));
    currencyValues.add(new currencyExchange("HRK",getHRK()));
    currencyValues.add(new currencyExchange("NOK",getNOK()));
    currencyValues.add(new currencyExchange("IDR",getIDR()));
    currencyValues.add(new currencyExchange("DKK",getDKK()));
    currencyValues.add(new currencyExchange("CZK",getCZK()));
    currencyValues.add(new currencyExchange("HUF",getHUF()));
    currencyValues.add(new currencyExchange("GBP",getBGN()));
    currencyValues.add(new currencyExchange("NXN",getNXN()));
    currencyValues.add(new currencyExchange("KRW",getKRW()));
    currencyValues.add(new currencyExchange("ISK",getISK()));
    currencyValues.add(new currencyExchange("SGD",getSGD()));
    currencyValues.add(new currencyExchange("BRL",getBRL()));
    currencyValues.add(new currencyExchange("PLN",getPLN()));
    currencyValues.add(new currencyExchange("INR",getINR()));
    currencyValues.add(new currencyExchange("RON",getRON()));
    currencyValues.add(new currencyExchange("CNY",getCNY()));
    currencyValues.add(new currencyExchange("SEK",getSEK()));
    return currencyValues;

}


public void setBGN(double BGN) {
    this.BGN = BGN;
}

public void setNZD(double NZD) {
    this.NZD = NZD;
}

public void setILS(double ILS) {
    this.ILS = ILS;
}

public void setRUB(double RUB) {
    this.RUB = RUB;
}

public void setCAD(double CAD) {
    this.CAD = CAD;
}

public void setUSD(double USD) {
    this.USD = USD;
}

public void setPHP(double PHP) {
    this.PHP = PHP;
}

public void setCHF(double CHF) {
    this.CHF = CHF;
}

public void setZAR(double ZAR) {
    this.ZAR = ZAR;
}

public void setAUD(double AUD) {
    this.AUD = AUD;
}

public void setJPY(double JPY) {
    this.JPY = JPY;
}

public void setTRY(double TRY) {
    this.TRY = TRY;
}

public void setHKD(double HKD) {
    this.HKD = HKD;
}

public void setMYR(double MYR) {
    this.MYR = MYR;
}

public void setTHB(double THB) {
    this.THB = THB;
}

public void setHRK(double HRK) {
    this.HRK = HRK;
}

public void setNOK(double NOK) {
    this.NOK = NOK;
}

public void setIDR(double IDR) {
    this.IDR = IDR;
}

public void setDKK(double DKK) {
    this.DKK = DKK;
}

public void setCZK(double CZK) {
    this.CZK = CZK;
}

public void setHUF(double HUF) {
    this.HUF = HUF;
}

public void setGBP(double GBP) {
    this.GBP = GBP;
}

public void setNXN(double NXN) {
    this.NXN = NXN;
}

public void setKRW(double KRW) {
    this.KRW = KRW;
}

public void setISK(double ISK) {
    this.ISK = ISK;
}

public void setSGD(double SGD) {
    this.SGD = SGD;
}

public void setBRL(double BRL) {
    this.BRL = BRL;
}

public void setPLN(double PLN) {
    this.PLN = PLN;
}

public void setINR(double INR) {
    this.INR = INR;
}

public void setRON(double RON) {
    this.RON = RON;
}

public void setCNY(double CNY) {
    this.CNY = CNY;
}

public void setSEK(double SEK) {
    this.SEK = SEK;
}


public double getBGN() {
    return BGN;
}

public double getNZD() {
    return NZD;
}

public double getILS() {
    return ILS;
}

public double getRUB() {
    return RUB;
}

public double getCAD() {
    return CAD;
}

public double getUSD() {
    return USD;
}

public double getPHP() {
    return PHP;
}

public double getCHF() {
    return CHF;
}

public double getZAR() {
    return ZAR;
}

public double getAUD() {
    return AUD;
}

public double getJPY() {
    return JPY;
}

public double getTRY() {
    return TRY;
}

public double getHKD() {
    return HKD;
}

public double getMYR() {
    return MYR;
}

public double getTHB() {
    return THB;
}

public double getHRK() {
    return HRK;
}

public double getNOK() {
    return NOK;
}

public double getIDR() {
    return IDR;
}

public double getDKK() {
    return DKK;
}

public double getCZK() {
    return CZK;
}

public double getHUF() {
    return HUF;
}

public double getGBP() {
    return GBP;
}

public double getNXN() {
    return NXN;
}

public double getKRW() {
    return KRW;
}

public double getISK() {
    return ISK;
}

public double getSGD() {
    return SGD;
}

public double getBRL() {
    return BRL;
}

public double getPLN() {
    return PLN;
}

public double getINR() {
    return INR;
}

public double getRON() {
    return RON;
}

public double getCNY() {
    return CNY;
}

public double getSEK() {
    return SEK;
 }
}

JSONResponse类

public class JSONResponse {

private currencyExchange[] currency1;

public currencyExchange[] getCurrency() {
    return currency1;
 }
}

RequestInterface

public interface RequestInterface {

@GET("latest")
Call<JSONResponse> getJSON();
}

DataAdapter class

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {

private ArrayList<currencyExchange> list;

public DataAdapter(ArrayList<currencyExchange> currencyExchanges){
    this.list = currencyExchanges;
}

@Override
public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.currency_row,parent,false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(DataAdapter.ViewHolder holder, int position){
    holder.currencyNationality.setText(list.get(position).getDate());
}

@Override
public int getItemCount() {
    return list.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{
    private TextView currencyNationality,valueOfCurrency;


    public ViewHolder(View view){
        super(view);
        currencyNationality = (TextView)view.findViewById(R.id.tv_name);
        valueOfCurrency = (TextView)view.findViewById(R.id.tv_version);
    }

    }
  }

这可能是什么问题?我的主要目的是显示某些货币的国籍及其价值,但就目前而言,仅出于测试目的,我想显示数据和基准。这是我在此应用中使用的api

将基本网址更改为

后出错
https://api.exchangeratesapi.io/latest/

2019-05-05 17:44:32.788 4420-4420/com.example.test_app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test_app, PID: 4420
java.lang.NullPointerException: Attempt to invoke virtual method 'com.example.test_app.currencyExchange[] com.example.test_app.JSONResponse.getCurrency()' on a null object reference
    at com.example.test_app.MainActivity$1.onResponse(MainActivity.java:54)
    at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:71)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

1 个答案:

答案 0 :(得分:0)

尝试将基本URL更改为https://api.exchangeratesapi.io/latest