Monday 15 June 2015

Java: boolean primitive private method returning false even though set to true -



Java: boolean primitive private method returning false even though set to true -

i have public method calls private method, conditionally calls private method. 3 methods homecoming boolean primitive value, value changing expect be.

i missing incredibly obvious, close code see problem.

here cleaned sample version of code , results seeing:

edit: have added actual code @ bottom

public class myclass { // default constructor public myclass() {} public boolean foo(final object arg0, final object arg1, final object arg2) { final boolean result = this.bar(arg0, arg1, arg2); system.out.println("foo: " + result); homecoming result; } private boolean bar(final object arg0, final object arg1, final object arg2) { boolean result = false; system.out.println("bar: " + result); seek { // complicated code generate exception if (this.wowsers(arg0, arg1, arg2)) { result = true; system.out.println("bar: " + result); system.out.println("bar: phone call wowsers() returned true"); } } grab (exception e) { system.out.println("something blew up"); e.printstacktrace(); } { // should not alter result this.irrelevantmethod_1(null); this.irrelevantmethod_2(null); this.irrelevantmethod_3(null); } system.out.println("bar: " + result); homecoming result; } private boolean wowsers(final object arg0, final object arg1, final object arg2) { boolean result = false; // complicated code involving passed in arguments // might alter result system.out.println("wowsers: " + result); homecoming result; } private void irrelevantmethod_1(object arg0) { // nil in here alter result } private void irrelevantmethod_2(object arg0) { // nil in here alter result } private void irrelevantmethod_3(object arg0) { // nil in here alter result } } // end class myclass

calling code:

myclass myinstance = new myclass(); myinstance.foo();

console output:

> bar: false > wowsers: true > bar: true > bar: phone call wowsers() returned true > foo: false

in sample above, value of result gets set true in method dowsers(), , correctly returned bar(). when bar() tests returned value wowsers() , finds true sets it's own result true , prints console value of result , prints dowsers() returned true.

the system.out.println @ end of bar() never executed (at to the lowest degree never see show in console), , value of result (which @ point true) returned false.

the foo() method prints value received phone call bar, false.

does see i'm messing up?

edit: here actual code -replaces methods foo() , bar()

public boolean sendremindernotifcation(final requestcontroller controller, final session session, final request request, final comment comment, final boolean treatasnewnote) { final boolean result = this.sendnotification(controller, session, request, comment, null, mailtype.remind, treatasnewnote); system.out.println("sendremindernotifcation(): " + result); system.out.println("***"); homecoming result; } private boolean sendnotification(final requestcontroller controller, final session session, final request request, final comment comment, final treeset<string> copyto, final mailtype mailtype, final boolean treatasnewnote) { hashmap<notifywhom, treeset<string>> sendto = null; boolean result = false; system.out.println("sendnotification(): " + result); seek { if (null == controller) { throw new illegalargumentexception("requestcontoller null"); } this.setrequesturlprefix(controller.getrequesturlprefix()); if (null == session) { throw new illegalargumentexception("session null"); } if (null == request) { throw new illegalargumentexception("request null"); } if (null == mailtype) { throw new illegalargumentexception("mailtype null"); } final enumset<notifywhom> recipients = this.getrecipients(controller, session, request, mailtype, treatasnewnote); if ((null == recipients) || recipients.isempty()) { homecoming false; } final hashmap<notifywhom, treeset<string>> tempsendto = this.getsendto(controller, request, recipients); if (null == tempsendto) { throw new runtimeexception("no recipients notification"); } // clear out duplicates sendto = this.purgeduplicaterecpients(tempsendto); // update prior assignee info // update requestor info // update queue owner info this.updatereopenedinformation(controller, session, request); final string subject = (request.isreopened()) ? helpdesknotifications.subject_reopened : helpdesknotifications.subject_updated; final iterator<entry<notifywhom, treeset<string>>> sit down = sendto.entryset().iterator(); final treeset<namehandle> exclude = this.getexcluderecipients(); while (sit.hasnext()) { final map.entry<notifywhom, treeset<string>> entry = sit.next(); final mailnotifykey key = new mailnotifykey(this.getlogger(), mailtype, entry.getkey()); if (mailtype.remind.equals(mailtype)) { final status status = request.getstatus(); final mailrecipientoption mro = (null == status) ? null : status.getmailrecipientoption(key); // null mro indicates notifications disabled if (null == mro) { homecoming false; } } final treeset<string> sendto = entry.getvalue(); if (this.sendemail(controller, session, request, subject, comment, sendto, copyto, exclude, key, treatasnewnote)) { result = true; system.out.println("sendnotification(): " + result); system.out.println("sendnotification(): (call sendemail() returned true)"); } } // send special re-opened notifications if (this.sendreopenednotifications(controller, session, request, subject, comment, treatasnewnote)) { result = true; system.out.println("sendnotification(): " + result); system.out.println("sendnotification(): (call sendreopenednotifications() returned true)"); } } grab (final exception e) { this.getlogger().logexception(this, e); e.printstacktrace(); } { this.setpriorassigneenotify(null); this.setreopenedrecipients(null); this.setexcluderecipients(null); } system.out.println("sendnotification(): " + result); homecoming result; }

the console output getting is:

> sendnotification(): false > sendnotification(): true > sendnotification(): (call sendemail() returned true) > sendremindernotifcation(): false > ***

(i realize should have posted actual code in first place)

for reason cannot figure out, seems though final 2 lines of code in method bar() , method sendnotification() not appear running. there other way method finish , homecoming unaware of?

i suggest set debug print statement before line:

if (null == mro) { homecoming false; }

since in while loop, , allows method homecoming false thoughresult has been set true. i'll bet false homecoming coming from, , why don't see final print statements beingness executed.

java boolean return primitive result

No comments:

Post a Comment