class - Android pass context causes NullPointerException -
i trying make methods work other classes current activity. in case there backup mechanism uses toasts , works flawlessly if method in same class activity every time it's called there nullpointerexception.
these essential lines main activity:
backup bckp; private context context = main.this; bckp.copybackup(backupdir, backupdirfile, database, context);
and called method:
public void copybackup(final string backupdir, final string target, final string source, final context context) { final file sdcard = environment.getexternalstoragedirectory(); if (sdcard.canwrite()) { inputstream input = null; try { input = new fileinputstream(source); } catch (filenotfoundexception e) { toast.maketext(context, "there error finding database", toast.length_short).show(); e.printstacktrace(); } file dir = new file (backupdir); if (!dir.exists()) { dir.mkdirs(); } outputstream output = null; try { output = new fileoutputstream(target); } catch (filenotfoundexception e) { toast.maketext(context, "there error creating backup", toast.length_short).show(); e.printstacktrace(); } byte[] buffer = new byte[1024]; int length; try { while ((length = input.read(buffer)) > 0) { output.write(buffer, 0, length); } } catch (ioexception e) { toast.maketext(context, "there error copying database", toast.length_short).show(); e.printstacktrace(); } try { output.flush(); output.close(); input.close(); } catch (ioexception e) { toast.maketext(context, "there error ending copy", toast.length_short).show(); e.printstacktrace(); } } else { toast.maketext(context, "no sd card found", toast.length_short).show(); } }
it makes no sense me why works if in 1 class not if it's in separate class... help!
edit:
05-08 14:13:24.935: e/androidruntime(636): fatal exception: main 05-08 14:13:24.935: e/androidruntime(636): java.lang.nullpointerexception 05-08 14:13:24.935: e/androidruntime(636): @ maturaarbeit.nicola_pfister.marks.main$9$1.onclick(main.java:271) 05-08 14:13:24.935: e/androidruntime(636): @ com.android.internal.app.alertcontroller$buttonhandler.handlemessage(alertcontroller.java:166) 05-08 14:13:24.935: e/androidruntime(636): @ android.os.handler.dispatchmessage(handler.java:99) 05-08 14:13:24.935: e/androidruntime(636): @ android.os.looper.loop(looper.java:137) 05-08 14:13:24.935: e/androidruntime(636): @ android.app.activitythread.main(activitythread.java:4745) 05-08 14:13:24.935: e/androidruntime(636): @ java.lang.reflect.method.invokenative(native method) 05-08 14:13:24.935: e/androidruntime(636): @ java.lang.reflect.method.invoke(method.java:511) 05-08 14:13:24.935: e/androidruntime(636): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:786) 05-08 14:13:24.935: e/androidruntime(636): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:553) 05-08 14:13:24.935: e/androidruntime(636): @ dalvik.system.nativestart.main(native method)
you haven't initialized bckp
null
when try call function on it. context
fine
backup bckp;
make it
backup bckp = new backup(); // send parameters if constructor takes them private context context = main.this; bckp.copybackup(backupdir, backupdirfile, database, context);
Comments
Post a Comment