Error: Exception in thread "main" java.lang.NoSuchMethodError:

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 463 Views - Last Post: 20 November 2012 - 07:04 AM Rate Topic: -----

#1 DJ1one  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 05-October 12

Error: Exception in thread "main" java.lang.NoSuchMethodError:

Posted 20 November 2012 - 04:52 AM

I'm trying to make a program that lists Animals in order based on their "outstanding balance" - all this data coming from a txt file. I have gotten the program to compile, but i keep getting the "Exception in thread "main" java.lang.NoSuchMethodError: main" Error. I don't understand why because I do have a main method. Also, I am not too sure if my output method will work properly - correct me if i'm mistaken. Here's the code for the "Animal" class:

public class Animal {
	String pName;
	int year;
	String species;
	double bal;
	String oName;
} 


And here is the code for the "Vet Billing" class:

 import java.util.Scanner;

public class VetBilling {

	//create an array for the animals
	Animal[] pets = new Animal[5000];
	int petCount;

	//create output method
	public void output() {
		for (int i = 1; i < petCount; i++) {
			System.out.println(pets[i].pName);
			System.out.print(pets[i].year);
			System.out.print(pets[i].species);
			System.out.print(pets[i].bal);
			System.out.print(pets[i].oName);
		}
	}


	//create input method
	public void input() throws Exception {
		int i;
		Scanner somePets = new Scanner ("favorite.txt");
		petCount = somePets.nextInt();
		for (i = 0; i < petCount; i++) {
			pets[i].pName = somePets.next();
			pets[i].year = somePets.nextInt();
			pets[i].species = somePets.next();
			pets[i].bal = somePets.nextDouble();
			pets[i].oName = somePets.next();
		}
	}


	//create sort method	
	public void sort() {
		for (int i = 0; i < petCount; i++) {
			//find minimum in list
			double currentMin = pets[i].bal;
			int currentMinIndex = i;
			
			for (int j = i + 1; j < petCount; j++) {
				if (currentMin > pets[j].bal) {
					currentMin = pets[j].bal;
					currentMinIndex = j;
				}
			}
			// swap
			if (currentMinIndex != i) {
				pets[currentMinIndex] = pets[i];
				pets[i].bal = currentMin;
			}
		}
	}
		

	//main method
	public void main(String[] args) throws Exception {
		input();
		sort();
		output();
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Error: Exception in thread "main" java.lang.NoSuchMethodError:

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Error: Exception in thread "main" java.lang.NoSuchMethodError:

Posted 20 November 2012 - 05:06 AM

The requirements for the main() method signature are very specific, and it must be static.
Was This Post Helpful? 1
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,191
  • Joined: 20-September 08

Re: Error: Exception in thread "main" java.lang.NoSuchMethodError:

Posted 20 November 2012 - 05:06 AM

Quote

public void main(String[] args) throws Exception 
is wrong. Should be
public static void main(String[] args) throws Exception 

Was This Post Helpful? 1
  • +
  • -

#4 DJ1one  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 05-October 12

Re: Error: Exception in thread "main" java.lang.NoSuchMethodError:

Posted 20 November 2012 - 05:39 AM

Ok thanks i've changed the main method to static, as well as everything else that needed to be also in order to compile the progra. But now I am getting 1 error that says:
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 VetBilling.input(VetBilling.java:25)
	at VetBilling.main(VetBilling.java:60)


Here is my code:
import java.util.Scanner;

public class VetBilling {

	//create an array for the animals
	static Animal[] pets = new Animal[5000];
	static int petCount;

	//create output method
	public static void output() {
		for (int i = 1; i < petCount; i++) {
			System.out.println(pets[i].pName);
			System.out.print(pets[i].year);
			System.out.print(pets[i].species);
			System.out.print(pets[i].bal);
			System.out.print(pets[i].oName);
		}
	}


	//create input method
	public static void input() throws Exception {
		int i;
		Scanner somePets = new Scanner ("favorite.txt");
		petCount = somePets.nextInt();
		for (i = 0; i < petCount; i++) {
			pets[i].pName = somePets.next();
			pets[i].year = somePets.nextInt();
			pets[i].species = somePets.next();
			pets[i].bal = somePets.nextDouble();
			pets[i].oName = somePets.next();
		}
	}


	//create sort method	
	public static void sort() {
		for (int i = 0; i < petCount; i++) {
			//find minimum in list
			double currentMin = pets[i].bal;
			int currentMinIndex = i;
			
			for (int j = i + 1; j < petCount; j++) {
				if (currentMin > pets[j].bal) {
					currentMin = pets[j].bal;
					currentMinIndex = j;
				}
			}
			// swap
			if (currentMinIndex != i) {
				pets[currentMinIndex] = pets[i];
				pets[i].bal = currentMin;
			}
		}
	}
		

	//main method
	public static void main(String[] args) throws Exception {
		input();
		sort();
		output();
	}
}

Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Error: Exception in thread "main" java.lang.NoSuchMethodError:

Posted 20 November 2012 - 05:45 AM

So there's a mismatch between what you're trying to read from the file and the data found. Can you post the file? Post it in code tags.

BTW - Your approach to read from the file can be massaged to work, but sometimes it's easier to just read the whole line as a String and then parse the String into the required pieces and data types. But post the file being read, and we'll figure it out.

This post has been edited by GregBrannon: 20 November 2012 - 05:49 AM

Was This Post Helpful? 0
  • +
  • -

#6 DJ1one  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 05-October 12

Re: Error: Exception in thread "main" java.lang.NoSuchMethodError:

Posted 20 November 2012 - 05:48 AM

Here is the favorite.txt file
334
fuzzy   2003    dog     53      kyle
fluffy  2007    cat     320     maria
boots   1998    horse   582     unknown
socks   2009    cat     61.42   chris

Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,191
  • Joined: 20-September 08

Re: Error: Exception in thread "main" java.lang.NoSuchMethodError:

Posted 20 November 2012 - 06:02 AM

I hope that's not the entire file as it says you have 334 pets in there
Was This Post Helpful? 0
  • +
  • -

#8 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Error: Exception in thread "main" java.lang.NoSuchMethodError:

Posted 20 November 2012 - 06:05 AM

Consider this more closely, review the Scanner constructors:

Scanner somePets = new Scanner ( "favorite.txt" );
Was This Post Helpful? 0
  • +
  • -

#9 DJ1one  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 05-October 12

Re: Error: Exception in thread "main" java.lang.NoSuchMethodError:

Posted 20 November 2012 - 06:08 AM

View Postg00se, on 20 November 2012 - 06:02 AM, said:

I hope that's not the entire file as it says you have 334 pets in there


Oh right. The actual file that has 334 pets hasn't been provided yet, but this is the file i'm suppose to to test the program with. I replaced the 334 with 4, but it still gives the same error.
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,191
  • Joined: 20-September 08

Re: Error: Exception in thread "main" java.lang.NoSuchMethodError:

Posted 20 November 2012 - 06:08 AM

Quote

Scanner somePets = new Scanner ( "favorite.txt" ); 


Yes - missed that ;)/> Note my comment also (clue: 334%5 != 0)

This post has been edited by g00se: 20 November 2012 - 06:09 AM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

#11 DJ1one  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 05-October 12

Re: Error: Exception in thread "main" java.lang.NoSuchMethodError:

Posted 20 November 2012 - 06:17 AM

View PostGregBrannon, on 20 November 2012 - 06:05 AM, said:

Consider this more closely, review the Scanner constructors:

Scanner somePets = new Scanner ( "favorite.txt" );


I'm looking at it but i don't see whats wrong with it. Is it something that I have to add to it or remove??
Was This Post Helpful? 0
  • +
  • -

#12 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Error: Exception in thread "main" java.lang.NoSuchMethodError:

Posted 20 November 2012 - 06:24 AM

You want to open a Scanner object with a File as a subject, not a String.

Also, this line:

static Animal[] pets = new Animal[5000];

allocates memory space for 5000 Animal objects, but it doesn't create any Animal objects in that memory, so they're all null. I don't normally use static initialization blocks, and I'm not sure you should be, but you need to initialize those 5000 objects somewhere. Again, this is legal, but I'm not sure it's appropriate at your level:
    //create an array for the animals
    static Animal[] pets = new Animal[5000];
    static
    {
        for ( int i = 0 ; i < pets.length ; i++ )
        {
            pets[i] = new Animal();
        }
    }


Was This Post Helpful? 2
  • +
  • -

#13 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,191
  • Joined: 20-September 08

Re: Error: Exception in thread "main" java.lang.NoSuchMethodError:

Posted 20 November 2012 - 06:29 AM

Quote

I'm looking at it but i don't see whats wrong with it.
Your code operates on the String 'favorite.txt' and not the file of that name. Check javadoc as Greg suggested
Was This Post Helpful? 1
  • +
  • -

#14 DJ1one  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 05-October 12

Re: Error: Exception in thread "main" java.lang.NoSuchMethodError:

Posted 20 November 2012 - 06:37 AM

Oh so it does actually get the data from that text file?
Was This Post Helpful? 0
  • +
  • -

#15 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Error: Exception in thread "main" java.lang.NoSuchMethodError:

Posted 20 November 2012 - 06:41 AM

It will when you fix your Scanner, and I suggest you modify the file to begin with the number 4 rather than 334.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2