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 4 of 4

Thread: Double cannot be dereferenced

  1. #1
    Junior Member
    Join Date
    Dec 2017
    Posts
    18
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default Double cannot be dereferenced

    Hey everyone,

    Im working on passing a double input to a method by the following code:

      //sales and sPerson Declaration
                 double sales;
                 sales = AnnualSales;
                 Salesperson sPerson = new Salesperson();
                 sPerson.setSales(sales);
     
            //Output Total Commission
            System.out.println("\nYour Total Commission for the year is: $" +
                    sPerson.getComm());
     
            //Output Total Compensation
            System.out.println("Your Total Compensation for the year is: $"+
                    String.format("%.2f", sPerson.getAnnComp())+ "+\n");
     
            //Output Table Headers
            System.out.println(" Total Sales           Total Compensation" + 
                               "\n ________________________________________");
     
            //UpperLimit Set: 150%
            int upperlimit = (int) ((AnnualSales/2)/5000);
     
            //Calculate Total Compensation in relevance with Total Sales
             for(int i=0;i<=upperlimit;i++){
              System.out.println(" $"+ sales+"       |         "
                      +"$"+String.format("%.2f", sales.getAnnComp()));   *****REFERENCE ERROR IS HERE*****
              sales+=5000;   
             }
       }

    The getAnnComp method code is here:

     public double getAnnComp ()
        {
        TotalAnnComp = Fix_Sal + CommEarned;
        return TotalAnnComp;
        }

    Any ideas? I'll continue working on a solution in the meantime. I can't just convert it to a string because the method used a double

    Thanks,
    - Deployment

  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Double cannot be dereferenced

    //sales and sPerson Declaration
        double sales;
     
    // ... and later
     
    System.out.println(" $"+ sales+"       |         "
        +"$"+String.format("%.2f", sales.getAnnComp()));   *****REFERENCE ERROR IS HERE*****

    You have declared sales as a double and later you attempt to call a getAnnComp() method on it. The compiler message ("double cannot be dereferenced") is telling you that you can't do this because doubles don't have any methods.

    Why did you abandon the other thread which was about how to call the getAnnComp() method? Both Norm and I asked questions in that thread, but you seem to have walked away from it.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    Deployment (January 9th, 2018)

  4. #3
    Junior Member
    Join Date
    Dec 2017
    Posts
    18
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default Re: Double cannot be dereferenced

    Quote Originally Posted by pbrockway2 View Post
    //sales and sPerson Declaration
        double sales;
     
    // ... and later
     
    System.out.println(" $"+ sales+"       |         "
        +"$"+String.format("%.2f", sales.getAnnComp()));   *****REFERENCE ERROR IS HERE*****

    You have declared sales as a double and later you attempt to call a getAnnComp() method on it. The compiler message ("double cannot be dereferenced") is telling you that you can't do this because doubles don't have any methods.

    Why did you abandon the other thread which was about how to call the getAnnComp() method? Both Norm and I asked questions in that thread, but you seem to have walked away from it.
    I was at a standstill here because I could not convert the double to a string as the method required a double. Therefore I wrote a new method that allowed use of a tostring conversion and got this to work

    As for the my other thread, I could have swore I responded. I apologize and will go respond again now

    Thank you,
    - Deployment

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Double cannot be dereferenced

    No problem. I'll post a link to the other thread: trouble-calling-method. And close this one.

Similar Threads

  1. int cannot be dereferenced
    By Wolverine89 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 9th, 2013, 09:30 AM
  2. [SOLVED] how to format double value?
    By arvindbis in forum What's Wrong With My Code?
    Replies: 13
    Last Post: March 10th, 2012, 01:49 PM
  3. [SOLVED] Read double from console without having to read a string and converting it to double.
    By Lord Voldemort in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 26th, 2011, 08:08 AM
  4. [SOLVED] Char cannot be dereferenced
    By zukipuu in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 17th, 2010, 11:13 PM
  5. Char cannot be dereferenced!! Please help
    By humdinger in forum Object Oriented Programming
    Replies: 5
    Last Post: February 14th, 2010, 03:18 PM