Computer Science Canada Accessing a variable from one class to another |
Author: | Junaid2pac [ Sun Dec 30, 2007 8:15 pm ] |
Post subject: | Accessing a variable from one class to another |
//1st class: class GameFrame extends JFrame public JTextField getVelocity; //2nd class: GameFrame vx; int getDx = Integer.parseInt(vx.getVelocity.getText().trim()); Question: Would this be the correct way of accessing one variable from one class to another? |
Author: | wtd [ Mon Dec 31, 2007 12:32 am ] | ||||||||
Post subject: | RE:Accessing a variable from one class to another | ||||||||
Ideally, no. The variable should be encapsulated, so that accessing it requires a getVelocity method, and setting a new value requires a setVelocity method. I would also suggest a naming along the lines of:
Or better:
|
Author: | Tony [ Mon Dec 31, 2007 12:57 am ] | ||
Post subject: | RE:Accessing a variable from one class to another | ||
indeed it is was wtd has said.
is way too misleading. One would expect getVelocity to be a method and return the Velocity. It is certainly not obvious that it actually holds a textfield that could hold an arbitrary value. |
Author: | Junaid2pac [ Mon Dec 31, 2007 4:20 pm ] |
Post subject: | Re: Accessing a variable from one class to another |
thanks |
Author: | Junaid2pac [ Mon Dec 31, 2007 4:30 pm ] |
Post subject: | Re: Accessing a variable from one class to another |
but it still gives me the following error: Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Ball.<init>(Junaid_Java_ISU.java:124) ---------------------------------------------------------------------------------------- which is at: int getDx = vx.getVelocity(); |
Author: | Junaid2pac [ Mon Dec 31, 2007 5:22 pm ] | ||
Post subject: | Re: Accessing a variable from one class to another | ||
also: [/code] //First Class class GameFrame extends JFrame { public GameFrame() { velocityText = new JTextField(2); velocityText.setText("2"); public int getVelocity() { return Integer.parseInt(velocityText.getText().trim()); } addButton(p, "Start", new ActionListener() { public void actionPerformed(ActionEvent e) { Ball b = new Ball(canvas); b.start(); } }); } public void addButton(Container c, String title, ActionListener a) { JButton b = new JButton(title); c.add(b); b.addActionListener(a); } } //2nd Class class Ball extends Thread { GameFrame vx; int getDx = vx.getVelocity(); //This is where the error points to }
|
Author: | Junaid2pac [ Mon Dec 31, 2007 5:22 pm ] |
Post subject: | Re: Accessing a variable from one class to another |
sorry i didnt know how to use the code thing |
Author: | HeavenAgain [ Mon Dec 31, 2007 5:27 pm ] | ||
Post subject: | RE:Accessing a variable from one class to another | ||
that is because in your 2nd class the vx object of GameFrame does not point to anything to fix this try this
|
Author: | Junaid2pac [ Mon Dec 31, 2007 5:31 pm ] |
Post subject: | Re: Accessing a variable from one class to another |
Thanx....finally its working |
Author: | Junaid2pac [ Mon Dec 31, 2007 5:43 pm ] |
Post subject: | Re: Accessing a variable from one class to another |
it works but only once When i change the value in the textfield it keeps the velocity that was set in the text field at in the beginning. How can i change my code so it gets the value each time i click the start button |
Author: | HeavenAgain [ Mon Dec 31, 2007 5:48 pm ] | ||
Post subject: | RE:Accessing a variable from one class to another | ||
you have a very mess code there, and its really hard to read off code which is not organized, but from the look of the code, your value comes from the constructor of your GameFrame class (you are using it to prompt user for a value useing JTextField (which is wrong) constructor should only "over write" the default ones, by giving the new default value. Now to fix your problem, you can try something like this in your 2nd class
|
Author: | Junaid2pac [ Wed Jan 02, 2008 7:50 pm ] | ||
Post subject: | Re: Accessing a variable from one class to another | ||
i am still sort of confused as to why the velcoity of the ball only works for the initial value assigned to the text field. i used this:
in the first class called GameFrame After that changing the value inside the text field box dosnt acutally change the velocity, it stays the same Pls help |
Author: | Junaid2pac [ Wed Jan 02, 2008 7:51 pm ] | ||
Post subject: | Re: Accessing a variable from one class to another | ||
I also have this in the first class to constantly checking the text field
|
Author: | Junaid2pac [ Wed Jan 02, 2008 10:33 pm ] | ||
Post subject: | Re: Accessing a variable from one class to another | ||
I just realized i don't have an addActionListener but when i add this to the GameFrame Class:
it gives me the following error: addActionListener(java.awt.event.ActionListener) in javax.swing.JTextField cannot be applied to (GameFrame) |
Author: | Junaid2pac [ Wed Jan 02, 2008 10:54 pm ] |
Post subject: | Re: Accessing a variable from one class to another |
i believe i have figured out my problem, but in the action performed method...how do you show which button u r talking about when there is more than 1? |
Author: | TheFerret [ Thu Jan 03, 2008 12:57 am ] |
Post subject: | RE:Accessing a variable from one class to another |
Use the name of the button to do it... ie get an eventsource and compare it to name in an if statement... I do have examples but not on this computer... |
Author: | wtd [ Thu Jan 03, 2008 1:23 am ] |
Post subject: | RE:Accessing a variable from one class to another |
Do you understand what an interface is? |