StackOverFlow Exception, alternatives for my function? - stack-overflow

I've created a program that at some point has to log in to some website. If something goes wrong, I want it to wait for some time, say 2 seconds, and then try again. so the function looks something like this:
public void log_in ()
{
try
{
//login code
}
catch
{
Thread.Sleep(2000);
log_in();
}
}
But this code started to give me StackOverFlowExeptions every now and then. Does anyone know how to solve that?

You're getting a stack overflow exception because every time you call log_in() you're adding the function to the stack. This this:
public void log_in ()
{
while (true) {
try
{
//login code
return;
}
catch
{
}
}
}

what's because if code can't login at all it keeps creating new function call each time on the stack. Next code would work:
public void log_in ()
{
while(true) {
try {
//login code
return;
}
catch {
Thread.Sleep(2000);
}
}
}
But overall you shouldn't try to login endlessly, after several time report error to user/admin.

Related

How do i exit from a method and run another when a condition is triggered, JDA discord wrappper

public void onMessageReceived(MessageReceivedEvent event) {
String[] messagelements = event.getMessage().getContentRaw().split("\\s+");
if (event.getMessage().getContentRaw().equalsIgnoreCase("what is the number beside " + wordanswer) && event.getAuthor().isBot()){
time = System.currentTimeMillis();
while (System.currentTimeMillis() - time <= 8000){
if (answered.equalsIgnoreCase("answered")) {
return;
}else {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
}
if (answered.equalsIgnoreCase("notanswered")){
event.getChannel().sendMessage("times up").queue();
}
}
}
public void onMessageReactionAdd (MessageReactionAddEvent event) {
Thread.currentThread().setPriority(10);
if (event.getUser().equals(worker)) {
if (answered.equalsIgnoreCase("notanswered")){
if (event.getReactionEmote().getName().equalsIgnoreCase(emoteanswer)){
event.getChannel().sendMessage("Correct!").queue();
}
else{
event.getChannel().sendMessage("Wrong").queue();
}
answered = "answered";
}
}
}
the variable answered is to check if the user has already answered the question. However, when my timer starts, it does not detect any activity from the the onMessageReactionAdd and only reacts to it after the timer ends in which "times up" will always be sent then the Wrong/Correct option will then be sent. How do i run both methods simultanously or stop a method and run another when a condition happens. (note that the reaction is the user answer). The timer gives a time limit on the answering the qn and the player will be wrong if they fail to answer within the time limit. Some people suggested using a new thread but i haven’t mastered java and need some help doing that
You could use either an eventwaiter, or create a response based on a state-machine (Credits to Minn for posthing this)
Depending on your use case, this will differ on how you're going to use it.

Synchronous Ajax Call in Angular

In my angular application, I have a scenario where i need to make looping over ajax calls. Scenario is as follows:
Based upon response from first request i need to initiate 2nd request, response from 2nd request would trigger 3rd request and so on. Wherever response does not meet certain criteria, loop needs to be broken.
Loop can go several times 10 or 20 based upon configured value.
Just want to make it synchronous. Anyone who can suggest approach to implement it ?
someList.forEach(async (value,index,arr)=> {
if(!isPrintingError)
{
let out = await this.someService.Print(someBuffer);
if(!out)
{
isPrintingError = true;
}
else {
console.log("Print successful");
}
}
}
Just have a look at Promises or async/await.
I'm not sure about how you want to do your ajax calls, and it would be great to have a small chunk of code.
But the idea is to do something like that
try {
const response1 = await this.apiCall1();
if (!response1) {
throw new Error('error1');
}
const response2 = await this.apiCall2();
if (!response2) {
throw new Error('error2');
}
// etc...
} catch (e) {
// logic in case of error
}
Also you can do it in a loop. But in order to give better help, i'll need some code
Try using RxJS Library, it will help you also in other different async stuff issues.
Using RxJS operators I'd take advantage of the Merge Operator.
More info here: RxJS Merge Operator
Thanks for your snippet. Here is how to break the loop in case of bad output
try {
someList.forEach(async (value, index, arr) => {
let output = await this.someService.Print(someBuffer);
if(!output) {
// break the loop
throw new Error('error');
} else {
console.log("Print successful");
}
}
} catch (e) {
// what to do if failed ?
}

Akavache always calling the fetchFunc

I have the following code using Akavache in a Xamarin app and it's not behaving the way I would think it should. Probably my misunderstanding of how it should be but it's driving me crazy.
So in my viewmodel I'm making the call to FetchNewsCategories and specifying a cache of 5 minutes for the item. What I'd expect to happen is that if the cache item is not there, it would make a call to the fetchFunc (ie. FetchNewsCategoriesAsync) but if I call the service any number of times inside the cache timeout of 5 minutes, it should just give me the cached item and not do the server call. In all cases that I've tried, it keeps doing the rest call and never gives me the cached item. I've also tried this with GetAndFetchLatest and if there is a cached item, it doesn't make the rest call but it also doesn't make the call in the subscribe event in the viewmodel.
Any ideas what I'm doing wrong here?
EDIT: I tested this same code on Android (Nexus 5 KitKat API19) and it's working flawlessly. I'm going to reset my IOS emulator and see if something was just out of whack.
NewsService.cs
public static async Task<ServiceResponse<List<ArticleCategoryInfo>>> FetchNewsCategoriesAsync(BlogSourceType blogSource)
{
return await ServiceClient.POST<List<ArticleCategoryInfo>>(Config.ApiUrl + "news/categories", new
{
ModuleId = Int32.Parse(Config.Values[blogSource == BlogSourceType.News ? ConfigKeys.KEY_NEWS_MODULE_ID : ConfigKeys.KEY_BLOG_MODULE_ID])
});
}
public static IObservable<ServiceResponse<List<ArticleCategoryInfo>>> FetchNewsCategories(BlogSourceType blogSource)
{
var cache = BlobCache.LocalMachine;
var cachedCategories = cache.GetOrFetchObject("categories" + blogSource,
async () => await FetchNewsCategoriesAsync(blogSource),
DateTime.Now.AddMinutes(5));
return cachedCategories;
}
NewsViewModel.cs
public async Task LoadCategories()
{
var cachedCategories = NewsService.FetchNewsCategories(blogSource);
cachedCategories.Subscribe((obj) => { Device.BeginInvokeOnMainThread(() => DisplayCategories(obj.Result,"Subscribe"));});
return;
}
private void DisplayCategories(IList<ArticleCategoryInfo> categories, string source)
{
Categories.Clear();
System.Diagnostics.Debug.WriteLine("Redisplaying categories from " + source);
foreach (var item in categories)
{
Categories.Add(item);
}
}
Just wanted to add my resolution to the issue I experienced above for reference to others with this problem.
The ServiceResponse object that I was trying to cache had an HttpResponseMessage in it which I suspect was causing a serialization error, probably a cyclical reference, so it never did get cached and ended up calling the endpoint every time. I ended up putting an [IgnoreDataMemberAttribute] on that property so it wasn't serialized and the problems went away.
I ended up handling the subscribe in the following manner to handle errors and to make sure the activity indicator bound to the IsBusy property was updated properly.
public async Task LoadActivities(bool refresh)
{
IsBusy = true;
if (refresh) OlderThanJournalId = int.MaxValue;
var cached = ActivityService.FetchJournalItems(GroupId, OlderThanJournalId, refresh);
cached.Subscribe((result) => { Device.BeginInvokeOnMainThread(() => {
DisplayActivities(result);
}); }, (err) => HandleError(err), () => IsBusy = false);
}
public void HandleError(Exception ex) {
IsBusy = false;
DialogService.ShowErrorToast(AppResources.ErrorMessage, "Unable to load activity stream.");
System.Diagnostics.Debug.WriteLine(ex.Message);
}
private void DisplayActivities(ServiceResponse<List<JournalItem>> response)
{
if (!response.IsConnected) {
DialogService.ShowInfoToast(AppResources.ErrorMessage, AppResources.NotConnected);
return;
}
if (!response.Authorized) {
App.LoginManager.Logout();
}
Activities.Clear();
foreach (var item in response.Result)
{
Activities.Add(item);
}
}
BeginInvokeOnMainThread is used to make sure that the updates to the ObservableCollection in DisplayActivities are seen in the UI.

JavaFx controlsFX AutoCompletionEvent doesn't work

I want to handle the AutoCompletionEvent of the controlsFX 8.0.5 framework, but somehow it will be never fired?! When no suggestions are available there should be one entry with "new..." and when this entry is chosen I want to do something. Therefore I set an EventHandler.
I implemented the binding like this:
AutoCompletionBinding<String> bind = TextFields.bindAutoCompletion(tf, sr -> {
List<String> shownSuggestions = new ArrayList<String>();
for (Client c : suggestions) {
if (!sr.getUserText().isEmpty()
&& c.toString().toLowerCase().startsWith(sr.getUserText().toLowerCase())) {
shownSuggestions.add(c.toString());
}
if (shownSuggestions.isEmpty()) {
if (sr.getUserText().isEmpty()) {
shownSuggestions.add(NEW_PARTY);
} else {
shownSuggestions.add(sr.getUserText() + NEW_PARTY_WITH_NAME);
}
}
}
return shownSuggestions;
});
And this is my EventHandler:
bind.setOnAutoCompleted(new EventHandler<AutoCompletionEvent<String>>() {
#Override
public void handle(AutoCompletionEvent<String> event) {
if (event.getCompletion().equals(NEW_PARTY)) {
System.out.println("new party chosen");
} else if (event.getCompletion().endsWith(NEW_PARTY_WITH_NAME)) {
System.out.println("new party with input chosen");
}
event.consume();
}
});
But there is no output on the console.
Can someone please help me? I'm trying this for days now...
regards
There is a bug in controlsFX 8.0.5 causing that the event never gets fired. So the code is correct, but it's never called.
Bug report in controlsFX 8.0.5

onPostExecute is getting executed before doInBackground

I am stuck with the following issue. I tried searching for answers but none of the responses could help me so i am asking this question here. Any response will be greatly appreciated!
I am using this below AsyncTask to use the login methods exposed by the Firebase API. However when i invoke new LoginOperation().execute() at the click of login button, i am not seeing the expected results. I am pasting the code below and the Logcat output.
I would like to know why onPostExecute is getting executed before doInBackground? Please note i am using a valid email id & password, so i should have been able to login properly.
Code:
private class LoginOperation extends AsyncTask<Void, Void, Boolean> {
protected Boolean doInBackground(Void... params) {
try{
authClient.loginWithEmail(emailid.getText().toString(),password.getText().toString(), new SimpleLoginAuthenticatedHandler() {
public void authenticated(
com.firebase.simplelogin.enums.Error error, User user) {
if(error != null) {
// There was an error logging into this account
loginStatus=false;
errorMsg=error.name();
Log.d(appName, "Inside if block in doInBackground of LoginOperation");
}
else {
// We are now logged in
loginStatus=true;
Log.d(appName, "Inside else block in doInBackground of LoginOperation");
}
}
});
}
catch (Exception e)
{
e.printStackTrace();
}
return Boolean.valueOf(loginStatus);
}
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if (result.booleanValue()) {
toastMsg="User logged in successfully";
Log.d(appName, "Inside onPostExecute success of LoginOperation");
}
else
{
toastMsg="Error in login";
Log.d(appName, "Inside onPostExecute failure of LoginOperation");
}
TextView displayStatus = (TextView) findViewById(R.id.displayStatus);
displayStatus.setText(toastMsg);
}
protected void onPreExecute() {
super.onPreExecute();
}
protected void onProgressUpdate(Void... values) {}
}
Code invoked on Login click:
public void onLogin(View arg0)
{
Log.d(appName, " email id is " + emailid.getText().toString());
Log.d(appName, " password is " + password.getText().toString());
try {
Boolean finalStatus= new LoginOperation().execute().get(5000, TimeUnit.MILLISECONDS);
Log.d(appName, " final Status is: " + finalStatus.booleanValue());
}
LogCat:
01-27 17:50:02.054: D/LOGIN(984): email id is abc#gmail.com
01-27 17:50:02.054: D/LOGIN(984): password is abc123
01-27 17:50:02.082: D/LOGIN(984): final Status is: false
01-27 17:50:02.082: D/LOGIN(984): Inside onPostExecute failure of LoginOperation
01-27 17:50:05.502: D/LOGIN(984): Inside else block in doInBackground of LoginOperation
Expected result:
Inside else block in doInBackground of LoginOperation
Inside onPostExecute success of LoginOperation
You don't need your AsyncTask because the class you are using in doInBackground seems to already perform its task in a background thread. Move the code in doInBackground somewhere to the UI thread and move your code from onPostExecute into the SimpleLoginAuthenticationHandler.
Currently, onPostExecuted is called immediately, because it just starts a new background thread and comes back immediately.
Most likely this is because SimpleLoginAuthenticatedHandler.authenticated() is executed asynchronously. It looks you don't need AsyncTask at all.
My guess is that loginWithEmail() runs within a Thread. That means that when you call this method, the thread is indeed run but that doesn't mean it has ended its execution, so maybe it's running paralelly, you get back, finish the doInBackground() method and it joins the onPostExecute() method.
I would remove the AsyncTask if it's that way, it's not necessary.

Resources