Can't initialize static member in Java - Android -
i've helper class
named prefs
in i've static class member
of interface
syncfinishedlistener
. here's are:
public interface syncfinishedlistener { void onsyncfinished(); }
here's prefs
helper class
public class prefs { public static final string tag = prefs.class.getsimplename(); private static final string prefsname = prefs.class.getsimplename(); sharedpreferences mprefs; public prefs(context context) { super(); mprefs = context.getsharedpreferences(prefsname, context.mode_private); } private static syncfinishedlistener listener; public prefs(context context, syncfinishedlistener listener) { this(context); prefs.listener = listener; syncutils.get(context).requestsync(hremployee.authority); } private boolean issyncfinished() { boolean issyncfinished = (ishremployeesyncfinished() && ishrattendancesyncfinished() && isattreportsyncfinished() && isschoolschoolsyncfinished() ); log.e(tag, "issyncfinished :" + issyncfinished); if (issyncfinished) { try { sethremployeesyncfinished(false); sethrattendancesyncfinished(false); setattreportsyncfinished(false); setschoolschoolsyncfinished(false); // here's nullpointerexception prefs.listener.onsyncfinished(); } catch (exception e) { e.printstacktrace(); } } return issyncfinished; } private static final string hremployeesyncfinished = "hremployeesyncfinished"; private static final string hrattendancesyncfinished = "hrattendancesyncfinished"; private static final string attreportsyncfinished = "attreportsyncfinished"; private static final string schoolschoolsyncfinished = "schoolschoolsyncfinished"; private boolean ishremployeesyncfinished() { return mprefs.getboolean(hremployeesyncfinished, false); } public prefs sethremployeesyncfinished(boolean hremployeesyncfinished) { mprefs.edit().putboolean(hremployeesyncfinished, hremployeesyncfinished).apply(); if (hremployeesyncfinished) { issyncfinished(); } return this; } private boolean ishrattendancesyncfinished() { return mprefs.getboolean(hrattendancesyncfinished, false); } public prefs sethrattendancesyncfinished(boolean hrattendancesyncfinished) { mprefs.edit().putboolean(hrattendancesyncfinished, hrattendancesyncfinished).apply(); if (hrattendancesyncfinished) { issyncfinished(); } return this; } private boolean isattreportsyncfinished() { return mprefs.getboolean(attreportsyncfinished, false); } public prefs setattreportsyncfinished(boolean attreportsyncfinished) { mprefs.edit().putboolean(attreportsyncfinished, attreportsyncfinished).apply(); if (attreportsyncfinished) { issyncfinished(); } return this; } private boolean isschoolschoolsyncfinished() { return mprefs.getboolean(schoolschoolsyncfinished, false); } public prefs setschoolschoolsyncfinished(boolean schoolschoolsyncfinished) { mprefs.edit().putboolean(schoolschoolsyncfinished, schoolschoolsyncfinished).apply(); if (schoolschoolsyncfinished) { issyncfinished(); } return this; } }
and here's how i'm calling fragment
public class employees extends basefragment { private static final string tag = employees.class.getsimplename(); private prefs mprefs; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); sethasoptionsmenu(true); mprefs = new prefs(getcontext()); } @override public void oncreateoptionsmenu(menu menu, menuinflater inflater) { inflater.inflate(r.menu.menu_employee, menu); super.oncreateoptionsmenu(menu, inflater); } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.action_sync) { return onsync(); } return super.onoptionsitemselected(item); } private boolean onsync() { try { final progressdialog dialog = new progressdialog(getcontext(), r.style.appdialogtheme); dialog.settitle("please wait"); dialog.setmessage("syncing data"); dialog.setcancelable(false); dialog.show(); new prefs(getcontext(), new syncfinishedlistener() { @override public void onsyncfinished() { // not getting called dialog.dismiss(); getactivity().finish(); } }); } catch (exception e) { e.printstacktrace(); } return true; } }
my code should run fine. but, can't figure out why i'm getting nullpointerexception
. here's log
debugging.
e/hremployee: onsyncstarted e/hrattendance: onsyncstarted e/schoolschool: onsyncstarted e/schoolschool: onsyncfinished e/prefs: issyncfinished :false e/hremployee: onsyncfinished e/prefs: issyncfinished :false e/attreport: onsyncstarted e/hremployee: onsyncstarted e/hremployee: onsyncfinished e/prefs: issyncfinished :false e/hrattendance: onsyncstarted e/hrattendance: onsyncfinished e/prefs: issyncfinished :false e/schoolschool: onsyncstarted e/schoolschool: onsyncfinished e/prefs: issyncfinished :false e/attreport: onsyncstarted e/hremployee: onsyncstarted e/hremployee: onsyncstarted e/hremployee: onsyncfinished e/prefs: issyncfinished :false e/hrattendance: onsyncstarted e/hremployee: onsyncfinished e/prefs: issyncfinished :false e/attreport: onsyncstarted e/attreport: onsyncfinished e/prefs: issyncfinished :false e/hrattendance: onsyncfinished e/prefs: issyncfinished :false e/hrattendance: onsyncstarted e/hrattendance: onsyncfinished e/prefs: issyncfinished :false e/schoolschool: onsyncstarted e/schoolschool: onsyncfinished e/prefs: issyncfinished :false e/hrattendance: onsyncfinished e/prefs: issyncfinished :false e/attreport: onsyncfinished e/prefs: issyncfinished :true w/system.err: java.lang.nullpointerexception: attempt invoke interface method 'void com.odoo.addons.employees.utils.syncfinishedlistener.onsyncfinished()' on null object reference w/system.err: @ com.odoo.addons.employees.utils.prefs.issyncfinished(prefs.java:145) w/system.err: @ com.odoo.addons.employees.utils.prefs.setattreportsyncfinished(prefs.java:115) w/system.err: @ com.odoo.addons.employees.models.attreport.onsyncfinished(attreport.java:58) w/system.err: @ com.odoo.core.service.osyncadapter.syncdata(osyncadapter.java:291) w/system.err: @ com.odoo.core.service.osyncadapter.onperformsync(osyncadapter.java:204) w/system.err: @ android.content.abstractthreadedsyncadapter$syncthread.run(abstractthreadedsyncadapter.java:272)
first there no reason why should calling super()
in first constructor, not extending class.
second there no reason why listener
should static, in code @ least. if calling prefs.listener
outside code, should reconsider whole architecture removing static calls. bad habit can avoided. if class has static variables used on application, should think using singleton design pattern. it's easy , safe use once used it.
Comments
Post a Comment