Hi there,

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());
    }

}

//Here is just some code that creates the GUI

//And finaly the main mehtod
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; 

    }

//Agian just some code that creates the GUI

//And the 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!

This code is just all wrong. contactkeeper is a apckage so contactkeeper.mainscr would have to reference a class "mainscr" in the package "contactkeeper" statically, not a local (in main defined) instance of the class MainScreen. You also do not define the "MainList" thing anywhere, and definately not as a static variable, as you are referencing it. I see where you try to use it both classes, but not where you have defined it.

You need to get away from this "VB"/".Net" way of referencing things. It is different in Java.

Hi masi,
I've also tried changing "mainscr" to "MainScreen" so it references the "MainScreen" class directly. But then it throws the same error and instead of not finding mainscr it doesn't find MainScreen. Would it work if I then make the array static??

About the MainList thing, it's been defined in the code that creates the GUI. nothing fancy, just "List MainList = new List(//then the array that populates it);

Otherwise if i'm just referencing it wrong, what is the right way then? Sorry for just asking you this straight up, but this is how I was taught?

If an instance of "ViewContact" is meant to interact (directly) with an instance of the "MainScreen" class, then it should have a reference to an instance of the "MainScreen" class (i.e. as in passed in in the constructor). As it is all of this code is skewed just enough that I haven't got a clue where to start.

Okay, well, I can post all the code for both classes but it's not going to be pretty. The GUI has been created using netbeans. Anyways, here it is. And thanks again at looking at this eventhough the code is pretty.. interesting.

MainScreen():

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());
    }

}

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jSpinner1 = new javax.swing.JSpinner();
        list1 = new java.awt.List();
        btnSearch = new javax.swing.JButton();
        txtSearch = new javax.swing.JTextField();
        lblHeader = new javax.swing.JLabel();
        MainList = new java.awt.List();
        jButton1 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        mnuItemNewContact = new javax.swing.JMenuItem();

        jScrollPane1.setViewportView(jSpinner1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("ContactKeeper");
        setResizable(false);

        btnSearch.setAction(mnuItemNewContact.getAction());
        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        lblHeader.setIcon(new javax.swing.ImageIcon(getClass().getResource("/contactkeeper/Header.png"))); // NOI18N

        jButton1.setText("View Contact");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jMenu1.setText("Menu");
        jMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu1ActionPerformed(evt);
            }
        });

        mnuItemNewContact.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        mnuItemNewContact.setText("New Contact");
        mnuItemNewContact.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuItemNewContactActionPerformed(evt);
            }
        });
        jMenu1.add(mnuItemNewContact);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(MainList, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE)
                            .addComponent(lblHeader, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(txtSearch, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(82, 82, 82)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblHeader)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnSearch)
                    .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addComponent(MainList, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnSearch, txtSearch});

        pack();
    }// </editor-fold>                        

    private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {                                       
       new NewContact().setVisible(true);
    }                                      

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {                                          
       //Actions to be performed on the Search button
    }                                         

    private void mnuItemNewContactActionPerformed(java.awt.event.ActionEvent evt) {                                                  
        new NewContact().setVisible(true);
    }                                                 

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        new ViewContact().setVisible(true);
    }                                        

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {

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

    // Variables declaration - do not modify                     
    public static java.awt.List MainList;
    private javax.swing.JButton btnSearch;
    private javax.swing.JButton jButton1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSpinner jSpinner1;
    private javax.swing.JLabel lblHeader;
    private java.awt.List list1;
    private javax.swing.JMenuItem mnuItemNewContact;
    private javax.swing.JTextField txtSearch;
    // End of variables declaration                   

}

ViewContact():

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; 

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        lblName = new javax.swing.JLabel();
        lblSurname = new javax.swing.JLabel();
        lblMobile = new javax.swing.JLabel();
        lblHome = new javax.swing.JLabel();
        lblWork = new javax.swing.JLabel();
        lblEmail = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        txtSurname = new javax.swing.JTextField();
        txtMobile = new javax.swing.JTextField();
        txtHome = new javax.swing.JTextField();
        txtWork = new javax.swing.JTextField();
        txtEmail = new javax.swing.JTextField();
        btnEdit = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("View Contact Details");

        lblName.setText("Name");

        lblSurname.setText("Surname");

        lblMobile.setText("Mobile");

        lblHome.setText("Home");

        lblWork.setText("Work");

        lblEmail.setText("Email");

        txtName.setEditable(false);
        txtName.setText("jTextField1");

        txtSurname.setEditable(false);
        txtSurname.setText("jTextField2");

        txtMobile.setEditable(false);
        txtMobile.setText("jTextField3");

        txtHome.setEditable(false);
        txtHome.setText("jTextField4");

        txtWork.setEditable(false);
        txtWork.setText("jTextField5");

        txtEmail.setEditable(false);
        txtEmail.setText("jTextField6");

        btnEdit.setText("Edit");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEditActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnEdit, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnCancel, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblSurname)
                            .addComponent(lblMobile)
                            .addComponent(lblHome)
                            .addComponent(lblWork)
                            .addComponent(lblEmail)
                            .addComponent(lblName))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
                            .addComponent(txtWork, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
                            .addComponent(txtHome, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
                            .addComponent(txtMobile, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
                            .addComponent(txtSurname, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblName)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblSurname)
                    .addComponent(txtSurname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblMobile)
                    .addComponent(txtMobile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblHome)
                    .addComponent(txtHome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblWork)
                    .addComponent(txtWork, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblEmail)
                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnEdit)
                    .addComponent(btnCancel))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void btnEditActionPerformed(java.awt.event.ActionEvent evt) {                                        
        txtName.setEditable(true);
        txtSurname.setEditable(true);
        txtMobile.setEditable(true);
        txtHome.setEditable(true);
        txtWork.setEditable(true);
        txtEmail.setEditable(true);
    }                                       

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {                                          
        this.dispose();
    }                                         

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {

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

    // Variables declaration - do not modify                     
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnEdit;
    private javax.swing.JLabel lblEmail;
    private javax.swing.JLabel lblHome;
    private javax.swing.JLabel lblMobile;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblSurname;
    private javax.swing.JLabel lblWork;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtHome;
    private javax.swing.JTextField txtMobile;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtSurname;
    private javax.swing.JTextField txtWork;
    // End of variables declaration                   

}

Sigh... Still no reply :-(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.