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 строк.Чтение текстового файла и сохранение его в массиве (разработка Android)
Это ошибка 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());
не было распечатано.
Вашей стек трассировка говорят вам чтобы начать поиск в MainActivity.java в строке 235 для чего-то, что вы пытались использовать, когда оно было null (не инициализировано). Вы не указали имена файлов двух ваших кодовых списков, поэтому сложно сказать, что это может быть. – CandiedOrange
Попробуйте отладить, какой объект является нулевым? –
@Ultimo_m Я думаю, что элемент в array_data равен null. ', если (я == 19) \t \t \t \t \t \t \t {Log.i ("ноль", Data.array_data [0] .ToString()); ... postData();} \t ' Эта часть работает. Но когда я делаю 'Log.i (« первый индекс массива », Data.array_data [0] .toString());' внутри 'postData()', это значение null. Я думаю, что это что-то о 'Thread', что неверно ... –