java - Why am i getting null object after i filled it? -


i´m getting inputstream website(viz. http://developer.android.com/training/basics/network-ops/connecting.html) have xml parser , in 1 class i´m making own object xml parsing stream. if check there fine. after try object class getter i´m getting null pointer exception , don´t see why.

first class

public class connection {  private final string urlstring = "http://blabla.html"; private xml xml; private context context; private string result; private boolean connection;  public xml getxml() {     return xml; }  public void setxml(xml xml) {     this.xml = xml; }  public string getresult() {     return result; }  public void setresult(string result) {     this.result = result; }  public boolean getconnection() {     return connection; }  public void setconnection(boolean connection) {     this.connection = connection; }  public connection(context context) {     this.context = context; } public void connect() {     connectivitymanager connmgr = (connectivitymanager)             context.getsystemservice(context.connectivity_service);     networkinfo networkinfo = connmgr.getactivenetworkinfo();     if (networkinfo != null && networkinfo.isconnected()) {         new downloadwebpagetask().execute(urlstring);         connection = true;      } else {         connection = false;     } }   private class downloadwebpagetask extends asynctask<string, void,    string> { @override protected string doinbackground(string... urls) {      // params comes execute() call: params[0] url.     try {         return downloadurl(urls[0]);     } catch (ioexception e) {         return "unable retrieve web page. url may invalid.";     } } // onpostexecute displays results of asynctask. @override protected void onpostexecute(string result) {     result = ""; } }     private string downloadurl(string myurl) throws ioexception {     inputstream = null;      try {         url url = new url(myurl);         httpurlconnection conn = (httpurlconnection) url.openconnection();         conn.setrequestmethod("get");         conn.setdoinput(true);         // starts query         conn.connect();         int response = conn.getresponsecode();         //log.d(debug_tag, "the response is: " + response);         = conn.getinputstream();           string contentasstring ="aa";         xmlparser xmlparser = new xmlparser();         xml = xmlparser.getparsedxml(is);         return contentasstring;     } {         if (is != null) {             is.close();         }     } } 

i tried rework downloadwebpagetask xml without success. second main activity class

public class mainactivity extends actionbaractivity {  private button button; private textview textview; private final string urlstring = "http://blabla.html"; private xml xml;   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     button = (button) findviewbyid(r.id.button);     textview = (textview) findviewbyid(r.id.textview2);     final context context = this;      button.setonclicklistener(new view.onclicklistener() {         public void onclick(view v) {             connection con = new connection(context);             con.connect();             if(con.getconnection()){                 xml = con.getxml();                 textview.settext(xml.getdate());             }else toast.maketext(getapplicationcontext(),                     "no connection", toast.length_long).show();          }      }); } 

error appeares when try set textview. advice. edit: logcat

fatal exception: main java.lang.nullpointerexception         @ com.example.blackess.connectiontest.mainactivity$1.onclick(mainactivity.java:48)         @ android.view.view.performclick(view.java:2485)         @ android.view.view$performclick.run(view.java:9080)         @ android.os.handler.handlecallback(handler.java:587)         @ android.os.handler.dispatchmessage(handler.java:92)         @ android.os.looper.loop(looper.java:130)         @ android.app.activitythread.main(activitythread.java:3687)         @ java.lang.reflect.method.invokenative(native method)         @ java.lang.reflect.method.invoke(method.java:507)         @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:867)         @ com.android.internal.os.zygoteinit.main(zygoteinit.java:625)         @ dalvik.system.nativestart.main(native method) 

this problem of concurrency.

the method connect() :

public void connect() {   ...   if (networkinfo != null && networkinfo.isconnected()) {     new downloadwebpagetask().execute(urlstring);     connection = true;   } } 

does not wait downloadwebpagetask asynctask finish. returns.

what can make listener. put inside connection

public interface connectionlistener {     public void ongotxml(xml xml); } 

add in constructor:

connectionlistener listener; public connection(context context, connectionlistener listener) {     this.context = context;     this.listener = listener; } 

which call in postexecute

@override protected void onpostexecute(string result) {     listener.ongotxml(this.xml); } 

then in activity implement listener:

connection con; textview textview;      public void oncreate(){ ...    textview = (textview) findviewbyid(r.id.textview2);    con = new connection(context, new connection.connectionlistener(){     @override    public void ongotxml(xml xml){       if(xml != null){          textview.settext(xml.getdate());       }else{           toast.maketext(getapplicationcontext(),                 "no connection", toast.length_long).show();       }      }    });     button.setonclicklistener(new view.onclicklistener() {       public void onclick(view v) {          con.connect();       }      });  }  

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -