Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Accessing Variables Between Classes

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Accessing Variables Between Classes

    I'm trying to learn the ins and outs of Classes and especially Objects, but I'm having trouble accessing the variables between classes.

    So far, I've got the main() starting the ball rolling by calling the startProgram() method of the Program class.

    In the Program class, I have it creating objects to build the UI:

    package control;
    import ui.*;
    import java.awt.*;
    import javax.swing.*;
     
    public class Program {
    	public static void startProgram() {
    		Frame window = new JFrame("CoordinateCalculator");
    		DisplayPanel displayPanel = new DisplayPanel();
    		NavPanel navPanel = new NavPanel();
    		NumPanel numPanel = new NumPanel();
    		KeyEntry keyEntry = new KeyEntry();
    		MouseEntry mouseEntry = new MouseEntry();
    		window.setLayout(new BorderLayout(5,10));
    		window.add(displayPanel.panel, BorderLayout.NORTH);
    		window.add(navPanel.panel,BorderLayout.CENTER);
    		window.add(numPanel.panel, BorderLayout.SOUTH);
    		window.pack();
    		window.setLocation(250,250);
    		window.setResizable(false);
    		window.setVisible(true);
    	}
    }

    The class used to build the displayPanel object:

    package ui;
    import java.awt.*;
    import java.text.NumberFormat;
    import javax.swing.*;
     
    public class DisplayPanel {
    	public JPanel panel = new JPanel();
    	public JLabel display = new JLabel();
    	public JFormattedTextField object1a = new JFormattedTextField(NumberFormat.getInstance());
    	public JFormattedTextField object1b = new JFormattedTextField(NumberFormat.getInstance());
    	public JFormattedTextField object1c = new JFormattedTextField(NumberFormat.getInstance());
    	public JFormattedTextField object2a = new JFormattedTextField(NumberFormat.getInstance());
    	public JFormattedTextField object2b = new JFormattedTextField(NumberFormat.getInstance());
    	public JFormattedTextField object2c = new JFormattedTextField(NumberFormat.getInstance());
     
    	public DisplayPanel() {
    		panel.setLayout(new GridLayout(3,3));
    		panel.add(object1a);
    		panel.add(object1b);
    		panel.add(object1c);
    		panel.add(object2a);
    		panel.add(object2b);
    		panel.add(object2c);
    		panel.add(display);
    	}
    }

    The next part I'm having trouble with. I have a DataHandling() class, which holds the method doMath() where the magic happens:

    package math;
    import ui.*;
    import java.awt.*;
    import javax.swing.*;
     
    public class DataHandling {
    	public static void doMath() {
    		double object1A = Double.parseDouble(displayPanel.object1a.getText());
    		double object1B = Double.parseDouble(displayPanel.object1b.getText());
    		double object1C = Double.parseDouble(displayPanel.object1c.getText());
    		double object2A = Double.parseDouble(displayPanel.object2a.getText());
    		double object2B = Double.parseDouble(displayPanel.object2b.getText());
    		double object2C = Double.parseDouble(displayPanel.object2c.getText());
     
    		double objectA = object1A - object2A;
    		double objectB = object1B - object2B;
    		double objectC = object1C - object2C;
     
    		if (objectA < 0) {
    			objectA *= -1;
    		}
    		if (objectB < 0) {
    			objectB *= -1;
    		}
    		if (objectC < 0) {
    			objectC *= -1;
    		}	
     
    		String i = Double.toString(Math.sqrt((objectA * objectA) + (objectB * objectB) + (objectC * objectC)));
     
    		displayPanel.display.setText(i);
    		}
    }

    I'm getting the error 'displayPanel cannot be resolved', since displayPanel is the variable referencing the DisplayPanel() object, I thought that I could use 'displayPanel.object1a' to access that item (for instance), but I can only get that to work is from in the Program() class... So, did I set something up wrong? Am I using the wrong syntax to access the data in question? What am I obviously missing?


    Thank you kindly,


    Chris


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Accessing Variables Between Classes

    I'm getting the error 'displayPanel cannot be resolved'
    Please copy and paste here the full text of the error message.

    Where is the variable displayPanel defined? Is it in scope (within the same {}s) where you are trying to use it?

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Accessing Variables Between Classes

    Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
    displayPanel cannot be resolved to a variable

    displayPanel is defined in the startProgram() method of the Program() class.

    I am trying to use it in another class, specifically the DataHandling() class.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Accessing Variables Between Classes

    I am trying to use it in another class
    Is it in scope where you are trying to use it?
    Classes are independent of each other. What's in one can not be used in another without referencing the class that the variable is defined in.

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Accessing Variables Between Classes

    I believe it's within scope, declared public imported the package... I've tried both with and without full name 'Program.displayPanel.object1a.getText();'

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Accessing Variables Between Classes

    The import statement does not put a class variable in scope.
    Your compiler(?) error message does not give all the information that many compilers provide.
    For example my compiler gives this:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^

    Can you compile your program and get an error message like this one?

  7. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Accessing Variables Between Classes

    It compiles with these errors:


    JAR export finished with warnings. See details for additional information.
    Exported with compile warnings: Coordinate Calculator/src/CoordinateCalculator.java
    Exported with compile warnings: Coordinate Calculator/src/control/ActionHandling.java
    Exported with compile errors: Coordinate Calculator/src/control/MouseEntry.java
    Exported with compile errors: Coordinate Calculator/src/math/DataHandling.java


    But gave no actual warnings. When I run it, it doesn't throw an error or even have the decency to crash.


    In the emulator, it throws the following:

    Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem:
    displayPanel cannot be resolved to a variable

    at control.MouseEntry.actionPerformed(MouseEntry.java :20)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

    These are the only errors I can get out of it.

  8. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Accessing Variables Between Classes

    double object1A = Double.parseDouble(displayPanel.object1a.getText());
    How can you access displayPanel directly in your DataHandling class? Your class name is DisplayPanel and displayPanel is the only object you've created in your program class, so i don't think if it has the scope in the DataHandling class too. Norm was right with his question towards you about scope. It's out of the scope and you can't call/use it.

  9. #9
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Accessing Variables Between Classes

    Okay, so I'm having trouble with the scope. Guess I'll be hitting the books again. Thanks for the help.

  10. #10
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Accessing Variables Between Classes

    Well, to add more into your information to read and understand stack traces
    Unresolved compilation problem:
    displayPanel cannot be resolved to a variable
    clearly mentions that compiler can not resolve displayPanel as a variable coz it doesn't even know it. So, there can be one of many solutions to your problem, use displayPanel as a static variable (though it's not recommended in this case but can still lead you to the solution.)

Similar Threads

  1. Instance Variables and local variables difference
    By dcwang3 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 31st, 2011, 06:33 AM
  2. [SOLVED] Trouble accessing variables from other classes
    By Elementality in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 5th, 2011, 11:50 AM
  3. Changing Array variables from different classes
    By smellyhole85 in forum Collections and Generics
    Replies: 6
    Last Post: December 9th, 2010, 03:18 PM
  4. using variables from other classes in arrays :/
    By mozyman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 30th, 2010, 11:26 PM
  5. State Variables interaction with outside classes?
    By Ace Coder in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 24th, 2010, 03:52 PM