Results 1 to 18 of 18
  1. #1
    jhering1986 is offline Member
    Join Date
    Mar 2009
    Posts
    8
    Rep Power
    0

    Default No main class found error

    Alright, so I know I need to have the main class defined, but I'm not sure where to put it, here is my code:
    Java Code:
    package item;
    //***************************************************************
    //Item.java
    //
    //Represents an item in a shopping cart.
    //***************************************************************
    import java.text.NumberFormat;
    public class Item{
    	private String name;
    	private double price;
    	private int quantity;
    	// -------------------------------------------------------
    	//  Create a new item with the given attributes.
    	// -------------------------------------------------------
    	public Item (String itemName, double itemPrice, int numPurchased)
    	{
    		name = itemName;
    		price = itemPrice;
    		quantity = numPurchased;
    	}
    	// -------------------------------------------------------
    	//   Return a string with the information about the item
    	// -------------------------------------------------------
    	public String toString ()
    	{
    		NumberFormat fmt = NumberFormat.getCurrencyInstance();
    		 return (name + "\t" + fmt.format(price) + "\t" + quantity + "\t"
    		 		+ fmt.format(price*quantity));
    	}
    	// -------------------------------------------------
    	//   Returns the unit price of the item
    	// -------------------------------------------------
    	public double getPrice()
    	{
    		return price;
    	}
    	// -------------------------------------------------
    	//   Returns the name of the item
    	// -------------------------------------------------
    	public String getName()
    	{
    		return name;
    	}
    	// -------------------------------------------------
    	//   Returns the quantity of the item
    	// -------------------------------------------------
    	public int getQuantity()
    	{
    		return quantity;
    	}
    }

  2. #2
    Xyle's Avatar
    Xyle is offline Member
    Join Date
    Feb 2009
    Location
    Nevada
    Posts
    34
    Rep Power
    0

    Default

    Hmmm, interesting, I used JCreator and it built successfully without any errors. What IDE are you using to build the class file?

  3. #3
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default This is a data class. You don't put the main in data classes.

    This class is designed to hold data for an item only.
    It should be used with other classes to create a
    larger program.

    Among all those classes, there will be a class that's
    referred to as, or possibly named "App.class".
    In that class you should find the main.

    You would launch the project with the command line:

    java App

  4. #4
    Xyle's Avatar
    Xyle is offline Member
    Join Date
    Feb 2009
    Location
    Nevada
    Posts
    34
    Rep Power
    0

    Default

    I just dont understand where he's getting the error from. Unless, he's trying to launch this class as an app. It builds fine, so it should provide a class that he can use in his main class file.

    jhering1986, what are you doing when you recieve the error? compiling? or Launching something? If so, what and where are you recieveing the error?

  5. #5
    softslate is offline Member
    Join Date
    Mar 2009
    Location
    Schenectady, NY
    Posts
    11
    Rep Power
    0

    Default

    I think paul pasciak is correct in that you are probably attempting to run the class as a Java application, where it looks like it is simply an object that would be used in the context of another class.

    If you're looking to start programming a shopping cart application in Java, I encourage you to check out my company's site, SoftSlate.com. There is a code sample here that could help get you started, plus a Free Edition of the entire application that you might find serves your purposes. Finally, if it's source code you need, you can purchase a license with source code for a reasonable fee.

    I hope that helps!
    SoftSlate Commerce Java Shopping Cart
    www.softslate.com

  6. #6
    jhering1986 is offline Member
    Join Date
    Mar 2009
    Posts
    8
    Rep Power
    0

    Default

    I'm using netbeans 6.5 and that code that you see there is the code that our teacher gave us. I get the error when I try to run the program and when I try to compile it. It doesn't make any sense to me why it won't work.

  7. #7
    softslate is offline Member
    Join Date
    Mar 2009
    Location
    Schenectady, NY
    Posts
    11
    Rep Power
    0

    Default

    In order to run a Java application the class you're calling has to have a main method, like this:
    Java Code:
    public static void main(String[] args) {
    	System.out.println("Hello world!");
    }
    When the program is fired off, it executes the main method and everything else gets executed from there. This Item class doesn't have a main method which is why you get that error. It can only be used indirectly by some other class that has a main method. Your teacher must have wanted you to do something else with the class, I suspect.

    Does that make any sense?
    SoftSlate Commerce Java Shopping Cart
    www.softslate.com

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

    Default

    Another possiblity is that the OP is trying to run the above code as a standlone:
    java Item
    ... which won't happen, because it doesn't have a main method and probably doesn't neet one.
    The methods in this class need to be called from another class like for example:

    Java Code:
    Item item = new Item(toothpick, 1.05, 2); //instantiate the class
    int myQuantity = item.getQuantity(); //call the getQuantity method
    Luck,
    CJSL
    Last edited by CJSLMAN; 03-26-2009 at 03:05 PM. Reason: typo...
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    softslate is offline Member
    Join Date
    Mar 2009
    Location
    Schenectady, NY
    Posts
    11
    Rep Power
    0

    Default

    I think you're probably right. So, you could put the above code inside another classs's main method and run the other class.
    SoftSlate Commerce Java Shopping Cart
    www.softslate.com

  10. #10
    jhering1986 is offline Member
    Join Date
    Mar 2009
    Posts
    8
    Rep Power
    0

    Default

    I did try to add the public static line, but every time I put it in, it kept giving me a illegal start of expression error. Here is the other code that goes with it:
    Java Code:
    package item;
    //**********************************************************************
    //ShoppingCart.java
    //
    //Represents a shopping cart as an array of items
    //**********************************************************************
    
    import java.text.NumberFormat;
    
    public class ShoppingCart
    {
    	private int itemCount;     // total number of items in the cart
    	private double totalPrice; // total price of items in the cart
    	private int capacity;		// current cart capacity
    	private Item[] cart;       // the actual array of items to store things in the cart
    	// -----------------------------------------------------------
    	//  Creates an empty shopping cart with a capacity of 3 items.
    	// -----------------------------------------------------------
    	public ShoppingCart()
    	{
    		capacity = 3;
    		itemCount = 0;
    		totalPrice = 0.0;
    		cart = new Item[capacity];
    	}
    // -------------------------------------------------------
    //  Adds an item to the shopping cart.
    // -------------------------------------------------------
    	public void addToCart(String itemName, double price, int quantity)
    	{
    		cart[itemCount++] = new Item(itemName, price, quantity);
    		totalPrice += price * quantity;
    		if(itemCount == capacity) // if full, increase the size of the cart
    			increaseSize();
    	}
    // -------------------------------------------------------
    //  Returns the contents of the cart together with
    //  summary information.
    // -------------------------------------------------------
    	public String toString() // this method is called when an object needs to be "printed"
    							 // (when System.out.println() is called, this overriden method is called)
    	{
    		NumberFormat fmt = NumberFormat.getCurrencyInstance();
    		String contents = "\nShopping Cart\n";
    		contents += "\nItem\tPrice\tQty\tTotal\n";
    		for (int i = 0; i < itemCount; i++)
    			contents += cart[i].toString() + "\n";
    			contents += "\nTotal Price: " + fmt.format(totalPrice);
    			contents += "\n";
    			return contents;
    	}
    // ------------------------------------------------------------
    //  Increases the capacity of the shopping cart by doubling it.
    // ------------------------------------------------------------
    	private void increaseSize()
    	{
    		Item[] tempItem = new Item[capacity];
    		capacity *= 2; // double the size
    
    		for (int i=0; i< itemCount; i++)
    		{
    			tempItem[i] = cart[i];
    		}
    		cart = new Item[capacity];
    		for (int i=0; i< itemCount; i++)
    		{
    			cart[i] = tempItem[i];
    		}
    
    		// The above can also be accomplished by using:
    		// System.arraycopy(...)
    	}
    	/**
    	 * @return Returns the totalPrice.
    	 */
    	public double getTotalPrice() {
    		return totalPrice;
    	}
    }

  11. #11
    softslate is offline Member
    Join Date
    Mar 2009
    Location
    Schenectady, NY
    Posts
    11
    Rep Power
    0

    Default

    Well, you may have figure some of this out by yourself. :) I don't think you'd want us to do the exercise for you completely (well at least I don't think you're teacher would). :)

    If you add this main method to ShoppingCart and run it what happens?
    Java Code:
    public static void main(String[] args) {
    	System.out.println("Hello from main method!");
    }
    If you see the message spit out, you're on your way. Add more code to the main method to get it to do different stuff like add an item to the cart, etc.
    SoftSlate Commerce Java Shopping Cart
    www.softslate.com

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

    Default

    I repeat... these classes (...and the second class you posted, shoppingCart, is different than the first class you posted, item) are not meant to have a main class. Their methods are meant to be called from other classes. You can't run them (as is) as follows:
    java item
    or
    java shoppingCart
    Like I said, as is, they can't be run as stand alone programs. Their methods have to be called from other classes (which one of them has a main method). In my last post I gave an example how to call one of the method of the item class.

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

  13. #13
    Xyle's Avatar
    Xyle is offline Member
    Join Date
    Feb 2009
    Location
    Nevada
    Posts
    34
    Rep Power
    0

    Default

    Hehehehe, 4 people saying the same thing.

  14. #14
    jhering1986 is offline Member
    Join Date
    Mar 2009
    Posts
    8
    Rep Power
    0

    Default

    Ok, I figured it out, I didn't read the rest of my assignment and had to write a program. So I did that, but now i get a NullPointerException error on this line:

    System.out.println(totalPrice += item.getQuantity() * item.getPrice());

    here is the new code:
    Java Code:
    package item;
    //***************************************************************
    //Item.java
    //
    //Represents an item in a shopping cart.
    //***************************************************************
    import java.text.NumberFormat;
    import java.util.Scanner;
    import java.util.ArrayList;
    public class Item{
    	private String name;
    	private double price;
    	private int quantity;
    	// -------------------------------------------------------
    	//  Create a new item with the given attributes.
    	// -------------------------------------------------------
    	public Item (String itemName, double itemPrice, int numPurchased)
    	{
    		name = itemName;
    		price = itemPrice;
    		quantity = numPurchased;
    	}
    	// -------------------------------------------------------
    	//   Return a string with the information about the item
    	// -------------------------------------------------------
    	public String toString ()
    	{
    		NumberFormat fmt = NumberFormat.getCurrencyInstance();
    		 return (name + "\t" + fmt.format(price) + "\t" + quantity + "\t"
    		 		+ fmt.format(price*quantity));
    	}
    	// -------------------------------------------------
    	//   Returns the unit price of the item
    	// -------------------------------------------------
    	public double getPrice()
    	{
    		return price;
    	}
    	// -------------------------------------------------
    	//   Returns the name of the item
    	// -------------------------------------------------
    	public String getName()
    	{
    		return name;
    	}
    	// -------------------------------------------------
    	//   Returns the quantity of the item
    	// -------------------------------------------------
    	public int getQuantity()
    	{
    		return quantity;
    	}
        public static void main(String[] args) {
    Item item = null;
    String itemName;
    double itemPrice;
    int quantity;
    double totalPrice;
    
            System.out.println("Welcome to Shopper's Paradise");
    System.out.println();
    
    Scanner scan = new Scanner(System.in);
    
    String keepShopping = "y";
    
    do {
    System.out.print("Enter the name of the item: ");
    itemName = scan.nextLine();
    
    System.out.print("Enter the unit price: ");
    itemPrice = scan.nextDouble();
    
    System.out.print("Enter the quantity: ");
    quantity = scan.nextInt();
    totalPrice = 0;
    System.out.println("\nCurrent Cart");
    
    // *** print the contents of the cart object
    
    // *** print the total price of the cart
    System.out.println(totalPrice += item.getQuantity() * item.getPrice());
    System.out.println();
    System.out.print("Continue shopping (y/n)? ");
    scan.nextLine();
    keepShopping = scan.nextLine();
    } while (keepShopping.equals("y"));
        }
    }

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

    Default

    So the error is telling you that you are trying to call methods on a null object, on a variable that has not yet been assigned a valid object. The only candidate variable in that line is the item variable which you declare as
    Java Code:
    Item item = null;
    So where do you construct an item object?

  16. #16
    jhering1986 is offline Member
    Join Date
    Mar 2009
    Posts
    8
    Rep Power
    0

    Default

    in an array?

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

    Default

    Please look at my post (#8) at the example I gave as how instantiate a class and therefore use it's methods. I would strongly suggest that you study the following which will teach about classes, methods, constructors and how to use them:
    Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)

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

  18. #18
    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 jhering1986 View Post
    in an array?
    This doesn't make sense. Time to hit the books my friend.

Similar Threads

  1. Replies: 2
    Last Post: 09-22-2011, 10:48 PM
  2. Error: no class definition found
    By toby in forum New To Java
    Replies: 6
    Last Post: 08-28-2011, 11:32 PM
  3. JRE rtapplet class not found error
    By avinash.natekar in forum Java Applets
    Replies: 11
    Last Post: 04-02-2009, 09:02 AM
  4. Replies: 4
    Last Post: 01-01-2009, 04:58 AM
  5. No Class Def Found Error:
    By wrap23 in forum New To Java
    Replies: 9
    Last Post: 10-02-2008, 05:07 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
  •