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

Thread: Getting errors

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Getting errors

    Hi there, I'm new to this site and java programming, and this is my first given assignment. Define a class named Asset to store the details of an asset in the company. Each object of Asset should store the information. The Asset class should include a no-argument constructor, a constructor with parameters, appropriate setter and getter methods. Write a driver program that tests the various operations of the class. I get 6 errors of non-static method cannot be referenced from a static context.

    import java.util.Scanner;
    public class Asset{
    	private String assetID;
    	private String description;
    	private String date;
    	private int orderNum;
    	private double cost;
    	private String status;
     
    	public Asset(){
    	}
     
    	public Asset(String assetID, String description, String date, int orderNum, double cost, String status){
    		this.assetID = assetID;
    		this.description = description;
    		this.date = date;
    		this.orderNum = orderNum;
    		this.cost = cost;
    		this.status = status;
    	}
     
    	public void setAssetID(String assetID){
    		this.assetID = assetID;
    	}
     
    	public void setDescription(String description){
    		this.description = description;
    	}
     
    	public void setDate(String date){
    		this.date = date;
    	}
     
    	public void setOrderNum(int orderNum){
    		this.orderNum = orderNum;
    	}
     
    	public void setCost(double cost){
    		this.cost = cost;
    	}
     
    	public void setStatus(String status){
    		this.status = status;
    	}
     
    	public String getAssetID(){
    		return assetID;
    	}
     
    	public String getDescription(){
    		return description;
    	}
     
    	public String getDate(){
    		return date;
    	}
     
    	public int getOrderNum(){
    		return orderNum;
    	}
     
    	public double getCost(){
    		return cost;
    	}
     
    	public String getStatus(){
    		return status;
    	}
    }
     
    class TestAsset{
    	public static void main(String [] args){
    		Scanner s = new Scanner(System.in);
     
    		String assetID;
    		String description;
    		String date;
    		int orderNum;
    		double cost;
    		String status;
     
    		System.out.print("Enter asset ID: ");
    		assetID = s.next();
     
    		System.out.print("Enter the description: ");
    		description = s.next();
     
    		System.out.print("Enter the date: ");
    		date = s.next();
     
    		System.out.print("Enter the order number: ");
    		orderNum = s.nextInt();
     
    		System.out.print("Enter the cost: ");
    		cost = s.nextDouble();
     
    		System.out.print("Enter the status: ");
    		status = s.next();
     
    		System.out.print("Asset ID: " + Asset.getAssetID());
    		System.out.print("Description: " + Asset.getDescription());
    		System.out.print("Date: " + Asset.getDate());
    		System.out.print("Order number: " + Asset.getOrderNum());
    		System.out.print("Cost: " + Asset.getCost());
    		System.out.print("Status: " + Asset.getStatus());
     
    	}
    }

    Any guideline will be appreciated.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Getting errors

    non-static method cannot be referenced from a static context.
    You need to create an object to be able to reference a class's non-static methods. The non-static members of a class do NOT exist until an object is created. static members do exist without an object.
    Classname objRef = new Classname(); // create object
    objRef.method(); // use non-static method

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

    Nonire (July 4th, 2010)

  4. #3
    Junior Member
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Getting errors

    Thanks Norm, now I'm able to run the program, but now I get the error Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)
    at TestAsset.main(Asset.java:93)
    in the output.

    import java.util.Scanner;
    public class Asset{
    	private String assetID;
    	private String description;
    	private String date;
    	private int orderNum;
    	private double cost;
    	private String status;
     
    	public Asset(){
    	}
     
    	public Asset(String assetID, String description, String date, int orderNum, double cost, String status){
    		this.assetID = assetID;
    		this.description = description;
    		this.date = date;
    		this.orderNum = orderNum;
    		this.cost = cost;
    		this.status = status;
    	}
     
    	public void setAssetID(String assetID){
    		this.assetID = assetID;
    	}
     
    	public void setDescription(String description){
    		this.description = description;
    	}
     
    	public void setDate(String date){
    		this.date = date;
    	}
     
    	public void setOrderNum(int orderNum){
    		this.orderNum = orderNum;
    	}
     
    	public void setCost(double cost){
    		this.cost = cost;
    	}
     
    	public void setStatus(String status){
    		this.status = status;
    	}
     
    	public String getAssetID(){
    		return assetID;
    	}
     
    	public String getDescription(){
    		return description;
    	}
     
    	public String getDate(){
    		return date;
    	}
     
    	public int getOrderNum(){
    		return orderNum;
    	}
     
    	public double getCost(){
    		return cost;
    	}
     
    	public String getStatus(){
    		return status;
    	}
    }
     
    class TestAsset{
    	public static void main(String [] args){
    		Scanner s = new Scanner(System.in);
    		Asset a = new Asset();
     
    		String assetID;
    		String description;
    		String date;
    		int orderNum;
    		double cost;
    		String status;
     
    		System.out.print("Enter asset ID: ");
    		assetID = s.next();
     
    		System.out.print("Enter the description: ");
    		description = s.next();
     
    		System.out.print("Enter the date: ");
    		date = s.next();
     
    		System.out.print("Enter the order number: ");
    		orderNum = s.nextInt();
     
    		System.out.print("Enter the cost: ");
    		cost = s.nextDouble();
     
    		System.out.print("Enter the status: ");
    		status = s.next();
     
    		System.out.print("Asset ID: " + a.getAssetID());
    		System.out.print("Description: " + a.getDescription());
    		System.out.print("Date: " + a.getDate());
    		System.out.print("Order number: " + a.getOrderNum());
    		System.out.print("Cost: " + a.getCost());
    		System.out.print("Status: " + a.getStatus());
     
    	}
    }

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Getting errors

    java.util.InputMismatchException
    What did you type in for the statement at line 93 to read? Was it an integer or something else?
    The Scanner nextInt() method requires you enter an integer.

  6. #5
    Junior Member
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Getting errors

    I get that error after I type in the input for line 87, couldn't get to the next enter input.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Getting errors

    at java.util.Scanner.nextInt(Scanner.java:2050)
    at TestAsset.main(Asset.java:93)
    The error message says the error occurred in line 93 where there is a call to nextInt()
    I have no idea where line 87 or line 93 are in your program.

    Can you open a command prompt, execute the program and copy and paste the full contents of the screen here?

  8. #7
    Junior Member
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Getting errors

    Sorry for taking too long, is this fine?
    Attached Images Attached Images

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Getting errors

    You need to read what the Scanner next() method does.
    The effect on your program was that your answer to the question: Enter the description:
    supplied answers for the next few questions.
    Did you notice that the program did not wait for you to enter data before it asked the next question?

Similar Threads

  1. Why am I getting 62 errors?
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 1st, 2010, 04:41 AM
  2. not spelling errors
    By britishgoose01 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2010, 08:59 PM
  3. Exception Errors
    By TIMBERings in forum Exceptions
    Replies: 1
    Last Post: December 10th, 2009, 02:13 AM
  4. Errors with LispList
    By Newoor in forum Collections and Generics
    Replies: 10
    Last Post: October 25th, 2009, 04:25 PM
  5. [SOLVED] Error of "cannot find symbol"
    By big_c in forum File I/O & Other I/O Streams
    Replies: 31
    Last Post: April 9th, 2009, 11:20 AM