accessing values inside an inner method through setters and getters in java
This seems like an easy problem, yet I am limited by my knowledge in java.
Consider the following code:
public void method1()
Object1 obj = new Object1()
@Override
public void innerMethod(Object response)
setList(response.list);
// Displaying the result of the getter is for sample purposes
System.out.println(getList().get(0).getName()); // This works and prints out the name of the first item.
;
obj.execute(); // Suppose execute method is pre-defined and just means it'll execute the `innerMethod`.
System.out.println(getList().get(0).getName()); // This returns null for the getList().
From what I understand about the execution at runtime, the list
should be populated once method1
is run since innerMethod
is the first one written.
Please help me understand why the second accessing of getList()
returns null and how else can I access the data from innerMethod
.
Note as well that I'm using getList()
in another class, and there as well it returns null.
EDIT:
Below is the full code for above scenario.
public void callMusicAPI()
Callback<SongList> callback = new Callback<SongListResponse>()
@Override
public void onResponse(Call<SongListResponse> call, Response<SongListResponse> response)
setSongListResult(response.body().getResults());
Log.d(TAG, "Number of Songs received: " + Songs.size()); // This works.
Log.d(TAG, "Actual Response from API Call: " + response.raw() + "" + success + " " + getSongListResult().get(1).getTitle()+getSongListResult().get(1).getRelease_date() ); //This works.
@Override
public void onFailure(Call<SongListResponse> call, Throwable throwable)
Log.e(TAG, throwable.toString());
;
call.enqueue(callback);
if(call.isExecuted())
if (getSongListResult() != null)
Log.d(TAG, "Data received in setter and getter: " + getSongListResult().get(2).getTitle() + getSongListResult().get(2).getRelease_date());
else
Log.e(TAG, "List is super null");
public void setSongListResult(List<Song> songs)
this.songs = songs;
public List<Song> getSongListResult()
return this.songs;
I expect after call.enqueue(callback)
the list would be populated however, this is not the case.
Feel free to note if this is a java problem and the setter-getter would work fine on most scenarios, or if this is specific to the response from the api call. If, so do recommend if the question needs to be changed.
java
|
show 1 more comment
This seems like an easy problem, yet I am limited by my knowledge in java.
Consider the following code:
public void method1()
Object1 obj = new Object1()
@Override
public void innerMethod(Object response)
setList(response.list);
// Displaying the result of the getter is for sample purposes
System.out.println(getList().get(0).getName()); // This works and prints out the name of the first item.
;
obj.execute(); // Suppose execute method is pre-defined and just means it'll execute the `innerMethod`.
System.out.println(getList().get(0).getName()); // This returns null for the getList().
From what I understand about the execution at runtime, the list
should be populated once method1
is run since innerMethod
is the first one written.
Please help me understand why the second accessing of getList()
returns null and how else can I access the data from innerMethod
.
Note as well that I'm using getList()
in another class, and there as well it returns null.
EDIT:
Below is the full code for above scenario.
public void callMusicAPI()
Callback<SongList> callback = new Callback<SongListResponse>()
@Override
public void onResponse(Call<SongListResponse> call, Response<SongListResponse> response)
setSongListResult(response.body().getResults());
Log.d(TAG, "Number of Songs received: " + Songs.size()); // This works.
Log.d(TAG, "Actual Response from API Call: " + response.raw() + "" + success + " " + getSongListResult().get(1).getTitle()+getSongListResult().get(1).getRelease_date() ); //This works.
@Override
public void onFailure(Call<SongListResponse> call, Throwable throwable)
Log.e(TAG, throwable.toString());
;
call.enqueue(callback);
if(call.isExecuted())
if (getSongListResult() != null)
Log.d(TAG, "Data received in setter and getter: " + getSongListResult().get(2).getTitle() + getSongListResult().get(2).getRelease_date());
else
Log.e(TAG, "List is super null");
public void setSongListResult(List<Song> songs)
this.songs = songs;
public List<Song> getSongListResult()
return this.songs;
I expect after call.enqueue(callback)
the list would be populated however, this is not the case.
Feel free to note if this is a java problem and the setter-getter would work fine on most scenarios, or if this is specific to the response from the api call. If, so do recommend if the question needs to be changed.
java
2
Could you provide a Minimal, Complete, and Verifiable example? It would make it much easier to work out exactly what's going on.
– Jon Skeet
Nov 11 at 17:31
It would be easier if you'd post more code. E.g. the getList() method.
– Ridcully
Nov 11 at 17:32
this seems more of understanding Java's execution at runtime, doesn't the above suffice it?the getList is just a return statement of the List<Objects>
– BrainAmplifier1820
Nov 11 at 17:44
@JonSkeet I've updated the question. Request to provide some insight for this. Much appreciated.
– BrainAmplifier1820
Nov 11 at 18:08
It remains unclear what the type ofcall
may be, but the method nameenqueue()
certainly suggests that your expectation is incorrect. Normally, if you "enqueue" something, you are setting it up for action to be performed on it later, often an indeterminate amount of time in the future.
– John Bollinger
Nov 11 at 18:15
|
show 1 more comment
This seems like an easy problem, yet I am limited by my knowledge in java.
Consider the following code:
public void method1()
Object1 obj = new Object1()
@Override
public void innerMethod(Object response)
setList(response.list);
// Displaying the result of the getter is for sample purposes
System.out.println(getList().get(0).getName()); // This works and prints out the name of the first item.
;
obj.execute(); // Suppose execute method is pre-defined and just means it'll execute the `innerMethod`.
System.out.println(getList().get(0).getName()); // This returns null for the getList().
From what I understand about the execution at runtime, the list
should be populated once method1
is run since innerMethod
is the first one written.
Please help me understand why the second accessing of getList()
returns null and how else can I access the data from innerMethod
.
Note as well that I'm using getList()
in another class, and there as well it returns null.
EDIT:
Below is the full code for above scenario.
public void callMusicAPI()
Callback<SongList> callback = new Callback<SongListResponse>()
@Override
public void onResponse(Call<SongListResponse> call, Response<SongListResponse> response)
setSongListResult(response.body().getResults());
Log.d(TAG, "Number of Songs received: " + Songs.size()); // This works.
Log.d(TAG, "Actual Response from API Call: " + response.raw() + "" + success + " " + getSongListResult().get(1).getTitle()+getSongListResult().get(1).getRelease_date() ); //This works.
@Override
public void onFailure(Call<SongListResponse> call, Throwable throwable)
Log.e(TAG, throwable.toString());
;
call.enqueue(callback);
if(call.isExecuted())
if (getSongListResult() != null)
Log.d(TAG, "Data received in setter and getter: " + getSongListResult().get(2).getTitle() + getSongListResult().get(2).getRelease_date());
else
Log.e(TAG, "List is super null");
public void setSongListResult(List<Song> songs)
this.songs = songs;
public List<Song> getSongListResult()
return this.songs;
I expect after call.enqueue(callback)
the list would be populated however, this is not the case.
Feel free to note if this is a java problem and the setter-getter would work fine on most scenarios, or if this is specific to the response from the api call. If, so do recommend if the question needs to be changed.
java
This seems like an easy problem, yet I am limited by my knowledge in java.
Consider the following code:
public void method1()
Object1 obj = new Object1()
@Override
public void innerMethod(Object response)
setList(response.list);
// Displaying the result of the getter is for sample purposes
System.out.println(getList().get(0).getName()); // This works and prints out the name of the first item.
;
obj.execute(); // Suppose execute method is pre-defined and just means it'll execute the `innerMethod`.
System.out.println(getList().get(0).getName()); // This returns null for the getList().
From what I understand about the execution at runtime, the list
should be populated once method1
is run since innerMethod
is the first one written.
Please help me understand why the second accessing of getList()
returns null and how else can I access the data from innerMethod
.
Note as well that I'm using getList()
in another class, and there as well it returns null.
EDIT:
Below is the full code for above scenario.
public void callMusicAPI()
Callback<SongList> callback = new Callback<SongListResponse>()
@Override
public void onResponse(Call<SongListResponse> call, Response<SongListResponse> response)
setSongListResult(response.body().getResults());
Log.d(TAG, "Number of Songs received: " + Songs.size()); // This works.
Log.d(TAG, "Actual Response from API Call: " + response.raw() + "" + success + " " + getSongListResult().get(1).getTitle()+getSongListResult().get(1).getRelease_date() ); //This works.
@Override
public void onFailure(Call<SongListResponse> call, Throwable throwable)
Log.e(TAG, throwable.toString());
;
call.enqueue(callback);
if(call.isExecuted())
if (getSongListResult() != null)
Log.d(TAG, "Data received in setter and getter: " + getSongListResult().get(2).getTitle() + getSongListResult().get(2).getRelease_date());
else
Log.e(TAG, "List is super null");
public void setSongListResult(List<Song> songs)
this.songs = songs;
public List<Song> getSongListResult()
return this.songs;
I expect after call.enqueue(callback)
the list would be populated however, this is not the case.
Feel free to note if this is a java problem and the setter-getter would work fine on most scenarios, or if this is specific to the response from the api call. If, so do recommend if the question needs to be changed.
java
java
edited Nov 11 at 18:07
asked Nov 11 at 17:27
BrainAmplifier1820
12
12
2
Could you provide a Minimal, Complete, and Verifiable example? It would make it much easier to work out exactly what's going on.
– Jon Skeet
Nov 11 at 17:31
It would be easier if you'd post more code. E.g. the getList() method.
– Ridcully
Nov 11 at 17:32
this seems more of understanding Java's execution at runtime, doesn't the above suffice it?the getList is just a return statement of the List<Objects>
– BrainAmplifier1820
Nov 11 at 17:44
@JonSkeet I've updated the question. Request to provide some insight for this. Much appreciated.
– BrainAmplifier1820
Nov 11 at 18:08
It remains unclear what the type ofcall
may be, but the method nameenqueue()
certainly suggests that your expectation is incorrect. Normally, if you "enqueue" something, you are setting it up for action to be performed on it later, often an indeterminate amount of time in the future.
– John Bollinger
Nov 11 at 18:15
|
show 1 more comment
2
Could you provide a Minimal, Complete, and Verifiable example? It would make it much easier to work out exactly what's going on.
– Jon Skeet
Nov 11 at 17:31
It would be easier if you'd post more code. E.g. the getList() method.
– Ridcully
Nov 11 at 17:32
this seems more of understanding Java's execution at runtime, doesn't the above suffice it?the getList is just a return statement of the List<Objects>
– BrainAmplifier1820
Nov 11 at 17:44
@JonSkeet I've updated the question. Request to provide some insight for this. Much appreciated.
– BrainAmplifier1820
Nov 11 at 18:08
It remains unclear what the type ofcall
may be, but the method nameenqueue()
certainly suggests that your expectation is incorrect. Normally, if you "enqueue" something, you are setting it up for action to be performed on it later, often an indeterminate amount of time in the future.
– John Bollinger
Nov 11 at 18:15
2
2
Could you provide a Minimal, Complete, and Verifiable example? It would make it much easier to work out exactly what's going on.
– Jon Skeet
Nov 11 at 17:31
Could you provide a Minimal, Complete, and Verifiable example? It would make it much easier to work out exactly what's going on.
– Jon Skeet
Nov 11 at 17:31
It would be easier if you'd post more code. E.g. the getList() method.
– Ridcully
Nov 11 at 17:32
It would be easier if you'd post more code. E.g. the getList() method.
– Ridcully
Nov 11 at 17:32
this seems more of understanding Java's execution at runtime, doesn't the above suffice it?the getList is just a return statement of the List<Objects>
– BrainAmplifier1820
Nov 11 at 17:44
this seems more of understanding Java's execution at runtime, doesn't the above suffice it?the getList is just a return statement of the List<Objects>
– BrainAmplifier1820
Nov 11 at 17:44
@JonSkeet I've updated the question. Request to provide some insight for this. Much appreciated.
– BrainAmplifier1820
Nov 11 at 18:08
@JonSkeet I've updated the question. Request to provide some insight for this. Much appreciated.
– BrainAmplifier1820
Nov 11 at 18:08
It remains unclear what the type of
call
may be, but the method name enqueue()
certainly suggests that your expectation is incorrect. Normally, if you "enqueue" something, you are setting it up for action to be performed on it later, often an indeterminate amount of time in the future.– John Bollinger
Nov 11 at 18:15
It remains unclear what the type of
call
may be, but the method name enqueue()
certainly suggests that your expectation is incorrect. Normally, if you "enqueue" something, you are setting it up for action to be performed on it later, often an indeterminate amount of time in the future.– John Bollinger
Nov 11 at 18:15
|
show 1 more comment
2 Answers
2
active
oldest
votes
Actually, I would much rather write a comment, but since I don't have enough reputation to do that, I'll write my input as an answer. I hope that I can be of help.
It looks to me as if there is a problem with scope. If you remove all code that is "out of reach" for the second getList()
it looks like this
public void method1()
Object1 obj = new Object1()
obj.execute(); // Suppose execute method is pre-defined and just means it'll execute the `innerMethod`.
System.out.println(getList().get(0).getName()); // This returns null for the getList().
There seems to be nothing for getList()
to get, so maybe that's the reason for your problem.
Hi, I posted the full code for the scenario. It's important to capture the data inside the innerMethod as that's where it can only be accessed, which is why I thought of using setters and getters.
– BrainAmplifier1820
Nov 11 at 18:13
add a comment |
public void method1()
Object1 obj = new Object1()
@Override
public void innerMethod(Object response)
setList(response.list);
// Displaying the result of the getter is for sample purposes
System.out.println(getList().get(0).getName()); // This works and prints out the name of the first item.
;
obj.execute(); // Suppose execute method is pre-defined and just means it'll execute the `innerMethod`.
System.out.println(obj.getList().get(0).getName()); // <-- ***********
I think you're calling getList() from the wrong object. If you don't specify the object to call the method from it's assuming this.list(), in which case you're getting uninitialized data members.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251317%2faccessing-values-inside-an-inner-method-through-setters-and-getters-in-java%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Actually, I would much rather write a comment, but since I don't have enough reputation to do that, I'll write my input as an answer. I hope that I can be of help.
It looks to me as if there is a problem with scope. If you remove all code that is "out of reach" for the second getList()
it looks like this
public void method1()
Object1 obj = new Object1()
obj.execute(); // Suppose execute method is pre-defined and just means it'll execute the `innerMethod`.
System.out.println(getList().get(0).getName()); // This returns null for the getList().
There seems to be nothing for getList()
to get, so maybe that's the reason for your problem.
Hi, I posted the full code for the scenario. It's important to capture the data inside the innerMethod as that's where it can only be accessed, which is why I thought of using setters and getters.
– BrainAmplifier1820
Nov 11 at 18:13
add a comment |
Actually, I would much rather write a comment, but since I don't have enough reputation to do that, I'll write my input as an answer. I hope that I can be of help.
It looks to me as if there is a problem with scope. If you remove all code that is "out of reach" for the second getList()
it looks like this
public void method1()
Object1 obj = new Object1()
obj.execute(); // Suppose execute method is pre-defined and just means it'll execute the `innerMethod`.
System.out.println(getList().get(0).getName()); // This returns null for the getList().
There seems to be nothing for getList()
to get, so maybe that's the reason for your problem.
Hi, I posted the full code for the scenario. It's important to capture the data inside the innerMethod as that's where it can only be accessed, which is why I thought of using setters and getters.
– BrainAmplifier1820
Nov 11 at 18:13
add a comment |
Actually, I would much rather write a comment, but since I don't have enough reputation to do that, I'll write my input as an answer. I hope that I can be of help.
It looks to me as if there is a problem with scope. If you remove all code that is "out of reach" for the second getList()
it looks like this
public void method1()
Object1 obj = new Object1()
obj.execute(); // Suppose execute method is pre-defined and just means it'll execute the `innerMethod`.
System.out.println(getList().get(0).getName()); // This returns null for the getList().
There seems to be nothing for getList()
to get, so maybe that's the reason for your problem.
Actually, I would much rather write a comment, but since I don't have enough reputation to do that, I'll write my input as an answer. I hope that I can be of help.
It looks to me as if there is a problem with scope. If you remove all code that is "out of reach" for the second getList()
it looks like this
public void method1()
Object1 obj = new Object1()
obj.execute(); // Suppose execute method is pre-defined and just means it'll execute the `innerMethod`.
System.out.println(getList().get(0).getName()); // This returns null for the getList().
There seems to be nothing for getList()
to get, so maybe that's the reason for your problem.
answered Nov 11 at 18:03
OingoBoingo
158
158
Hi, I posted the full code for the scenario. It's important to capture the data inside the innerMethod as that's where it can only be accessed, which is why I thought of using setters and getters.
– BrainAmplifier1820
Nov 11 at 18:13
add a comment |
Hi, I posted the full code for the scenario. It's important to capture the data inside the innerMethod as that's where it can only be accessed, which is why I thought of using setters and getters.
– BrainAmplifier1820
Nov 11 at 18:13
Hi, I posted the full code for the scenario. It's important to capture the data inside the innerMethod as that's where it can only be accessed, which is why I thought of using setters and getters.
– BrainAmplifier1820
Nov 11 at 18:13
Hi, I posted the full code for the scenario. It's important to capture the data inside the innerMethod as that's where it can only be accessed, which is why I thought of using setters and getters.
– BrainAmplifier1820
Nov 11 at 18:13
add a comment |
public void method1()
Object1 obj = new Object1()
@Override
public void innerMethod(Object response)
setList(response.list);
// Displaying the result of the getter is for sample purposes
System.out.println(getList().get(0).getName()); // This works and prints out the name of the first item.
;
obj.execute(); // Suppose execute method is pre-defined and just means it'll execute the `innerMethod`.
System.out.println(obj.getList().get(0).getName()); // <-- ***********
I think you're calling getList() from the wrong object. If you don't specify the object to call the method from it's assuming this.list(), in which case you're getting uninitialized data members.
add a comment |
public void method1()
Object1 obj = new Object1()
@Override
public void innerMethod(Object response)
setList(response.list);
// Displaying the result of the getter is for sample purposes
System.out.println(getList().get(0).getName()); // This works and prints out the name of the first item.
;
obj.execute(); // Suppose execute method is pre-defined and just means it'll execute the `innerMethod`.
System.out.println(obj.getList().get(0).getName()); // <-- ***********
I think you're calling getList() from the wrong object. If you don't specify the object to call the method from it's assuming this.list(), in which case you're getting uninitialized data members.
add a comment |
public void method1()
Object1 obj = new Object1()
@Override
public void innerMethod(Object response)
setList(response.list);
// Displaying the result of the getter is for sample purposes
System.out.println(getList().get(0).getName()); // This works and prints out the name of the first item.
;
obj.execute(); // Suppose execute method is pre-defined and just means it'll execute the `innerMethod`.
System.out.println(obj.getList().get(0).getName()); // <-- ***********
I think you're calling getList() from the wrong object. If you don't specify the object to call the method from it's assuming this.list(), in which case you're getting uninitialized data members.
public void method1()
Object1 obj = new Object1()
@Override
public void innerMethod(Object response)
setList(response.list);
// Displaying the result of the getter is for sample purposes
System.out.println(getList().get(0).getName()); // This works and prints out the name of the first item.
;
obj.execute(); // Suppose execute method is pre-defined and just means it'll execute the `innerMethod`.
System.out.println(obj.getList().get(0).getName()); // <-- ***********
I think you're calling getList() from the wrong object. If you don't specify the object to call the method from it's assuming this.list(), in which case you're getting uninitialized data members.
answered Nov 11 at 18:18
Travis
11
11
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53251317%2faccessing-values-inside-an-inner-method-through-setters-and-getters-in-java%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Could you provide a Minimal, Complete, and Verifiable example? It would make it much easier to work out exactly what's going on.
– Jon Skeet
Nov 11 at 17:31
It would be easier if you'd post more code. E.g. the getList() method.
– Ridcully
Nov 11 at 17:32
this seems more of understanding Java's execution at runtime, doesn't the above suffice it?the getList is just a return statement of the List<Objects>
– BrainAmplifier1820
Nov 11 at 17:44
@JonSkeet I've updated the question. Request to provide some insight for this. Much appreciated.
– BrainAmplifier1820
Nov 11 at 18:08
It remains unclear what the type of
call
may be, but the method nameenqueue()
certainly suggests that your expectation is incorrect. Normally, if you "enqueue" something, you are setting it up for action to be performed on it later, often an indeterminate amount of time in the future.– John Bollinger
Nov 11 at 18:15