java - Android NullPointerException Bluetooth -


my app crashes , gives me error when click on button2 seen in gamesfragment activity below. want able click button , send data bluetooth device calling writedata function in other toprated activity.

public class gamesfragment extends fragment {     public button chaser;      private string datatosend;     view rootview;       @override     public void onactivitycreated(bundle savedinstancestate) {         super.onactivitycreated(savedinstancestate);          chaser = (button) rootview.findviewbyid(r.id.button2);          chaser.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {                 switch (view.getid()){                     case r.id.button2:{                         datatosend = "on";                         topratedfragment top = new topratedfragment();                          top.writedata(datatosend);                       }                 }             }         });     }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,                              bundle savedinstancestate) {          rootview = inflater.inflate(r.layout.fragment_games, container, false);          return rootview;     } }  /******* writedata function is***/     public class topratedfragment extends fragment {     private arrayadapter<string> mnewdevicesarrayadapter;     private outputstream outstream = null;     private string datatosend;      private bluetoothadapter bluetoothadapter;     public static string extra_device_address = "device_address";     private bluetoothsocket btsocket = null;     public static final uuid my_uuid = uuid.fromstring("00001101-0000-1000-8000-00805f9b34fb");      public button connect;     public button disconnect;     view rootview;      @override     public void onactivitycreated(bundle savedinstancestate) {         super.onactivitycreated(savedinstancestate);         bluetoothadapter = bluetoothadapter.getdefaultadapter();         final set<bluetoothdevice> paireddevices = bluetoothadapter.getbondeddevices();           // initialize array adapters. 1 paired devices ,         // 1 newly discovered devices         final arrayadapter<string> paireddevicesarrayadapter =                 new arrayadapter<string>(getactivity(), r.layout.device_name);         mnewdevicesarrayadapter = new arrayadapter<string>(getactivity(), r.layout.device_name);         // register broadcasts when device discovered         intentfilter filter = new intentfilter(bluetoothdevice.action_found);         ((mainactivity)getactivity()).registerreceiver(this.mreceiver,filter);            // find , set listview paired devices         listview pairedlistview = (listview) getactivity().findviewbyid(r.id.listview);         pairedlistview.setadapter(paireddevicesarrayadapter);         pairedlistview.setonitemclicklistener(mdeviceclicklistener);          // register broadcasts when discovery has finished         filter = new intentfilter(bluetoothadapter.action_discovery_finished);         ((mainactivity)getactivity()).registerreceiver(this.mreceiver, filter);             connect = (button) rootview.findviewbyid(r.id.button);          connect.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {                 switch (view.getid()){                     case r.id.button:{                         intent intent = new intent(bluetoothadapter.action_request_enable);                         startactivityforresult(intent, 1);                          dodiscovery();                         if (paireddevices.size() > 0) {                             (bluetoothdevice device : paireddevices) {                                 paireddevicesarrayadapter.add(device.getname() + "\n" + device.getaddress());                             }                         }                      }                 }             }         });      }      @override     public view oncreateview(layoutinflater inflater, viewgroup container,             bundle savedinstancestate) {           rootview = inflater.inflate(r.layout.fragment_top_rated, container, false);          return rootview;     }       private void dodiscovery() {         //log.d(tag, "dodiscovery()");          // indicate scanning in title         //setprogressbarindeterminatevisibility(true);          // if we're discovering, stop         if (bluetoothadapter.isdiscovering()) {             bluetoothadapter.canceldiscovery();         }          // request discover bluetoothadapter         bluetoothadapter.startdiscovery();     }      @override     public void ondestroy() {         super.ondestroy();          // make sure we're not doing discovery anymore         if (bluetoothadapter!= null) {             bluetoothadapter.canceldiscovery();         }          // unregister broadcast listeners         //this.unregisterreceiver(mreceiver);         try {             btsocket.close();         } catch (ioexception e) {         }     }     private adapterview.onitemclicklistener mdeviceclicklistener             = new adapterview.onitemclicklistener() {         public void onitemclick(adapterview<?> av, view v, int arg2, long arg3) {             // cancel discovery because it's costly , we're connect             bluetoothadapter.canceldiscovery();              // device mac address, last 17 chars in view             string info = ((textview) v).gettext().tostring();             string address = info.substring(info.length() - 17);              // create result intent , include mac address             intent intent = new intent();             intent.putextra(extra_device_address, address);              bluetoothdevice device = bluetoothadapter.getremotedevice(address);             try {                 btsocket = device.createrfcommsockettoservicerecord(my_uuid);                 btsocket.connect();                 toast.maketext(getactivity(),"connected " + device.getname() ,toast.length_short).show();                  //log.d(tag, "connexion r�ussie !!");             } catch (ioexception e) {                 try {                     btsocket.close();                 } catch (ioexception e2) {                     //log.d(tag, "impossible de fermer la connexion.");                 }                 //log.d(tag, "cr�ation de socket �chou�e.");             }             // set result , finish activity             //setresult(activity.result_ok, intent);             //finish();         }     };      private final broadcastreceiver mreceiver = new broadcastreceiver() {         @override         public void onreceive(context context, intent intent) {             string action = intent.getaction();              // when discovery finds device             if (bluetoothdevice.action_found.equals(action)) {                 // bluetoothdevice object intent                 bluetoothdevice device = intent.getparcelableextra(bluetoothdevice.extra_device);                 // if it's paired, skip it, because it's been listed                 if (device.getbondstate() != bluetoothdevice.bond_bonded) {                     mnewdevicesarrayadapter.add(device.getname() + "\n" + device.getaddress());                 }                 // when discovery finished, change activity title             } else if (bluetoothadapter.action_discovery_finished.equals(action)) {                 //setprogressbarindeterminatevisibility(false);                 if (mnewdevicesarrayadapter.getcount() == 0) {                     //do nothing                 }             }         }     };      protected void writedata(string data) {          try {             outstream = btsocket.getoutputstream();         } catch (ioexception e) {             //log.d(tag, "bug avant l'envoie.", e);         }          string message = data;          byte[] msgbuffer = message.getbytes();          try {             outstream.write(msgbuffer);         } catch (ioexception e) {               //log.d(tag, "bug durant l'envoie.", e);         }     } } 

this error get:

java.lang.nullpointerexception: attempt invoke virtual method 'java.io.outputstream android.bluetooth.bluetoothsocket.getoutputstream()' on null object reference         @ info.androidhive.tabsswipe.topratedfragment.writedata(topratedfragment.java:207)         @ info.androidhive.tabsswipe.gamesfragment$1.onclick(gamesfragment.java:44)         @ android.view.view.performclick(view.java:5197)         @ android.view.view$performclick.run(view.java:20926) 

your outstream in topratedfragment null, , never initialize. have initialize outstream first, use in write data. btw, never use fragments - best solution move writedata method class , init outstream in constructor or in method.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -