java - Android: NullPointerException when the application attempts to encrypt .txt file -
i trying encrypt/decrypt file virtual device (genymotion). show code , exceptions have. guess nullpointerexception comes line declare view v = null
in onactivityresult
method not know how fix it.
exceptions
07-22 13:34:10.241 10419-10419/? e/androidruntime﹕ fatal exception: main process: application.nikola.com.encryptdecrypt, pid: 10419 java.lang.runtimeexception: failure delivering result resultinfo{who=null, request=1, result=-1, data=intent { dat=file:///storage/emulated/0/nikola.txt }} activity {application.nikola.com.encryptdecrypt/application.nikola.com.encryptdecrypt.mainactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'int android.view.view.getid()' on null object reference @ android.app.activitythread.deliverresults(activitythread.java:3539) @ android.app.activitythread.handlesendresult(activitythread.java:3582) @ android.app.activitythread.access$1300(activitythread.java:144) @ android.app.activitythread$h.handlemessage(activitythread.java:1327) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5221) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'int android.view.view.getid()' on null object reference @ application.nikola.com.encryptdecrypt.mainactivity.onactivityresult(mainactivity.java:70) @ android.app.activity.dispatchactivityresult(activity.java:6135) @ android.app.activitythread.deliverresults(activitythread.java:3535) at android.app.activitythread.handlesendresult(activitythread.java:3582) at android.app.activitythread.access$1300(activitythread.java:144) at android.app.activitythread$h.handlemessage(activitythread.java:1327) at android.os.handler.dispatchmessage(handler.java:102) at android.os.looper.loop(looper.java:135) at android.app.activitythread.main(activitythread.java:5221) at java.lang.reflect.method.invoke(native method) at java.lang.reflect.method.invoke(method.java:372) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) at com.android.internal.os.zygoteinit.main(zygoteinit.java:694) 07-22 13:34:10.263 765-1598/? w/activitymanager﹕ force finishing activity application.nikola.com.encryptdecrypt/.mainactivity 07-22 13:34:10.350 765-1598/? e/activitymanager﹕ invalid thumbnail dimensions: 576x576 07-22 13:34:10.400 765-2035/? i/openglrenderer﹕ initialized egl, version 1.4 07-22 13:34:10.466 765-2035/? w/egl_emulation﹕ eglsurfaceattrib not implemented 07-22 13:34:10.466 765-2035/? w/openglrenderer﹕ failed set egl_swap_behavior on surface 0x9ea263c0, error=egl_success 07-22 13:34:10.903 765-786/? w/activitymanager﹕ activity pause timeout activityrecord{2ef175ff u0 application.nikola.com.encryptdecrypt/.mainactivity t26 f} 07-22 13:34:11.032 1006-1265/? w/egl_emulation﹕ eglsurfaceattrib not implemented 07-22 13:34:11.033 1006-1265/? w/openglrenderer﹕ failed set egl_swap_behavior on surface 0xa095a020, error=egl_success 07-22 13:34:11.786 2640-2640/? i/art﹕ explicit concurrent mark sweep gc freed 3007(201kb) allocspace objects, 0(0b) los objects, 24% free, 16mb/21mb, paused 301us total 21.960ms 07-22 13:34:11.808 2640-2640/? i/art﹕ explicit concurrent mark sweep gc freed 3080(224kb) allocspace objects, 4(252kb) los objects, 24% free, 15mb/21mb, paused 173us total 7.668ms 07-22 13:34:11.822 2640-2640/? i/art﹕ explicit concurrent mark sweep gc freed 3(96b) allocspace objects, 0(0b) los objects, 24% free, 15mb/21mb, paused 237us total 13.937ms 07-22 13:34:12.735 10419-10419/? i/process﹕ sending signal. pid: 10419 sig: 9 07-22 13:34:12.738 765-823/? w/audiotrack﹕ audio_output_flag_fast denied client 07-22 13:34:12.764 765-805/? w/inputdispatcher﹕ channel '25d12a93 application.nikola.com.encryptdecrypt/application.nikola.com.encryptdecrypt.mainactivity (server)' ~ consumer closed input channel or error occurred. events=0x9 07-22 13:34:12.764 765-805/? e/inputdispatcher﹕ channel '25d12a93 application.nikola.com.encryptdecrypt/application.nikola.com.encryptdecrypt.mainactivity (server)' ~ channel unrecoverably broken , disposed! 07-22 13:34:12.777 765-782/? i/activitymanager﹕ process application.nikola.com.encryptdecrypt (pid 10419) has died 07-22 13:34:12.778 765-981/? i/windowstate﹕ win death: window{25d12a93 u0 application.nikola.com.encryptdecrypt/application.nikola.com.encryptdecrypt.mainactivity} 07-22 13:34:12.778 765-981/? w/inputdispatcher﹕ attempted unregister unregistered input channel '25d12a93 application.nikola.com.encryptdecrypt/application.nikola.com.encryptdecrypt.mainactivity (server)'
mainactivity
public class mainactivity extends activity implements view.onclicklistener { button btnencrypt; button btndecrypt; final int activity_choose_file = 1; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); btnencrypt = (button) this.findviewbyid(r.id.btnactivity1); btnencrypt.setonclicklistener(this); btndecrypt = (button) this.findviewbyid(r.id.btnactivity2); btndecrypt.setonclicklistener(this); } @override public void onclick(view v) { intent choosefile; intent intent; choosefile = new intent(intent.action_get_content); choosefile.settype("file*//*"); intent = intent.createchooser(choosefile, "choose file"); startactivityforresult(intent, activity_choose_file); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); encryptdecrypt crypto = new encryptdecrypt(); view v = null; string filepath = null; switch(requestcode) { case activity_choose_file: { if (resultcode == result_ok){ uri uri = data.getdata(); filepath = uri.getpath(); switch(v.getid()) { case r.id.btnactivity1: crypto.encryptor(filepath); break; case r.id.btnactivity2: crypto.decryptor(filepath); break; } } } } } }
this is encrypt/decrypt algorithm use.
public class encryptdecrypt { public void encryptor(string inputfilepath) { fileoutputstream fos = null; file file = new file(inputfilepath); string keystring = "140405px_0.$88"; string algorithm = "desede"; try { fileinputstream fileinputstream = new fileinputstream(file); byte[] filebytearray = new byte[fileinputstream.available()]; fileinputstream.read(filebytearray); (byte b : filebytearray) { system.out.println(b); } secretkey secretkey = getkey(keystring); cipher cipher = cipher.getinstance(algorithm); cipher.init(cipher.encrypt_mode, secretkey); objectoutputstream objectoutputstream = new objectoutputstream(new cipheroutputstream(new fileoutputstream("encrypt.file"), cipher)); objectoutputstream.writeobject(filebytearray); objectoutputstream.close(); } catch (exception e) { e.printstacktrace(); } } public void decryptor(string inputfilepath) { string outputfilepath = "decrypt.txt"; string keystring = "140405px_0.$88"; string algorithm = "desede"; try { file inputfilename = new file(inputfilepath); fileinputstream fileinputstream = new fileinputstream(inputfilename); fileoutputstream fileoutputstream = new fileoutputstream(outputfilepath); secretkey secretkey = getkey(keystring); cipher cipher = cipher.getinstance(algorithm); cipher.init(cipher.decrypt_mode, secretkey); objectinputstream objectinputstream = new objectinputstream(new cipherinputstream(fileinputstream, cipher)); system.out.println(objectinputstream.available()); fileoutputstream.write((byte[]) objectinputstream.readobject()); fileoutputstream.flush(); fileoutputstream.close(); fileinputstream.close(); objectinputstream.close(); } catch (exception e) { e.printstacktrace(); } } public static secretkey getkey(string message) throws exception { string messagetouppercase = message.touppercase(); byte[] digestofpassword = messagetouppercase.getbytes(); byte[] keybytes = arrays.copyof(digestofpassword, 24); secretkey key = new secretkeyspec(keybytes, "desede"); return key; } }
view v = null; // v null string filepath = null; switch(requestcode) { case activity_choose_file: { if (resultcode == result_ok){ uri uri = data.getdata(); filepath = uri.getpath(); switch(v.getid()) { // v still null case r.id.btnactivity1: crypto.encryptor(filepath); break; case r.id.btnactivity2: crypto.decryptor(filepath); break; } } } }
your v
is null , trying v.getid()
. why getting nullpointerexception
.
in order fix this, might want add onclicklistener btnactivity1
, btnactivity2
.
btnactivity1.setonclicklisterner(new onclicklistener() { @override public void onclick(view arg0) { crypto.encryptor(filepath); } }); } btnactivity2.setonclicklisterner(new onclicklistener() { @override public void onclick(view arg0) { crypto.decryptor(filepath); } }); }
Comments
Post a Comment