Results 1 to 6 of 6
  1. #1
    ScienceLife is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Exclamation Get an ArrayList from another class

    Hello everybody,
    I have a problem (obviously!)
    I have a class ReadFile where I read a file and store my data in an ArrayList of Doubles. I have another class where is my library and there I do all the work (I have ther 2 Constractors, one wich will get 2 doubles from the user and one wich will get the ArrayList from the file).
    I want to take the ArrayList and put it in the second constractor.

    What I have achieved so far?
    Well I have read the data and put it in the ArrayList (i know it is correct cause I Debugged it).
    But I don't know what wrong i have done in my library... Ok I'm gonna what I do in the Library and please correct me if I am wrong.

    Java Code:
    public class myLibrary
    {
        ArrayList<Double> double_numbers;
        ReadFile rf = new ReadFile();    
        private Double x, y;
    
    //The set and get methods are supposed to work for the 1st constractor too.
    public void setX(Double x)
        {
            this.x =x;
        }
    
        public double getX()
        {
            return x;
        }
    
        public void setY(double y)
        {
            this.y =y;
        }
    
        public double getY()
        {
            return y;
        }
    public myLibrary(ArrayList<Double> myArray)
        {
            double_numbers =rf.Read();
         
            myArray = double_numbers;
            x = A_function(myArray);
            y = Another_function(myArray);
            
        }
    
        public double A_function(ArrayList<Double> myArray)
        {
            
            
            double local_x, k=0;
    
            for (int i=0; i<  myArray.size(); i++)
                       k += i;
    
            local_x = k;
            return local_x;
        }
    
        public double Sd(ArrayList<Double> myArray)
        {
          
            double local_y=0, k=0;
            double local_x = A_function( myArray);
            for(int i=0; i< myArray.size(); i++)
            {
                k += 2*local_x;
            }
    
    
            
                local_y = k;
            
            
            return local_y;
        }
    The above functions are not the true ones cause it's for an assignment and I don't know if I am allowed to put my work here.

    Is this code wrong? or my problem is elsewhere?
    Thank in advance for your time!

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    299
    Rep Power
    4

    Default

    Are there any errors?
    Can you tell us what goes wrong?

    Check this part of code, it makes no sense.

    Java Code:
    public myLibrary(ArrayList<Double> myArray) { // here you provide an arrayList as a parameter
            double_numbers =rf.Read(); // but here you allegedly read fresh new array from file.
         
            myArray = double_numbers; // and you override provided myArray with imported one, 
            // so why bother to provide an arraylist if you are going to override it with new one
            x = A_function(myArray);
            y = Another_function(myArray);
            
    }

  3. #3
    ScienceLife is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    Thank you for your reply!
    Yes I believe that this is one of the majority of the problems.


    Well I tryied it just now and it throughs me a java.lang.NullPointerException. Maybe because I haven't inicialized the array?

  4. #4
    ScienceLife is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Default

    Probably the java.lang.NullPointerException is being throughn cause I haven't declare well the arrayList in the class that I call the constractor?
    (Bellow is the code I have from my GUI.java)

    Java Code:
    public class GUI extends javax.swing.JFrame {
    myLibrary ml;
        ChoseWay cw; //Another class of Gui
        
    
        private ArrayList<Double> myArray;
    
        ReadFile readF;
        double eX, eY;
        ViewSolution vs;
       
        public GUI() {
            initComponents();
            vs = new ViewSolution();
            myArray = new ArrayList<Double>();
            eX = 0;
            eY =0;
           
            readF = new ReadFromFile();
            ml = new myLibrary(myArray);
        }
    And than when the user will click the submit button I call the distructor:

    Java Code:
    myArray = new ReadFromFile();
                ml = new MyLibrary(myArray);
                eX = ml.getX();
                eY = nds.getY();
                vs = new ViewSolution(x,y);
                vs.setVisible(true);
                this.setVisible(false);
    am i doing it right?

  5. #5
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    299
    Rep Power
    4

    Default

    Hi ScienceLife.

    I'm not sure if I understand this: you have one class with two constructors and you want "to take the ArrayList and put it in the second constractor".
    See SecondClass below that has two constructors. If you provide its first constructor with an ArrayList <Double> , that constructor will call the second one which takes two doubles as parameters.
    Was that your question, and is this explanation clear enough to you?

    Java Code:
    class FirstClass{
        
        SecondClass secondClass;
        double eX = 1.0;
        double eY = 2.0;
    
        public FirstClass(){
    
            ArrayList <Double> myArray = new ReadFromFile().getArray();  // don't pay attention at the moment
            
            // now you can use one of [I]Second[/I]'s constructors:
            secondClass = new SecondClass(myArray);
    
            // or
            secondClass = new SecondClass(eX, eY);
        }
    }
    
    class SecondClass{
        
        public SecondClass(ArrayList <Double> array){  // assuming array has two elements
    
            double x = ((Double) array.get(0)).doubleValue();
            double y = ((Double) array.get(1)).doubleValue();
    
            this(x, y); // this calls next constructor
        }
    
        public SecondClass(double X, double Y){
            System.out.println("numbers: " + X + ", " + Y);
        }
    }


    Since I'm not sure what your intention is I will cover few irregularities I have spotted.

    Java Code:
    myArray = new ReadFromFile(); // did you try this, is it working?
    
        ml = new MyLibrary(myArray); // if [I]MyLibrary()[/I] is already declared this is ok
    
        eX = ml.getX(); // this is ok if you have [I]getX()[/I] method in [I]MyLibrary[/I] with correct return type
    
        eY = nds.getY(); // where this [B]nds[/B] comes from?
    
        vs = new ViewSolution(x,y); // yes, proper way to instantiate but where this [B]x and y[/B] comes from?
    
        // if they are [B]eX[/B] and [B]eY[/B] you can skipp them in next way
    
        // vs = new ViewSolution(ml.getX(), nds.getY()); 
    
        vs.setVisible(true);  // I'm not sure why you are doing this, it could be, but it is hardly a valid solution
        this.setVisible(false);

    Java Code:
    public GUI() {
            initComponents();
            vs = new ViewSolution(); // I'm confused is this the same ViewSolution from the above code?
            myArray = new ArrayList<Double>(); // myArray is just declared here, it is still empty
            eX = 0;
            eY =0;
           
            readF = new ReadFromFile(); // again
            ml = new myLibrary(myArray); // you provided myLibrary with empty array
            // what is purpose of [I]myLibrary[/I] (MyLibrary is more "java")
            // just to get elements of [I]myArray[/I] or it actually do something with [I]myArray[/I]
        }

    I don't know if this is of any help or not.
    Show me a line that returns NPE.

  6. #6
    ScienceLife is offline Member
    Join Date
    Nov 2010
    Posts
    7
    Rep Power
    0

    Smile

    Thanks a lot for your help. It was very usefull at some points that I had not noticed I had worng declaration or calling (I wander why the compiler hadn't catch them).
    I managed to solve my problem by changing the ReadFile() (I firstly read my data with BufferedReader, and now I read the file with Scanner). And also I did not have a Constractor in the ReadFile().
    Thank you very much for helping to solve my other problems too! :)

Similar Threads

  1. Java Arraylist (.get from other class)
    By Kingarmy in forum New To Java
    Replies: 1
    Last Post: 12-28-2010, 08:00 PM
  2. Class Instances stored in an ArrayList
    By Ersk in forum New To Java
    Replies: 4
    Last Post: 12-12-2009, 04:13 PM
  3. [newbie] getting class members from Arraylist
    By jon80 in forum New To Java
    Replies: 16
    Last Post: 05-15-2009, 07:45 AM
  4. Replies: 3
    Last Post: 03-28-2009, 05:02 AM
  5. Help passing arraylist to another class
    By adlb1300 in forum New To Java
    Replies: 3
    Last Post: 11-06-2007, 08:02 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •