【问题标题】:Reading Text File and Store it in Array (Android development)读取文本文件并将其存储在数组中(Android 开发)
【发布时间】:2014-06-07 22:35:55
【问题描述】:
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());

没有打印出来。

【问题讨论】:

  • 您的堆栈跟踪告诉您开始在第 235 行的 MainActivity.java 中查找您在 null 时尝试使用的内容(未初始化)您没有指出两个代码的文件名列表,所以很难说它可能是什么。
  • 尝试调试一下,为空的对象是什么?
  • @Ultimo_m 我认为 array_data 中的元素为空。 if(i==19) {Log.i("zero",Data.array_data[0].toString()); ...postData();} 这部分有效。但是当我在postData() 中执行Log.i("first index of array",Data.array_data[0].toString()); 时,它是一个空值。我认为这是关于 Thread 不正确的事情......

标签: android arrays file-io nullpointerexception crash


【解决方案1】:

线程 t 调用 postData() 和 static 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 已经有一篇关于尝试使数组不可变的帖子,所以我只会指出这一点:

Is there any way to make an ordinary array immutable in Java?

如果您只谈论字符串,您可以摆脱一直在做的那种事情,因为字符串对象已经是不可变的。但是字符串数组不是。

传递不可变副本在一定程度上会起作用,但当您准备好采用更强大的方法来解决此问题时,请查看observer pattern。

【讨论】:

  • if(i==19) {Log.i("zero",Data.array_data[0].toString()); ...postData();} 这部分有效。但是当我在postData() 中执行Log.i("first index of array",Data.array_data[0].toString()); 时,它是一个空值。我认为这是关于 Thread 不正确的事情......
  • @Junuthunlum 线程t 的唯一问题是它信任原始线程而不是与它的数据搞混。请参阅我的更新答案。
【解决方案2】:

试试这个代码:

Thread t = new Thread(new Runnable()
    {
        @Override
        public void run() 
        {
            postData(Data.array_data);
        }
    });
t.start();

在您的 Data 类中,postData 方法是:

public void postData(String arrayData[]) 
{
   ...

这样做可以避免静态数组出现问题

【讨论】:

  • 您让他将 static 传递给 postData。静态的问题是只有一个副本,因此必须共享。这意味着他仍然无法再次阅读(或毫无意义地将其设置为 null),因为他永远不知道 postData 何时完成。他需要创建新实例(最好是不可变的),一旦将它们交给 postData,就永远不要碰它们。如果他创建了一个新实例,他可以随时从文件中读取。
  • 是的,没错,对我来说另一种解决方案是在 postData 方法下调用使数组为空的函数。因此,在他完成工作后,他总是会将数组设为 null。
  • 我不明白它是如何工作的。没有制作独特的副本。当他开始清空它的元素时,简单地对同一个数据结构有两个引用是没有帮助的。
猜你喜欢
  • 2020-02-09
  • 1970-01-01
  • 2013-11-19
  • 1970-01-01
  • 2018-05-30
  • 1970-01-01
  • 2015-04-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多