[英]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();
}
假定該程序讀取文件並將數據存儲在數組中。 文件中的每一行都對應於索引中的以下元素。 讀取文件中的20行后,該數組將提交給Google表單。 然后刪除數組,並繼續讀取下一組20行。
這是一個nullpointerexception
錯誤。 因此,當我嘗試提交數組時,一個元素為null
。 為什么會出現錯誤? Thread
部分似乎有問題我的數組聲明為:
public static String[] array_data = new String[20];
在Data
類中。
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);
}
注意:
Log.i("first index of array",Data.array_data[0].toString());
沒有打印出來。
線程t
使用以下代碼調用postData()
和原始線程為null的靜態 array_data
:
for(int j=0; j<=19; j++)
{
Data.array_data[j]=null;
}
線程t
與您的原始線程共享此array_data
。 如果要執行此操作,則必須為其提供postData()
自己的副本(最好是不可變的)。
為什么? 因為線程不按任何特定順序運行。 您可能以為應該在作廢之前先完成t
但這不能保證。 實際上,這並不是線程的全部要點。 您很幸運,它在測試時就做到了,因此可以對其進行修復。
您需要通過將其靜態化來停止共享array_data。 而是為每次讀取創建一個新實例,並將其傳遞給線程t
。 這樣一來,您就可以將其視為線程t
的副本,而無需再次寫入。 該副本應該是不可變的,因此沒有線程可以更改它,因此可以隨時安全讀取。
您不能使數組不可變,但是可以將數據推入ArrayList並將其傳遞給線程。 SO已經有一篇關於使數組不可變的文章,所以我只想指出一下:
如果您只在談論Strings,因為String對象已經是不可變的,那么您可以擺脫所做的工作。 但是不是一個字符串數組。
傳遞不可變的副本在某種程度上可以起作用,但是當您准備好以更健壯的方式解決此問題時,請查看觀察者模式 。
試試這個代碼:
Thread t = new Thread(new Runnable()
{
@Override
public void run()
{
postData(Data.array_data);
}
});
t.start();
在您的Data類中,postData方法為:
public void postData(String arrayData[])
{
...
這樣做可以避免靜態數組出現問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.