Reading Text File and Store it in Array (Android development) -


if (detectconnection.checkinternetconnection(mainactivity.this))                  {                     try                      {                         fileinputstream fis = new fileinputstream(myinternalfile);                         datainputstream in = new datainputstream(fis);                         bufferedreader br = new bufferedreader(new inputstreamreader(in));                         string strline;                          //0-19                         int i=0;                         int internal_entries=0;                         toast.maketext(mainactivity.this,"start reading", toast.length_long).show();                         while ((strline = br.readline()) != null )                          {                             log.i("index" , integer.tostring(i));                             log.i("read_line" , strline.tostring());                              //data.array_data[i]=strline;                             data.array_data[i]=strline;                              log.i("element_in_array",data.array_data[i].tostring());                                 if(i==19)  //submit after collecting 0-19 results                             {                                 thread t = new thread(new runnable()                                 {                                     @override                                     public void run()                                      {                                         postdata();                                      }                                 });                                 t.start();                                   i=-1;                                  internal_entries++;                                 toast.maketext(mainactivity.this,"submitted entry "+ internal_entries, toast.length_long).show();                                  for(int j=0; j<=19; j++)                                 {                                     data.array_data[j]=null;                                 }                             }                             log.i("what?",data.array_data[0].tostring());                             i++;                         }                           toast.maketext(mainactivity.this,"done reading.", toast.length_long).show();                         in.close();                     }                      catch (ioexception e)                      {                         e.printstacktrace();                     }                      toast.maketext(mainactivity.this,"bye.", toast.length_long).show();                 } 

this program suppose read file , store data in array. each following line in file correspond following element in index. after reading 20 lines in file, array gets submitted google form. array gets deleted , continues read next set of 20 lines.

it's nullpointerexception error. so, 1 element null when try submit array. why there error? seems wrong thread part array declared as:

public static string[] array_data = new string[20];  

in data class.

public void postdata()  {     //httpclient httpclient = new defaulthttpclient();     //13 questions     //12 indices     //22 questions     //21 indices     string fullurl = googleforminfo.google_array[0];     httprequest mreq = new httprequest();       log.i("first index of array",data.array_data[0].tostring());       string col1 = data.array_data[0];     string col2 = data.array_data[1];     string col3 = data.array_data[2];     string col4 = data.array_data[3];     string col5 = data.array_data[4];     string col6 = data.array_data[5];     string col7 = data.array_data[6];     string col8 = data.array_data[7];     string col9 = data.array_data[8];     string col10 = data.array_data[9];     string col11 = data.array_data[10];     string col12 = data.array_data[11];     string col13 = data.array_data[12];     string col14 = data.array_data[13];     string col15 = data.array_data[14];     string col16 = data.array_data[15];     string col17 = data.array_data[16];     string col18 = data.array_data[17];     string col19 = data.array_data[18];     string col20 = data.array_data[19];      log.i("google!",googleforminfo.google_array[1].tostring());       log.i("google!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!",urlencoder.encode(col1).tostring());     string data = googleforminfo.google_array[1] + urlencoder.encode(col1) + "&" +             googleforminfo.google_array[2] + urlencoder.encode(col2) + "&" +             googleforminfo.google_array[3] + urlencoder.encode(col3)+ "&" +             googleforminfo.google_array[4] + urlencoder.encode(col4)+ "&" +             googleforminfo.google_array[5] + urlencoder.encode(col5)+ "&" +             googleforminfo.google_array[6] + urlencoder.encode(col6)+ "&" +             googleforminfo.google_array[7] + urlencoder.encode(col7)+ "&" +             googleforminfo.google_array[8] + urlencoder.encode(col8)+ "&" +             googleforminfo.google_array[9] + urlencoder.encode(col9)+ "&" +             googleforminfo.google_array[10]+ urlencoder.encode(col10)+ "&" +             googleforminfo.google_array[11]+ urlencoder.encode(col11)+ "&" +             googleforminfo.google_array[12]+ urlencoder.encode(col12)+ "&" +             googleforminfo.google_array[13]+ urlencoder.encode(col13)+ "&" +             googleforminfo.google_array[14]+ urlencoder.encode(col14)+ "&" +             googleforminfo.google_array[15]+ urlencoder.encode(col15)+ "&" +             googleforminfo.google_array[16]+ urlencoder.encode(col16)+ "&" +             googleforminfo.google_array[17]+ urlencoder.encode(col17)+ "&" +             googleforminfo.google_array[18]+ urlencoder.encode(col18)+ "&" +             googleforminfo.google_array[19]+ urlencoder.encode(col19)+ "&" +             googleforminfo.google_array[20]+ urlencoder.encode(col20);      string response = mreq.sendpost(fullurl, data);     //log.i(mytag, response); } 

enter image description here

note that:

  log.i("first index of array",data.array_data[0].tostring()); 

wasn't printed out.

thread t calls postdata() , static array_data original thread nulls code:

for(int j=0; j<=19; j++) {     data.array_data[j]=null; } 

thread t sharing array_data original thread. have provide postdata() it's own copy (preferably immutable) if you're going this.

why? because threads don't act in particular order. may have thought t should done before nulled not guarantied. in fact, not guarantying whole point of threads. you're lucky did when tested can fix it.

you need stop sharing array_data making static. instead make new instance every read , pass thread t. way once it's made can think of thread t's copy , never write again. copy should immutable no thread can ever change making safe read @ time.

you can't make array immutable can shove data in arraylist , pass thread. has post trying make array's immutable i'll point that:

is there way make ordinary array immutable in java?

you can away kind of stuff you've been doing if you're talking strings because string object immutable already. array of strings not.

passing immutable copy work extent when you're ready more robust way of tackling problem observer pattern.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -