4 Replies - 1633 Views - Last Post: 06 December 2010 - 04:18 PM Rate Topic: -----

#1 Dean_Grobler  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 390
  • Joined: 15-January 10

Accessing Objects in External Classes

Posted 06 December 2010 - 02:48 AM

Hi there guys!

I am getting a 'Cannot find symbol error' on an array in my "ViewContact" Class. The array has been created in the "MainScreen" class. Below I have pasted the code for both classes as to provide you with as much information as possible:

The MainScreen Class:

package contactkeeper;
import java.io.*;
import java.util.*;
/**
 *
 * @author Dean Grobler
 */
public class MainScreen extends javax.swing.JFrame{

    /** Creates new form MainScreen */
    public MainScreen() {
        initComponents();
        readInfo();
    }

    //Declare variables
    BufferedReader inputStream;
    String conInfo;
    String infoArray[];
    ArrayList<Contact> contactObj = new ArrayList<Contact>();

    public void readInfo(){
    try{
        inputStream = new BufferedReader(new FileReader("conList.txt"));
    }
    catch(FileNotFoundException e){}

    try{
        while ((conInfo = inputStream.readLine()) != null) {
             infoArray = conInfo.split("\\s+");

            //Create new Contact Object
            Contact newCon = new Contact(infoArray[0],infoArray[1],infoArray[2],
                                        infoArray[3],infoArray[4],infoArray[5]);

            //Add new Contact to ArrayList
            contactObj.add(newCon);
        }

    }
    catch(IOException v){}

    //Convert ArrayList to Array and Print on MainScreen
    int listSize = contactObj.size();
    Contact contactArray[];
    contactArray = contactObj.toArray(new Contact[listSize]);
    for(int i = 0; i < listSize; i++){
        MainList.add(contactArray[i].getName()+" "+ contactArray[i].getSurname());
    }

    //Then here is just the code that creates the GUI, declares variables and all the ActionPerformeds

    //The main Method
    public static void main(String args[]) {

        MainScreen mainscr = new MainScreen();
        mainscr.setVisible(true);
    }

}



The ViewContact Class:

package contactkeeper;

/**
 *
 * @author Dean Grobler
 */
public class ViewContact extends javax.swing.JFrame {

    /** Creates new form ViewContact */
    public ViewContact() {
        initComponents();
        printInfo();
    }

    public void printInfo(){
       int index = contactkeeper.MainScreen.MainList.getSelectedIndex();
       txtName.setText(contactKeeper.mainscr.contactArray[index].getName());			//Error: Cannont find symbol, class mainscr; 

    }

    //Again just code that creates GUI, variables, ActionPerformeds

    //Main method
    public static void main(String args[]) {

        ViewContact view = new ViewContact();
        view.setVisible(true);
    }
}



As you can see, the error gets trown in the ViewContact Class, inside the PrintInfo() mehtod. I have added a comment where the error gets thrown. I've been struggling quite a bit in accessing other objects and variables in other classes lately so I think there's something quite obvious that I'm missing here.

Thanks alot guys! :surrender:

PS, if you're Masijade, you already looked at this..

Is This A Good Question/Topic? 0
  • +

Replies To: Accessing Objects in External Classes

#2 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: Accessing Objects in External Classes

Posted 06 December 2010 - 02:54 AM

Namely here.
Was This Post Helpful? 0
  • +
  • -

#3 Dean_Grobler  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 390
  • Joined: 15-January 10

Re: Accessing Objects in External Classes

Posted 06 December 2010 - 03:23 AM

I realy did try, I just can't seem to get it to work..
Just thought I'd ask a second opinion (No offense meant in anyway masi)
Was This Post Helpful? 0
  • +
  • -

#4 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Accessing Objects in External Classes

Posted 06 December 2010 - 04:05 PM

If you want to access a variables from one class to another, you can do this.

public class A {

	static int apple=4;
        int pear=4;
}



and access them here


public class B {

	public static void main(String [] args){
		//access static variable
		int apple = A.apple;
		//create new instance and access variable
		int pear = new A().pear;
	}
	
}



Hope this helps.

This post has been edited by m-e-g-a-z: 06 December 2010 - 04:05 PM

Was This Post Helpful? 1
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,390
  • Joined: 27-December 08

Re: Accessing Objects in External Classes

Posted 06 December 2010 - 04:18 PM

Also, don't use the NetBeans GUI Builder. It produces code that no human can read. Swing is so easy that you should write all your GUIs by hand. In fact, we have a ton of good tutorials on Swing in the Java Tutorials Section.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1