Results 1 to 18 of 18
Thread: No main class found error
- 03-25-2009, 12:33 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 8
- Rep Power
- 0
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; } }
- 03-25-2009, 02:26 AM #2
Hmmm, interesting, I used JCreator and it built successfully without any errors. What IDE are you using to build the class file?
- 03-25-2009, 04:12 AM #3
Senior Member
- Join Date
- Jul 2008
- Posts
- 125
- Rep Power
- 0
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
- 03-25-2009, 05:54 AM #4
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?
- 03-25-2009, 06:57 PM #5
Member
- Join Date
- Mar 2009
- Location
- Schenectady, NY
- Posts
- 11
- Rep Power
- 0
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
- 03-25-2009, 07:33 PM #6
Member
- Join Date
- Mar 2009
- Posts
- 8
- Rep Power
- 0
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.
- 03-25-2009, 08:10 PM #7
Member
- Join Date
- Mar 2009
- Location
- Schenectady, NY
- Posts
- 11
- Rep Power
- 0
In order to run a Java application the class you're calling has to have a main method, like this:
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.Java Code:public static void main(String[] args) { System.out.println("Hello world!"); }
Does that make any sense?SoftSlate Commerce Java Shopping Cart
www.softslate.com
- 03-26-2009, 01:37 PM #8
Another possiblity is that the OP is trying to run the above code as a standlone:
... which won't happen, because it doesn't have a main method and probably doesn't neet one.java Item
The methods in this class need to be called from another class like for example:
Luck,Java Code:Item item = new Item(toothpick, 1.05, 2); //instantiate the class int myQuantity = item.getQuantity(); //call the getQuantity method
CJSLLast edited by CJSLMAN; 03-26-2009 at 02:05 PM. Reason: typo...
Chris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 03-26-2009, 02:00 PM #9
Member
- Join Date
- Mar 2009
- Location
- Schenectady, NY
- Posts
- 11
- Rep Power
- 0
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
- 03-26-2009, 07:36 PM #10
Member
- Join Date
- Mar 2009
- Posts
- 8
- Rep Power
- 0
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; } }
- 03-26-2009, 10:31 PM #11
Member
- Join Date
- Mar 2009
- Location
- Schenectady, NY
- Posts
- 11
- Rep Power
- 0
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?
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.Java Code:public static void main(String[] args) { System.out.println("Hello from main method!"); }SoftSlate Commerce Java Shopping Cart
www.softslate.com
- 03-26-2009, 11:35 PM #12
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:
orjava item
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.java shoppingCart
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 03-27-2009, 03:30 AM #13
- 03-27-2009, 09:19 PM #14
Member
- Join Date
- Mar 2009
- Posts
- 8
- Rep Power
- 0
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")); } }
-
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
So where do you construct an item object?Java Code:Item item = null;
- 03-27-2009, 11:04 PM #16
Member
- Join Date
- Mar 2009
- Posts
- 8
- Rep Power
- 0
in an array?
- 03-28-2009, 01:13 AM #17
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
-
Similar Threads
-
Error: Could Not Find Main Class. Program Will Exit
By silvia in forum New To JavaReplies: 2Last Post: 09-22-2011, 09:48 PM -
Error: no class definition found
By toby in forum New To JavaReplies: 6Last Post: 08-28-2011, 10:32 PM -
JRE rtapplet class not found error
By avinash.natekar in forum Java AppletsReplies: 11Last Post: 04-02-2009, 08:02 AM -
Simple program compiles but main class is not found-- not sure why
By damong in forum New To JavaReplies: 4Last Post: 01-01-2009, 03:58 AM -
No Class Def Found Error:
By wrap23 in forum New To JavaReplies: 9Last Post: 10-02-2008, 04:07 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks