Results 1 to 3 of 3
  1. #1
    Sidmyre is offline Member
    Join Date
    Nov 2008
    Posts
    7
    Rep Power
    0

    Default cannot be referenced from a static

    Hey all(new), got a rather simple problem that ..I cant cant seem to fix.

    error:
    file: Ch09Ex05.java
    java:16: cannot find symbol
    symbol : variable carpet
    location: class Ch09Ex05
    System.out.println("Number of carpets sold: " + carpet.getSales());
    ^
    java:22: non-static method getSales() cannot be referenced from a static context
    System.out.println("Number of carpets sold: " + Carpet.getSales());
    ^
    java:27: non-static method getSales() cannot be referenced from a static context
    System.out.println("Number of carpets sold: " + Carpet.getSales());
    ^
    java:32: non-static method getSales() cannot be referenced from a static context
    System.out.println("Number of carpets sold: " + Carpet.getSales());
    ^



    its referencing Carpet.java which looks like this
    Java Code:
    import java.text.DecimalFormat;
    
    public class Carpet
    {
    
        private Rectangle carpet;
    	private String carpetName;
    	private double costSquareFt;
    	private Rectangle rectangle;
    	private static int sales = 0;
    	DecimalFormat currency = new DecimalFormat("$#,##0.00");
    
    public Carpet(String name, double cpsf, Rectangle rectangle)
    {
       this.carpetName = name;
       this.costSquareFt = cpsf;
       this.rectangle = new Rectangle (rectangle.getLength(), rectangle.getWidth());
       sales++;
    }
    
    public Carpet()
    {
    
    }
    
    public void setCarpetName(String name)
    {
    	this.carpetName = name;
    }
    
    public void setCostSquareFt(double cpsf)
    {
    	this.costSquareFt =  cpsf;
    }
    
    public String getCarpetName ()
    {
    	return this.carpetName;
    }
    
    public double getCostSquareFt()
    {
    	return this.costSquareFt;
    }
    
    public double getTotalCost()
    {
    	return carpet.getArea() * costSquareFt;
    }
    
       public int getSales()
       {
          return sales;
       }
    
    public String toString()
    {
    	return "CARPET: " + carpetName + ", " + currency.format(costSquareFt) + " per sq. ft." +
    		   "\nWIDTH: " + carpet.getWidth() + "LENGTH: " + carpet.getLength() + "AREA: " + carpet.getArea() +
    		   "\nTOTAL COST: " + getTotalCost() + "\n" + carpet.toString();
    
    
    }
    
    }
    any help is appreciated :)
    Last edited by Sidmyre; 11-24-2008 at 08:36 AM.

  2. #2
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default

    As the error message says, your method is non-static, and you are calling it in a static context (i.e. on the class and not on an instance). Try changing it to:

    public static int getSales()
    {
    return sales;
    }

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Suggesstions...

    • You have a constructor that's empty. It's not doing anything, unless your planing on populating it later.
      Java Code:
      public Carpet()
      {
      
      }
    • As far as I can tell, you haven't instantiated a Carpet class, which is why you're getting the errors on lines 22, 27 & 32.
    • Error on line 16 is because the class is misspelled... it should be Carpet.getSales().. notice the capital "C"


    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Replies: 3
    Last Post: 03-20-2009, 12:35 AM
  2. Replies: 11
    Last Post: 11-19-2008, 03:13 AM
  3. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  4. Replies: 1
    Last Post: 08-01-2007, 09:25 PM
  5. int cannot be referenced
    By elizabeth in forum New To Java
    Replies: 2
    Last Post: 07-26-2007, 04:07 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
  •