I have created my own test JSON and try to incorporate it into my Android
app using Retrofit
but for some reason I can´t seem to understand I get a Null Point Exception
when trying to fetch the JSON
.
I have created JSON that looks something like this:
[{"date":"2019/01/01","availability":"available"},{"date":"2019/01/02","availability":"closed"},{"date":"2019/01/03","availability":"closed"}]
And so on...
Then I have set up both the Retrofit
code and the Room
database code.
Here is the Retrofit
code from the main class:
public class MainActivity extends AppCompatActivity {
MainRepository mainRepository;
Call<List<RFVariables>> getRFPosts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
getRFPosts = mainRepository.getGetPosts();
getRFPosts.enqueue( new Callback<List<RFVariables>>() {
@Override
public void onResponse(Call<List<RFVariables>> call, Response<List<RFVariables>> response) {
List<RFVariables> myResponse = response.body();
if(myResponse != null) {
for(RFVariables item: myResponse) {
Log.d("TAG", item.getDate());
}
}
}
@Override
public void onFailure(Call<List<RFVariables>> call, Throwable t) {
}
} );
It crashes on the getRFPosts = mainRepository.getGetPosts();
line with the error Attempt to invoke interface method 'void retrofit2.Call.enqueue(retrofit2.Callback)' on a null object reference
at com.slothmode.sunnyatsea.MainActivity.onCreate(MainActivity.java:25)
The repository code looks like this:
private Retrofit retrofitObject;
private RFCalls retrofitRepository;
private Call<List<RFVariables>> getPosts;
public MainRepository(Application application) {
this.retrofitObject = new Retrofit.Builder()
.baseUrl( "https://api.myjson.com/bins/" )
.addConverterFactory( GsonConverterFactory.create() )
.build();
this.retrofitRepository = this.retrofitObject.create( RFCalls.class );
this.getPosts = this.retrofitRepository.findPosts();
}
public Call<List<RFVariables>> getGetPosts() {
return getPosts;
}
I have done this successfully before, so not sure what's mising this time around. Seems like an exact replica of my other successful projects. What am I missing? Let me know if there is more code I should show. Thanks in advance. Really stuck here.