Nidhin_toms
Nidhin_toms

Reputation: 737

Error while passing value from arrayList to object array

package mp1similar;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import EarthquakeRecord.Earthquakerecd;


public class MP1Similar 
{

private static ArrayList arrayList  ;

public static void main(String[] args) 
{
 ArrayList arrayList= null;
    try 
    {
    BufferedReader br = new BufferedReader(new FileReader("data/Catalog.txt"));

    String line="";
    arrayList =new ArrayList();
    while((line = br.readLine())!=null)
        {
      //  System.out.println(line);
        StringTokenizer st = new StringTokenizer(line);
        while(st.hasMoreTokens())
            {
          //System.out.println(st.nextToken());
            arrayList.add(st.nextToken());
            //System.out.println(br.readLine());
            }
        }
    }

    catch (FileNotFoundException ex)
    {
         System.out.println(ex.getMessage());
         ex.printStackTrace();
    }
catch (IOException ex)
    {
          System.out.println(ex.getMessage());
          ex.printStackTrace();
    }
          int j=0;
    Earthquakerecd E[]= new Earthquakerecd[2000];
     for(int i=0;i< arrayList.size();i++)
    {
        System.out.println(arrayList.get(i));
        E[j] = new Earthquakerecd();


       E[j].setDate(arrayList.get(i));



    if (j>35 )
    {
        j=0;
    } 
    j++;

    }



}

}

I am getting an error when i pass the the values from arrayList to E[j]setDate. It says method in setDates cannot be applied to given types. aL an object of Arraylist. I have edited the code to include everything . Basically it is a code to read data from a TXT file . There are 35 columns and more than 1500 rows . Each column pertains to a certain attribute like date , name etc.

Upvotes: 0

Views: 312

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500675

You're trying to use an assignment operator on the result of a method call here:

E[j].setDate()=(aL.get(i));

I suspect you wanted:

E[j].setDate(aL.get(i));

However, that will then fail with a NullPointerException because you're never initializing E[j] - it will always be a null reference. I suspect you want:

E[j] = new Earthquakerec();

in the loop somewhere. You'll also probably want to increment j at some point... (It's not clear what the various magic numbers here are for - I suspect you could write the whole code a lot more clearly, but it's hard to suggest improvements when we don't know what it's trying to achieve.)

Upvotes: 2

Related Questions