Results 1 to 3 of 3
  1. #1
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default Problems accessing methods in an object.

    Once again I have a problem!!

    I was able to create an instance of a class (highlighted in red) in one action event and was able to access the variables speed and hours in other action events. I'm having a problem accessing the new object in my analyzeButtonActionPerformed section (underlined and highlighted in blue). Any reason why I can't here, but could elsewhere?



    Java Code:
    private void calcButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
            [COLOR="Red"]SpeedHours sh = new SpeedHours(speed, hours);[/COLOR]        
            this.resultField.setText("The distance is " + sh.getDistance() + " miles.");
    
        }                                          
    
        private void hourFieldActionPerformed(java.awt.event.ActionEvent evt) {                                          
            try {
    
                    hours = Double.parseDouble(this.hourField.getText());
                    if (hours < 1) {
                        JOptionPane.showMessageDialog(this, "Number must be greater"
                            + " than or equal to 1!", "ERROR", JOptionPane.ERROR_MESSAGE);
                    }
    
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Cannot be a letter!", "ERROR",
                        JOptionPane.ERROR_MESSAGE);
            }
        }                                         
    
        private void speedFieldActionPerformed(java.awt.event.ActionEvent evt) {                                           
            try {
    
                speed = Double.parseDouble(this.speedField.getText());
                if (speed < 0) {
                    JOptionPane.showMessageDialog(this, "Number must be greater"
                            + " than or equal to 0!", "ERROR", JOptionPane.ERROR_MESSAGE);
                }
    
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Cannot be a letter!", "ERROR",
                        JOptionPane.ERROR_MESSAGE);
            }
        }                                          
    
        private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
            speedField.setText("");
            hourField.setText("");
            resultField.setText("");
    
        }                                           
    
        private void analyzeButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
                this.jTextArea1.setText("Hour            Distance Travelled");
                this.jTextArea1.append(SEPARATOR);
                
                // Loop that calculates miles travelled per hour
                int count = 1;
                [U][COLOR="Blue"]while(count <= sh.getHours()){
                    double milesPerHour = sh.getSpeed() * count;[/COLOR][/U]                count++;
                }
        }                                             
    
        private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {
            System.exit(0);
        }
    
        public static void main(String args[]) {
            
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new SpeedHoursDemo().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify
        private javax.swing.JButton analyzeButton;
        private javax.swing.JButton calcButton;
        private javax.swing.JButton clearButton;
        private javax.swing.JButton exitButton;
        private javax.swing.JTextField hourField;
        private javax.swing.JLabel hourLabel;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanel2;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTextArea jTextArea1;
        private javax.swing.JTextField resultField;
        private javax.swing.JLabel resultLable;
        private javax.swing.JTextField speedField;
        private javax.swing.JLabel speedLabel;
        // End of variables declaration
        double speed;
        double hours;
        final String SEPARATOR = "...................................";
        final String SPACE = "                      ";
    }
    Last edited by Kevinius; 03-05-2011 at 07:18 AM.

  2. #2
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    I solved my problem...

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Kevinius View Post
    I solved my problem...

    obvious enough to say that the object variable is declared inside a method so the object reference is visible only locally. usually to access variables from different methods inside the class the variable is declared as a member of the object.

Similar Threads

  1. Accessing int's, String's by methods.
    By Cyloc in forum New To Java
    Replies: 2
    Last Post: 12-14-2010, 02:25 AM
  2. Replies: 5
    Last Post: 10-11-2010, 02:16 AM
  3. Accessing GUI object methods
    By km0r3 in forum AWT / Swing
    Replies: 5
    Last Post: 10-06-2010, 02:39 AM
  4. Error Accessing Methods in My JAR file
    By avu in forum Advanced Java
    Replies: 4
    Last Post: 03-23-2010, 01:35 AM
  5. accessing instance variables from static methods
    By ravian in forum New To Java
    Replies: 7
    Last Post: 03-01-2009, 11:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •