1
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 is suppose to read a file and store the data in an array. Each following line in the file correspond to the following element in an index. After reading 20 lines in the file, the array gets submitted to a Google Form. Then the array gets deleted and continues to read the next set of 20 lines.

It's a nullpointerexception error. So, one element is null when I try the submit the array. Why is there an error? It seems to be something wrong with the Thread part My array is declared as:

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

in the 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.

  • Your stack trace is telling you to start looking in MainActivity.java on line 235 for something you tried to use when it was null (not initialized) You haven't indicated the file names of your two code listings so it's hard to tell what it could be. – candied_orange Jun 07 '14 at 23:44
  • Try to debug, what is the object that is null? – Ultimo_m Jun 08 '14 at 00:14
  • @Ultimo_m I think the element in the array_data is null. `if(i==19) {Log.i("zero",Data.array_data[0].toString()); ...postData();} ` This part works. But when I do `Log.i("first index of array",Data.array_data[0].toString());` inside the `postData()`, it's a null. I think it's something about the `Thread` that is incorrect... – Junuthun lum Jun 08 '14 at 03:55

2 Answers2

0

Thread t calls postData() and the static array_data that your original thread nulls with this code:

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

Thread t is sharing this array_data with your original thread. You have to provide postData() with it's own copy (preferably immutable) if you're going to do this.

Why? Because threads don't act in any particular order. You may have thought t should be done before you nulled but that is not guarantied. In fact, not guarantying that is the whole point of threads. You're lucky it did this when you tested it so you can fix it.

You need to stop sharing array_data by making it static. Instead make a new instance for every read and pass it to thread t. That way once it's made you can think of it as thread t's copy and never write to it again. The copy should be immutable so no thread can ever change it thus making it safe to read at any time.

You can't make an array immutable but you can just shove your data in an ArrayList and pass that to your thread. SO already has a post about trying to make array's immutable so I'll just point you to that:

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

You can get away with the kind of stuff you've been doing if you're only talking about Strings because a String object is immutable already. But an array of Strings is not.

Passing the immutable copy will work to some extent but when you're ready for a more robust way of tackling this problem look into the observer pattern.

Community
  • 1
  • 1
candied_orange
  • 7,036
  • 2
  • 28
  • 62
  • `if(i==19) {Log.i("zero",Data.array_data[0].toString()); ...postData();} ` This part works. But when I do `Log.i("first index of array",Data.array_data[0].toString());` inside the `postData()`, it's a null. I think it's something about the `Thread` that is incorrect... – Junuthun lum Jun 08 '14 at 03:53
  • @Junuthunlum The only problem thread `t` has is that it trusted the original thread not screw with it's data. See my updated answer. – candied_orange Jun 08 '14 at 05:23
0

Try this code:

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

and in your Data class, postData method is:

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

Doing this you avoid problems with your static array

Ultimo_m
  • 4,724
  • 4
  • 38
  • 60
  • You have him passing a **static** into postData. The problem with static is there is only the one copy so it has to be shared. That means he still can't ever read again (or pointlessly set it to null) since he never knows when postData is done. He needs to make new instances (preferably immutable) and never touch them once he hands them to postData. If he makes a new instance he can read from the file whenever he wants. – candied_orange Jun 09 '14 at 05:33
  • Yes that is right, for me another solution would be to call the function that makes array null below postData method. So he will always make the array null after he has finished work with that. – Ultimo_m Jun 09 '14 at 07:21
  • I don't see how that works. No unique copy has been made. Simply having two references to the same data structure isn't going to help when he starts to null it's elements. – candied_orange Jun 10 '14 at 00:32