Hey all,

I'm having problems with a project I'm doing, I've started rewriting a project I'm doing as I ended up using too many arrays instead of classes >.< And running into some silly problems:


Everytime I run the program I get this error on compilation
java.lang.NoSuchMethodError: main
Exception in thread "main"

As you can see from the below code, The main exists and is public. :< Read some other threads and the error seems to be when main "isnt" declared, so rather stumped on this :<

Thankies Steph

package car;
import javax.swing.JOptionPane;

public class Car {

	private static String type;        /* Declares the strings used within Car Class*/
	private static String year;        /* Declares the strings used within Car Class*/
	private static String doors;       /* Declares the strings used within Car Class*/
	private static String make;        /* Declares the strings used within Car Class*/
	private static String model;       /* Declares the strings used within Car Class*/

	public Car() /* Constructor */
	{
		type = "";				/* Initialises the strings used within Car Class*/
		year = "";				/* Initialises the strings used within Car Class*/
		doors = "";			/* Initialises the strings used within Car Class*/
		make = "";			/* Initialises the strings used within Car Class*/
		model = "";			/* Initialises the strings used within Car Class*/
	}

	public Car(String t, String y, String g, String d, String m) /* Constructor Input */
	{
		type = t;				/* Initialises the strings used within Car Constructor*/
		year = y;				/* Initialises the strings used within Car Constructor*/
		doors = g;			/* Initialises the strings used within Car Constructor*/
		make = d;			/* Initialises the strings used within Car Constructor*/
		model = m;			/* Initialises the strings used within Car Constructor*/
	}
	
	public String toString()   	/* Converts Constructor into Outputted Line */
	{
		String test1;	/* Test output  */
		test1 = ("Type: " + Car.type + " Year: " + Car.year);   /* Test output  */
		System.out.println(test1);	/* Test output  */
		return test1; 								/* Test output  */
	}	

	public String toText()   	/* Converts Constructor into Outputted Line */
	{
		String test2; 		/* Test output  */
		test2 = ("Type: " + Car.type + " Year: " + Car.year +	" Doors: " + Car.doors + " Make: " + Car.make + " Model: " + Car.model);
		System.out.println(test2);	/* Test output  */
		return test2; 		/* Test output  */
	}	
	
	public String newCar()
	{
		String t,y,g,d,m, success;
		success = "Added successfully";
		t = JOptionPane.showInputDialog(null, "What's the Car's Name?");  /* Shows InputBox for User Input */
		y = JOptionPane.showInputDialog(null, "What's the Car's Year?");  /* Shows InputBox for User Input */
		g = JOptionPane.showInputDialog(null, "What's the Car's Doors?"); /* Shows InputBox for User Input */
		d = JOptionPane.showInputDialog(null, "What's the Car's Make?");  /* Shows InputBox for User Input */
		m = JOptionPane.showInputDialog(null, "What's the Car's Model?"); /* Shows InputBox for User Input */
		Car CarTest = new Car(t,y, g,d,m);
		toString();
		toText();
		return success;
	}

	public void main(String args[])
	{
		System.out.println("Test");
		newCar();
	}
	
	
}

how about this

public static void main

Cannot make a static reference to the non-static method newCar() from the type Car Car.java Test/src/car line 64 Java Problem

If I change it to (Auto suggestion says to add Static):
public static String newCar()

I then get (Auto suggestion then asks me to remove static, in an eternal loop):
This static method cannot hide the instance method from Object Car.java Test/src/car line 30 Java Problem

And those errors just loop, if I remove static one complains, if I add static other complains >.<

hehe thankies Steph

right, the compiler is correct, but you're not going to start the program without the static

you are trying to call the non static newCar from inside a static

use this inside of main

Car theCar = new Car();
System.out.println(theCar.newCar());

also this line, isn't really recommended, you wouldn't want to create a class of something inside itself

Car CarTest = new Car(t,y, g,d,m);

Hey,

Thank you that seems to have removed all the errors. hurrah! :*

I do have a query about this comment, Wouldn't that only ever call the default constructor which would set the values to NULL or "".

also this line, isn't really recommended, you wouldn't want to create a class of something inside itself

Car CarTest = new Car(t,y, g,d,m);

As public Car(String t, String y, String g, String d, String m) uses the t,y,g,d,m strings to take the data in, or do you mean simply changing it to use the input as follows:

theCar.type = JOptionPane.showInputDialog(null, "What's the Car's Name?");  /* Shows InputBox for User Input */
		theCar.year = JOptionPane.showInputDialog(null, "What's the Car's Year?");  /* Shows InputBox for User Input */
		theCar.doors = JOptionPane.showInputDialog(null, "What's the Car's Doors?"); /* Shows InputBox for User Input */
		theCar.make = JOptionPane.showInputDialog(null, "What's the Car's Make?");  /* Shows InputBox for User Input */
		theCar.model = JOptionPane.showInputDialog(null, "What's the Car's Model?"); /* Shows InputBox for User Input */

Wouldn't that remove the need for the "non-constructor" class though? Or have I completly misunderstood that :<

Thankies Steph

what dickersonka wants to say is, you do not need to create an object of Car within itself. What you can do is simply push the code of newCar into the constructor of the Car. Meaning the four statements where you pop up an input dialog in front of the user could be moved to the contsructor (why ? Can't they) and you could remove the newCar method.

I'd suggest reading about what static methods and static variables are, then you might start to understand why you are getting those errors, rather than guessing what you need to remove to get rid of the error. If you're using Eclipse it can be helpful because it has a lot of error detection, but you still need to understand why you get the errors that it reports to begin with.

This article has been dead for over six months. Start a new discussion instead.