Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By Norm

Thread: I'm getting an error I've not encountered before

  1. #1
    Big-D is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default I'm getting an error I've not encountered before

    I'm writing a program for class and I have to make a main class, and at least one other that has an interface with that class(Using BlueJ). I have my main class which is Car:
    Java Code:
    public class Car
    {
        private int Year;
        private String make;
        private int speed;
        private String Type;
        private String Name;
        private String Driver;
    
    
        public Car (int Year, String carMake, int topSpeed, String carType, String carName, String carDriver)
        {
            Year = Year;
            make = carMake;//brand(ford, chevrolet, etc.)
            speed = topSpeed;//top speed
            Type = carType;//sports car, luxury, exoitic, etc.
            Name = carName;//Mustang, cammaro, etc.
            Driver = carDriver;//Teenager, Adult, etc.
        }
    
        public void setYear(int Year)
        {
            Year = Year;
        }
        
        public void setMake (String carMake)
        {
            make = carMake;
        }
        
        public void setSpeed(int carSpeed)
        {
            speed = carSpeed;
        } 
        
        public void setType(String carType)
        {
            Type = carType;
        }
        
        public void setName(String carName)
        {
            Name = carName;
        }
        
        public void setDriver(String carDriver)
        {
            Driver = carDriver;
        }
        
        public String getDriver(String carDriver)
        {
            return Driver;
        }
        
        public String getName(String carName)
        {
            return Name;
        }
        
        public String getType(String carType)
        {
            return Type;
        }
        
        public int getYear(int Year)
        {
            return Year;
        }
        
        public String getMake ()
        {
            return make;
        }
        
        public int getSpeed ()
        {
            return speed;
        }
        
        /*public void AccelerateSpeed (int speed)
        {
            speed = speed + 5;
        
        }
        public void BrakeSpeed (int speed)
        {
            speed = speed - 5;
        }*/
    
        
    }
    
    
    
    
    and then I have my first class that will be using the car class, DailyDriver, this is where I'm encountering the error-int cannot be dereferenced, heres the code: 
    
    public class DailyDriver
    {
       private Car[] drivers;
       private int milesPerDay;
       
       public DailyDriver(Car[] drivers)
       {
           this.drivers = drivers;
           this.milesPerDay = initmilesPerDay();
        }
        
        public Car[] getDrivers()
        {
            return drivers;
        }
        
        public int getmilesPerDay()
        {
            return milesPerDay;
        }
        
        public int initmilesPerDay()
        {
            double sum = 0.0;
            for(int i = 0; i < milesPerDay.length; i++)
            {
                sum = sum + milesPerDAy[i].getValue();
            }
            return sum;
        }
            
    }
    Last edited by Norm; 03-16-2012 at 12:49 AM. Reason: added code tags

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default Re: I'm getting an error I've not encountered before

    When posting code, please use code tags, use standard naming conventions (variables and methods are lower case, classes are upper case) and narrow your problem down to an SSCCE that's just a few lines long. Make it easy for people to help you.

    I am a bit confused as to what milesPerDay (or milesPerDAy, those are two different names that I see) is supposed to be. You declare it as an int, but then you attempt to use it as an array, but then you try calling a getValue() method on whatever would be in that array, which doesn't make a whole lot of sense.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Big-D is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: I'm getting an error I've not encountered before

    its supposed to be milesPerDay, milesPerDAy was a mistake I made while typing, but this is supposed to be how many miles a day the driver drives, and depending on the type of drivers, the program should return a set integer called milesPerDay

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: I'm getting an error I've not encountered before

    Please post the full text of the error messages.

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default Re: I'm getting an error I've not encountered before

    Quote Originally Posted by Big-D View Post
    its supposed to be milesPerDay, milesPerDAy was a mistake I made while typing, but this is supposed to be how many miles a day the driver drives, and depending on the type of drivers, the program should return a set integer called milesPerDay
    Even working with milesPerDay, I'm still confused. You declare it as an int, but then attempt to use it as an array, and then call a method getValue() that I don't see declared anywhere.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Big-D is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: I'm getting an error I've not encountered before

    Ok, honestly, I missed the lesson where we learned that stuff and never got caught up on it becasue no one could explain it real well to me. Pretty much, our teacher gave us an example of a class like we're supposed to be doing called Money. It has the coin, change and manager classes in it. In my project the DailyDriver class of mine, which milesPerDay is in, is supposed to be like his change class which looks like this
    Java Code:
    public class Change
    {
       private Coin[] coins;
       private double amount;
    
       public Change(Coin[] coins)
       {
          this.coins = coins;
          this.amount = initAmount();
       }
       
       public Coin[] getCoins()
       {
          return coins;
       }
       
       public double getAmount()
       {
           return amount;
       }
       
       public double initAmount()
       {
          double sum = 0.0;
          for(int i = 0; i < coins.length; i++)
          {
              sum = sum + coins[i].getValue();
          }
          return sum;
       }
       
       
    
    }

  7. #7
    Big-D is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: I'm getting an error I've not encountered before

    and for Norm the full error message is "int cannot be dereferenced". Here is a screenshot of it I'm getting an error I've not encountered before-untitled.jpg

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: I'm getting an error I've not encountered before

    You can not call methods or reference varaibles on a primitive variable. What is the definition of the variable in the error message?
    You have coded it as if the variable is an array. arrays have a length, ints do not.

  9. #9
    Big-D is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: I'm getting an error I've not encountered before

    [QUOTE]Ok, honestly, I missed the lesson where we learned that stuff and never got caught up on it becasue no one could explain it real well to me. Pretty much, our teacher gave us an example of a class like we're supposed to be doing called Money. It has the coin, change and manager classes in it. In my project the DailyDriver class of mine, which milesPerDay is in, is supposed to be like his change class which looks like this
    Java Code:
    public class DailyDriver
    {
       private Car[] drivers;
       private int milesPerDay;
       
       public DailyDriver(Car[] drivers)
       {
           this.drivers = drivers;
           this.milesPerDay = initmilesPerDay();
        }
        
        public Car[] getDrivers()
        {
            return drivers;
        }
        
        public int getmilesPerDay()
        {
            return milesPerDay;
        }
        
        public int initmilesPerDay()
        {
            double sum = 0.0;
            for(int i = 0; i < milesPerDay.length; i++)
            {
                sum = sum + milesPerDay[i].getValue();
            }
            return sum;
        }
            
    }

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: I'm getting an error I've not encountered before

    Does that code compile without any errors? If not, you should post the full text of the error messages.

    If you get the same errors, you need to change the code so it does not get the error.
    What is the loop at line 25 supposed to do? Where is the array that you want loop through?

  11. #11
    Big-D is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: I'm getting an error I've not encountered before

    That code compiles with the error "int cannot be dereferenced" at line 25.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: I'm getting an error I've not encountered before

    If you get the same errors, you need to change the code so it does not get the error.
    What is the loop at line 25 supposed to do? Where is the array that you want loop through?

  13. #13
    Big-D is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: I'm getting an error I've not encountered before

    thats a wrong array like you and Kevin Workman have both said I declared it as an int and tried to use it in an array, basically I'm asking what I need to do to fix this. I've been drawing blanks recently when I've been programing

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: I'm getting an error I've not encountered before

    what I need to do to fix this.
    You need to go back and design what the code in that class and method is supposed to do.
    What data is to be stored in the class?
    What will each method do? Where will the method get the data it needs to do its job?

    Until you decide those issues, you can not write any code.
    KevinWorkman likes this.

Similar Threads

  1. Replies: 0
    Last Post: 06-10-2011, 07:48 PM
  2. Replies: 1
    Last Post: 03-19-2009, 07:38 AM
  3. error encountered when writing image file
    By angelinehelena in forum Advanced Java
    Replies: 1
    Last Post: 02-10-2009, 10:08 AM
  4. The server encountered an internal error ()
    By rakesh_n_mehta in forum Web Frameworks
    Replies: 0
    Last Post: 01-19-2009, 01:09 PM
  5. Description the server encountered an internal error
    By Jack in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 07-02-2007, 02:24 AM

Posting Permissions

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