5 Replies - 11276 Views - Last Post: 08 April 2011 - 07:54 AM Rate Topic: -----

#1 Cherri   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-April 11

Get an ArrayList from one class to another

Posted 07 April 2011 - 11:09 PM

Hello to everyone,
Well my problem is that I don't know if I am taking an arrayList from one class to another in a proper way.
Let me explain first that I have 3 classes.
The first class is my ReadFile.java where I read a txt file and store it in an ArrayList<Double> and I return my array. (I know this part is fine cause I have debugged it at school and It didn't seem to have a problem).
The second class is my MyLibrary.java where I have my whole work done there. In this class I have 2 Constractos. One wich gets as variables 2 doubles from the user and the other constractor takes the arrayList. (Both Cosntractors do in the end the same work, just their input differs). (Bellow follows a sample code of my work:)

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(double x, double y)
    {
        this.x =x;
        this.y = y;
    }
public myLibrary(ArrayList<Double> myArray)
    {
        
        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;
    }



In the third class I have my GUI.java where I want to call the constractors and then with x,y call other methods.(My GUI has been created with the ready Forms of NetBeans.
My GUI class is something like this:

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 have:

myArray = new ReadFromFile();
            ml = new MyLibrary(myArray);
            eX = ml.getX();
            eY = nds.getY();
            vs = new ViewSolution(x,y);
            vs.setVisible(true);
            this.setVisible(false);



My problem is that the array throws me java.lang.NullPointerException and I believe it is because in the button i have
myArray = new ReadFromFile();

do you have any idea on helping me?

Thank you in advance.

PS: Sorry for my english.

Is This A Good Question/Topic? 0
  • +

Replies To: Get an ArrayList from one class to another

#2 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Get an ArrayList from one class to another

Posted 08 April 2011 - 02:12 AM

I'm a little bit confused by:

Quote

The first class is my ReadFile.java where I read a txt file and store it in an ArrayList<Double> and I return my array. (I know this part is fine cause I have debugged it at school and It didn't seem to have a problem).


since classes don't "return" things.

It's hard to determine what's going on with your statement,

myArray = new ReadFromFile();


without seeing the constructor, ReadFromFile(), which would likely clear up my confusion with the statement above.
Was This Post Helpful? 0
  • +
  • -

#3 Cherri   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-April 11

Re: Get an ArrayList from one class to another

Posted 08 April 2011 - 03:02 AM

My ReadFile class is:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;


public class ReadFromFile
{
    private ArrayList<Double> myArray;

   

    public myLibrary()
    {
        myArray = new ArrayList<Double>();
        
    }
    public ArrayList<Double> Read()
    {
        myArray = new ArrayList<Double>();
        try
        {
            BufferedReader br = new BufferedReader(new FileReader("numbers.txt"));

            
            String s="0.0";


            while((s =br.readLine()) != null)
            {
                
                myArray.add(Double.valueOf(s));
            }
            br.close(); //close reading the file
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        
        return myArray;

    }

}


Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Get an ArrayList from one class to another

Posted 08 April 2011 - 04:00 AM

So you use the default constructor which just creates an "empty" instance of the class ReadFromFile called myArray. The ReadFromFile class has a method, Read(), which reads from a file and returns an array, but I'm not sure you actually use that method anywhere in your program.

There are a couple things that might be going on:

1. Creating an instance of your ReadFromFile class does not create an array that contains data from the file. That's not how it works. You would have to do something like

    ArrayList<Double> fileData = myArray.read();


where myArray is an instance of the class ReadFromFile.

2. You have a lot of variables called myArray that may be causing confusion. They're not all the same thing, they don't contain the same data, etc. If you have them all straight, then it's fine, but you might consider renaming some of them to make your program more clear.

3. You may not need the class ReadFromFile. Maybe you just need a method in one of your other classes that does what ReadFromFile.read() does.

This post has been edited by GregBrannon: 08 April 2011 - 04:46 AM

Was This Post Helpful? 1
  • +
  • -

#5 DaneAU   User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • Posts: 1,620
  • Joined: 15-May 08

Re: Get an ArrayList from one class to another

Posted 08 April 2011 - 04:51 AM

Your class declaration is also myLibrary when it should be MyLibrary to ensure you alleviate any confusion.
Was This Post Helpful? 0
  • +
  • -

#6 Cherri   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-April 11

Re: Get an ArrayList from one class to another

Posted 08 April 2011 - 07:54 AM

Thank you all very much for your help, I solved the problem differently by using Scanner to read my file.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1