Results 1 to 9 of 9
  1. #1
    zerkz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default SOLVED - "Static" modifier needed?

    SOLVED



    Hello, I've recently started coding Java. I took a C/C+ class last semester. This is a program I made for my class, but just as I was finishing my "main" method, I came across this error.


    I'm using Eclipse as my IDE

    Cannot make a static reference to the non-static field invoice1 TestRental.java /Assignment 2/src line 24 Java Problem

    the program is suppose to print out invoices for Video Rental customers.

    I have 4 classes total. 3 of them are intertwined. The last one is the TestRental, which executes what i need (printInvoice.()).


    Java Code:
    /**
    	 * This is the customer class, which holds the name, the address (street,city, zip)
    	 * as well as the telephone number, in string format.
    	 * @author 
    	 *
    	 */
    public class Customer {
    	 private String custName;
    	 private String custStreet;
    	 private String custCity;
    	 private String custZip;
    	 private String custPhone;
    	 /**
    	  * 
    	  * @param customerName - Customer's Name, value assigned to custName.
    	  * @param customerStreet - Customer's Street, value assigned to custStreet.
    	  * @param customerCity - Customer's City, value assigned to custCity.
    	  * @param customerZip - Customer's zipcode, assigned to custZip.
    	  * @param customerPhoneNumber - Customer's phone number, assigned to custPhone;
    	  */
    	 public Customer(String customerName, String customerStreet, String customerCity, String customerZip, String customerPhoneNumber) {
    		 custName = customerName;
    		 custStreet = customerStreet;
    		 custCity = customerCity;
    		 custZip = customerZip;
    		 custPhone = customerPhoneNumber;
                     
    	 }
    	 /**
    	  * 
    	  * @return - gets custName.
    	  */
    	public String getCustName() {
    		return custName;
    	}
    	/**
             *
             * @param newValue - sets entered value for custName.
             */
    	public void setCustName(String newValue) {
                custName = newValue;
    	}
    	/**
             * 
             * @returns - gets custStreet.
             */
            public String getCustStreet() {
                return custStreet;
            }
            /**
             *
             * @param newValue - sets entered value for custStreet
             */
            public void setCustStreet(String newValue) {
                custStreet = newValue;
            }
            /**
             *
             * @return - gets custCity.
             */
            public String getCustCity() {
                return custCity;
            }
            /**
             *
             * @param newValue - sets entered value for custCity.
             */
            public void setCustCity(String newValue) {
                custCity = newValue;
            }
    	/**
             * 
             * @return - gets custZip.
             */
            public String getCustZip() {
                return custZip;
            }
            /**
             *
             * @param newValue - sets value for custZip.
             */
            public void setCustZip(String newValue) {
                custZip = newValue;
            }
            /**
             *
             * @return - gets custPhone.
             */
            public String getCustPhone() {
                return custPhone;
            }
            /**
             *
             * @param newValue - sets entered value for custPhone.
             */
            public void setCustPhone (String newValue) {
                custPhone = newValue;
            }
    }
    ---------------------------------------
    Java Code:
    /**
     * class Video - This is the class Video. it features the video name(string), video year(int), copy number(short), and daily rental rate(double).
     *
     */
    
    public class Video {
        private String vidName;
        private int vidYear;
        private short copyNum;
        private double dailyRentalRate;
        /**
         *        The constructor is the variable class is fairly simple and uses parameters.
         * @param videoName
         * @param videoYear
         * @param copyNumber
         * @param dRentalRate
         */
        public Video(String videoName, int videoYear, short copyNumber, double dRentalRate) {
            vidName = videoName;
            vidYear = videoYear;
            copyNum = copyNumber;
            dailyRentalRate = dRentalRate;
    
        }
         
        /**
         *
         * Below are the following getter and setter methods for the instance variables in the Video Class.
         * They are in pairs.
         */
        public String getVidName(){
            return vidName;
        }
    
        public int getVidYear(){
            return vidYear;
        }
    
        public short getCopyNum(){
            return copyNum;
        }
      
        public double getDailyRentalRate(){
        	return dailyRentalRate;
        }
        
        public void setDailyRentalRate(double newValue){
            dailyRentalRate = newValue;
        }
    }
    -------------------------------------

    Java Code:
    /**
     *  In order to receive today's date, we must use the Calendar and SimpleDateFormat classes included with Java. 
     */ 
    import java.util.Calendar;
    import java.text.SimpleDateFormat;
    /**
     * 
     * @author Zack Whipkey - The Invoice class has 4 instance variables. 
     * Class Invoice - This class uses 4 instance variables. invoiceCustomer (Customer), invoiceVoice(Video), daysRented (int) are all parameters.
     * Rental date is set to today's date.
     */
    public class Invoice {
    	private Customer invoiceCustomer; 
    	private Video invoiceVideo;
    	private String rentalDate;
    	private int daysRented;
    	
    	public Invoice (Customer invCustomer, Video invVideo,int rentedDays){
    		invoiceCustomer = invCustomer;
    		invoiceVideo = invVideo;
    		daysRented = rentedDays;
    		/**
    		 * The next two methods allow us to get the current date in the common format. Both of these classes come in the JDK. 
    		 */
    		Calendar today = Calendar.getInstance();
            SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
            /**
             * rentalDate gets the string value of the dateFormat's output (dd/MM/yyyy).
             */
            rentalDate = dateFormat.format(today.getTime());
    	    
    	}
    	
    
    	/**
    	 * 
    	 *  The following methods below are the getter and setter methods. The first two are the class setter/getters.
    	 */
    	
    	public double getSubTotal(){
    		return invoiceVideo.getDailyRentalRate() * daysRented;
    	}
    	
    	public double getTax(){
    		return getSubTotal() * 0.06;
    	}
    	
    	public double getTotal(){
    		return getTax() + getSubTotal();
    	}
    	
    	public void printInvoice(){
    		String custName = invoiceCustomer.getCustName();
    		String custAddress = invoiceCustomer.getCustStreet() + " " + invoiceCustomer.getCustCity() + " " + invoiceCustomer.getCustZip();
    		String custPhone = invoiceCustomer.getCustPhone();
    		
    		String vidName = invoiceVideo.getVidName();
    		int vidYear = invoiceVideo.getVidYear();
    		short copyNumber = invoiceVideo.getCopyNum();
    		double dailyRentalRate = invoiceVideo.getDailyRentalRate();
    		
    		System.out.printf("-Invoice-");
    		System.out.printf("Name : %s/n", custName);
    		System.out.printf("Address : %s/n", custAddress);
    		System.out.printf("Phone : %s/n", custPhone);
    		System.out.printf("/n");
    		System.out.printf("Video Name : %s/n", vidName);
    		System.out.printf("video Year : %s/n", vidYear);
    		System.out.printf("Video Copy Number : %u/n", copyNumber);
    		System.out.printf("Rental Rate : %d/n", dailyRentalRate);
    		System.out.printf("Days Rented : %s/n", daysRented);
    		System.out.printf("Rental Date : %s/n", rentalDate);
    		System.out.printf("/n");
    		System.out.printf("Subtotal: %d/n", getSubTotal());
    		System.out.printf("Tax : %d/n", getTax());
    		System.out.printf("Total Due: %s/n", getTotal());
    }
    	
    	
    	public Customer getCustomer(){
    		return invoiceCustomer;
    	}
    	
    	public void setCustomer (Customer newCustomer){
    		invoiceCustomer = newCustomer;
    	}
    	
    	public Video getVideo(){
    		return invoiceVideo;
    	}
    	
    	public void setVideo(Video newVideo){
    		invoiceVideo = newVideo;
    	}
    	
    	public int getDaysRented(){
    		return daysRented;
    	}
    	
    	public void setDaysRented(int newValue){
    		daysRented = newValue;
    	}
    	
    	public void setRentalDate(String newDate){
    		rentalDate = newDate;
    	}
    	
    }
    ---------------------------------------

    Java Code:
    /**
     * 
     * @ Importing the Calendar and DateFormat utilities. 
     *
     */
    import java.util.Calendar;
    import java.text.SimpleDateFormat;
    
    public class TestRental {
    	Customer customer1 = new Customer ("Dave Burns", "Burning Road", "Burnington", "15300","724333777");
    	Customer customer2 = new Customer ("Zack Usher", "Claylick Road", "Holbrook", "15500", "724222666");
    	Customer customer3 = new Customer ("Matt Whipkey", "Cream Road", "Nineveh", "16500", "724555444");
    	
    	Video video1 = new Video ("Superman 14", 2009, (short) 2, 1.00);
    	Video video2 = new Video ("Godzilla", 2005, (short) 1, 0.50);
    	Video video3 = new Video ("Banana Man", 2008, (short) 5, 1.00);
    	
    	Invoice invoice1 = new Invoice (customer1, video1, 5);
    	Invoice invoice2 = new Invoice (customer2, video2, 7);
    	Invoice invoice3 = new Invoice (customer3, video3, 3);
    	
    	public static void main(String [ ] args){
    
    	invoice1.printInvoice();
    	
     }
    }
    Any help would be GREATLY appreciated. I've been working for hours on this. Must I make EVERYTHING static? :(
    Last edited by zerkz; 09-23-2009 at 04:50 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by zerkz View Post
    Any help would be GREATLY appreciated. I've been working for hours on this. Must I make EVERYTHING static? :(
    No, that would be the worst way to try to solve this. The invoice1 variable is a field of the TestRental class and is visible in instances or objects of this class, but is not visible in the static main method -- to fully understand this you must read your notes/texts/tutorials on just what an object is.

    One possible solution is to create a TestRental object and then directly access the variables it containes -- also a poor solution. Another possible solution is to place all the code found in TestRental within its main method.
    Last edited by Fubarable; 09-23-2009 at 12:40 AM.

  3. #3
    zerkz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default

    I figured this out right before you posted XD.

    But now Eclipse cannot find the printstream.format (aka printf) in my libraries. :(

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by zerkz View Post
    I figured this out right before you posted XD.
    Great!
    But now Eclipse cannot find the printstream.format (aka printf) in my libraries. :(
    This has nothing to do with Eclipse. Please post the code throwing the error and the specific error message.

  5. #5
    zerkz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default

    -Invoice-Name : Dave Burns/nAddress : Burning Road Burnington 15300/nPhone : 724333777/n/nVideo Name : Superman 14/nException in thread "main" java.util.UnknownFormatConversionException: Conversion = 'u'
    at java.util.Formatter$FormatSpecifier.conversion(Unk nown Source)
    at java.util.Formatter$FormatSpecifier.<init>(Unknown Source)
    video Year : 2009/n at java.util.Formatter.parse(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.io.PrintStream.format(Unknown Source)
    at java.io.PrintStream.printf(Unknown Source)
    at Invoice.printInvoice(Invoice.java:69)
    at TestRental.main(TestRental.java:17)

    I put a %u thinking it would take it for short in printf... what should I put for a short variable if I am using it in printf.

  6. #6
    zerkz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default

    System.out.printf("Video Copy Number : %u /n", copyNumber);

    -Invoice-Name : Dave Burns/nAddress : Burning Road Burnington 15300/nPhone : 724333777/n/nVideo Name : Superman 14/nvideo Year : 2009/nException in thread "main" java.util.UnknownFormatConversionException: Conversion = 'u'
    at java.util.Formatter$FormatSpecifier.conversion(Unk nown Source)
    at java.util.Formatter$FormatSpecifier.<init>(Unknown Source)
    at java.util.Formatter.parse(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.io.PrintStream.format(Unknown Source)
    at java.io.PrintStream.printf(Unknown Source)
    at Invoice.printInvoice(Invoice.java:70)
    at TestRental.main(TestRental.java:17)

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The API will tell you what are the allowed formatter place-holders: Formatter (Java Platform SE 6)

    Also, I usually do my line separation with %n rather than \n when using printf.

  8. #8
    zerkz is offline Member
    Join Date
    Sep 2009
    Posts
    21
    Rep Power
    0

    Default

    Thanks, got the program running great now. I had a few %d where i needed %f.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Java, Military Format using "/" and "%" Operator!!
    By sk8rsam77 in forum New To Java
    Replies: 11
    Last Post: 02-26-2010, 04:03 AM
  2. getDisplayLanguage returns "en" not "English"
    By DD70 in forum New To Java
    Replies: 6
    Last Post: 08-13-2009, 12:22 AM
  3. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  4. Replies: 1
    Last Post: 10-20-2008, 08:35 AM
  5. "Jumble" or "Scramble" Program
    By Shadow22202 in forum Java Applets
    Replies: 8
    Last Post: 04-30-2008, 04:42 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
  •