java - NullPointerException - attempt to invoke virtual method `notifyDataSetChanged()` - can't see list created -


i'm working way through this tutorial , stuck adding names list, , having list populate.

here's i'm trying get, list of names type in in input box: http://www.raywenderlich.com/78576/android-tutorial-for-beginners-part-2

(the underlined "darryl" input box, , shows name (after hitting button) on top, adds list).

i'm getting error though:

08-24 18:17:50.903  21935-21935/com.example.batman.myapplication e/androidruntime﹕ fatal exception: main     process: com.example.batman.myapplication, pid: 21935     java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.arrayadapter.notifydatasetchanged()' on null object reference             @ com.example.batman.myapplication.mainactivity.onclick(mainactivity.java:65)             @ android.view.view.performclick(view.java:5217)             @ android.view.view$performclick.run(view.java:20983)             @ android.os.handler.handlecallback(handler.java:739)             @ android.os.handler.dispatchmessage(handler.java:95)             @ android.os.looper.loop(looper.java:145)             @ android.app.activitythread.main(activitythread.java:6141)             @ java.lang.reflect.method.invoke(native method)             @ java.lang.reflect.method.invoke(method.java:372)             @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1399)             @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1194) 

here's mainactivity.java:

package com.example.batman.myapplication;  import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.menu; import android.view.view; import android.widget.arrayadapter; import android.widget.button; import android.widget.edittext; import android.widget.listview; import android.widget.textview;  import java.util.arraylist;  public class mainactivity extends appcompatactivity implements view.onclicklistener {     textview maintextview;     edittext mainedittext;     listview mainlistview;     arrayadapter marrayadapter; //  arraylist<string> mnamelist = new arraylist<string>();     arraylist mnamelist = new arraylist();      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         // 1. access textview defined in layout xml         // , set text         maintextview = (textview) findviewbyid(r.id.main_textview);         maintextview.settext("set in java!");          button mainbutton;         mainbutton = (button) findviewbyid(r.id.main_button);         mainbutton.setonclicklistener(this);          // 3.  access edittext defined in layout xml         mainedittext = (edittext) findviewbyid(r.id.main_edittext);          // 4. access listview         mainlistview = (listview) findviewbyid(r.id.main_listview);         // create arrayadapter listview         marrayadapter = new arrayadapter(this,  // removed arrayadapter before marrayadapter                 android.r.layout.simple_list_item_1,                 mnamelist);         // set listview use arrayadapter         mainlistview.setadapter(marrayadapter);     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.menu_main, menu);         return true;     }      @override     public void onclick(view v) {         // take typed edittext         // , use in textview         maintextview.settext(mainedittext.gettext().tostring() + ".");          // add value list shown in listview         mnamelist.add(mainedittext.gettext().tostring());         marrayadapter.notifydatasetchanged();     } } // end class 

here's activitymain.xml:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"               xmlns:tools="http://schemas.android.com/tools"               android:layout_width="match_parent"               android:layout_height="match_parent"               android:orientation="vertical"               tools:context=".mainactivity">      <textview         android:id="@+id/main_textview"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginleft="20dp"         android:layout_margintop="20dp"         android:text="textview"/>     <!-- displays keyboard when touched -->     <linearlayout         android:layout_width="match_parent"         android:layout_height="wrap_content">         <!-- list dataset defined in code adapter -->         <listview             android:id="@+id/main_listview"             android:layout_width="match_parent"             android:layout_height="0dp"             android:layout_weight="1"             android:layout_margintop="20dp"/>          <!-- set onclicklistener trigger results when pressed -->          <button             android:id="@+id/main_button"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_margintop="20dp"             android:layout_marginleft="20dp"             android:text="button" />         <!-- shows image drawable resources -->         <imageview             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_margintop="20dp"             android:layout_marginleft="20dp"             android:src="@drawable/ic_launcher" />         <!-- closing tag horizontal nested layout -->     </linearlayout>      <edittext         android:id="@+id/main_edittext"         android:inputtype="textcapsentences"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_margintop="20dp"         android:layout_marginleft="20dp"         android:hint="hint"/>      <!-- closing tag horizontal nested layout --> </linearlayout> 

i highlighting (android studio) on new arrayadapter(this, android.r.layout.simple_list_item_1, mnamelist); line, saying there's "unchecked call" it.

as can see 1 comment, need make mnamelist string arraylist<string> mnamelist = new arraylist<string>();?

(if need manifest or mainactivity.xml, let me know).

thanks ideas!

edit: comments, removed arrayadapter onclick part. final part of issue list display. displays input, doesn't show on list below. i've attached xml too.

you did not initialized global variable marrayadapter

change :

arrayadapter marrayadapter = new arrayadapter(this,             android.r.layout.simple_list_item_1,             mnamelist); 

to :

marrayadapter = new arrayadapter(this,             android.r.layout.simple_list_item_1,             mnamelist); 

in oncreate method of activity


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -