java - Android - NullPointerException: storage == null -
hi i'm having problems android app i'm building off of tutorial online, keeps crashing when try run mentioned exception. appreciate on how stop error please. here's main activity:
package com.niall.easytxt; import android.app.activity; import android.content.intent; import android.database.datasetobserver; import android.os.bundle; import android.view.keyevent; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.view.onkeylistener; import android.widget.abslistview; import android.widget.button; import android.widget.edittext; import android.widget.listview; public class mainactivity extends activity { private chatarrayadapter adp; private listview list; private edittext chattext; private button send; intent in; private boolean side = false; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); intent = getintent(); send = (button)findviewbyid(r.id.btn); list = (listview)findviewbyid(r.id.listview); adp = new chatarrayadapter(getapplicationcontext(),r.layout.chat, null); chattext = (edittext)findviewbyid(r.id.chat); chattext.setonkeylistener(new onkeylistener(){ public boolean onkey(view v, int keycode, keyevent event){ if((event.getaction()==keyevent.action_down) && (keycode == keyevent.keycode_enter)) { return sendchatmessage(); } return false; } }); send.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { sendchatmessage(); } }); list.settranscriptmode(abslistview.transcript_mode_always_scroll); list.setadapter(adp); adp.registerdatasetobserver(new datasetobserver() { public void onchanged(){ super.onchanged(); list.setselection(adp.getcount() -1); } }); } private boolean sendchatmessage() { adp.add(new chatmessage(side,chattext.gettext().tostring())); chattext.settext(""); side =! side; return true; } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } }
and chatarrayadapter:
package com.niall.easytxt; import java.util.arraylist; import java.util.list; import android.content.context; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.view.gravity; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.linearlayout; import android.widget.textview; public class chatarrayadapter extends arrayadapter<chatmessage>{ private textview chattext; private list<chatmessage> messagelist = new arraylist<chatmessage>(); private linearlayout layout; public chatarrayadapter(context context, int resource, chatmessage[] objects) { super(context, resource, objects); } public void add(chatmessage object) { messagelist.add(object); super.add(object); } public int getcount() { return this.messagelist.size(); } public chatmessage getitem(int index) { return this.messagelist.get(index); } public view getview(int position, view convertview, viewgroup parent) { view v = convertview; if(v==null){ layoutinflater inflater = (layoutinflater) this.getcontext().getsystemservice(context.layout_inflater_service); v =inflater.inflate(r.layout.chat, parent,false); } layout = (linearlayout)v.findviewbyid(r.id.message1); chatmessage messageobj = getitem(position); chattext = (textview)v.findviewbyid(r.id.singlemessage); chattext.settext(messageobj.message); chattext.setbackgroundresource(messageobj.left ? r.drawable.bubble_a :r.drawable.bubble_b); layout.setgravity(messageobj.left?gravity.start:gravity.end); return v; } public bitmap decodetobitmap(byte[] decodedbyte){ return bitmapfactory.decodebytearray(decodedbyte, 0, decodedbyte.length); } }
here's logcat
04-29 05:40:03.988: d/androidruntime(1794): shutting down vm 04-29 05:40:03.988: e/androidruntime(1794): fatal exception: main 04-29 05:40:03.988: e/androidruntime(1794): process: com.niall.easytxt, pid: 1794 04-29 05:40:03.988: e/androidruntime(1794): java.lang.runtimeexception: unable start activity componentinfo{com.niall.easytxt/com.niall.easytxt.mainactivity}: java.lang.nullpointerexception: storage == null 04-29 05:40:03.988: e/androidruntime(1794): @ android.app.activitythread.performlaunchactivity(activitythread.java:2298) 04-29 05:40:03.988: e/androidruntime(1794): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2360) 04-29 05:40:03.988: e/androidruntime(1794): @ android.app.activitythread.access$800(activitythread.java:144) 04-29 05:40:03.988: e/androidruntime(1794): @ android.app.activitythread$h.handlemessage(activitythread.java:1278) 04-29 05:40:03.988: e/androidruntime(1794): @ android.os.handler.dispatchmessage(handler.java:102) 04-29 05:40:03.988: e/androidruntime(1794): @ android.os.looper.loop(looper.java:135) 04-29 05:40:03.988: e/androidruntime(1794): @ android.app.activitythread.main(activitythread.java:5221) 04-29 05:40:03.988: e/androidruntime(1794): @ java.lang.reflect.method.invoke(native method) 04-29 05:40:03.988: e/androidruntime(1794): @ java.lang.reflect.method.invoke(method.java:372) 04-29 05:40:03.988: e/androidruntime(1794): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) 04-29 05:40:03.988: e/androidruntime(1794): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694) 04-29 05:40:03.988: e/androidruntime(1794): caused by: java.lang.nullpointerexception: storage == null 04-29 05:40:03.988: e/androidruntime(1794): @ java.util.arrays$arraylist.<init>(arrays.java:38) 04-29 05:40:03.988: e/androidruntime(1794): @ java.util.arrays.aslist(arrays.java:155) 04-29 05:40:03.988: e/androidruntime(1794): @ android.widget.arrayadapter.<init>(arrayadapter.java:128) 04-29 05:40:03.988: e/androidruntime(1794): @ com.niall.easytxt.chatarrayadapter.<init>(chatarrayadapter.java:26) 04-29 05:40:03.988: e/androidruntime(1794): @ com.niall.easytxt.mainactivity.oncreate(mainactivity.java:33) 04-29 05:40:03.988: e/androidruntime(1794): @ android.app.activity.performcreate(activity.java:5933) 04-29 05:40:03.988: e/androidruntime(1794): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1105) 04-29 05:40:03.988: e/androidruntime(1794): @ android.app.activitythread.performlaunchactivity(activitythread.java:2251) 04-29 05:40:03.988: e/androidruntime(1794): ... 10 more
adp = new chatarrayadapter(getapplicationcontext(),r.layout.chat, null);
your problem lies on line. passing null dataset. null reference of chatmessage[]
. super constructor calls
arrays.aslist(objects)
which causing exception. fix provide empty dataset. eg change constructor like:
public chatarrayadapter(context context, int resource) { super(context, resource, messagelist); }
Comments
Post a Comment