Results 1 to 12 of 12
- 01-13-2013, 12:46 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 14
- Rep Power
- 0
why it throw a "NoSuchMethodError"?
As subjected, anyone know why the following code can compile but can not run successfully?
Please advise...thanks a lot.
PS: i am new to Java...
Java Code:class test{ String text="000"; public test(String id){ text=id; } } public class TestMain{ public static void main(String[] args) { test a =new test("111"); System.out.println(a.text); } }
- 01-13-2013, 01:00 PM #2
Re: why it throw a "NoSuchMethodError"?
When seeking help with an error, always copy and paste the entire stack trace along with the exact code that gave rise to the error, also mentioning exactly how you compiled and ran the code.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-13-2013, 02:33 PM #3
Member
- Join Date
- Dec 2012
- Posts
- 14
- Rep Power
- 0
Re: why it throw a "NoSuchMethodError"?
Dear DarrylBurke,
Thanks for your remind.
Here are the error code.
Exception in thread "main" java.lang.NoSuchMethodError: test.<init>(Ljava/lang/String;)V
at TestMain.main(TestMain.java:12)
- 01-13-2013, 03:36 PM #4
Re: why it throw a "NoSuchMethodError"?
Do you have a class of your own somewhere, (badly) named String?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-13-2013, 07:01 PM #5
Member
- Join Date
- Dec 2012
- Posts
- 14
- Rep Power
- 0
Re: why it throw a "NoSuchMethodError"?
i beg your pardon, can you state an example?
- 01-13-2013, 07:19 PM #6
Senior Member
- Join Date
- Jan 2011
- Location
- Belgrade, Serbia
- Posts
- 227
- Rep Power
- 3
Re: why it throw a "NoSuchMethodError"?
Your above code looks ok. As Darryl pointed there must be something else we can't see from your example...
- 01-13-2013, 07:36 PM #7
Re: why it throw a "NoSuchMethodError"?
On second thoughts, a local class named String would prevent the main method being used as an entry point.
It's also possible that the code posted isn't the code that was run, and that the constructor was added after the last compile.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-14-2013, 04:52 AM #8
Member
- Join Date
- Dec 2012
- Posts
- 14
- Rep Power
- 0
Re: why it throw a "NoSuchMethodError"?
ok...but what kind of something else can not see from my example means?
as it is exactly display all of the code on it...
actucally it is come from one of the SCJP sample question as well.
I just wonder why the answer said it is failed to run as my initial thought is the program should be worked properly...but i was wrong...
thus i want to get the answer here...
- 01-14-2013, 05:12 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Re: why it throw a "NoSuchMethodError"?
Is this your code?
If so, the code looks Ok as others have said. Post exactly the output ypu get from compiling and running (or trying to run) the code.
If the code came from somewhere else, post a link. And check you have copied it correctly. If the example really called the class test with a lower case 't' then forget it and move on to a site that knows what it's posting: Java classes start with a capital letter. (Sorry for being picky, but it really does matter.)
- 01-17-2013, 04:10 PM #10
Member
- Join Date
- Dec 2012
- Posts
- 14
- Rep Power
- 0
Re: why it throw a "NoSuchMethodError"?
The code is worked finally as i put those code at the inappropriate location thus the error prompt for the path incorrect, i guess.
btw, i would like to share the original code as well and this time is REALLY error definitely.
here are the error message:Java Code:class Person{ String name="No name"; public Person(String nm){ name=nm; } } class Employee extends Person{ String empID="000"; public Employee(String id){ empID=id; } } public class EmployeeTest{ public static void main(String[] args) { Employee e =new Employee("111"); System.out.println(e.empID); } }
EmployeeTest.java:11: cannot find symbol
symbol : constructor Person()
location: class Person
public Employee(String id){
^
1 error
is it because the constructor from the subclass can not override the superclass constructor?
please advise.
- 01-17-2013, 05:23 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
Re: why it throw a "NoSuchMethodError"?
Constructors don't override constructors from the super class; they always call a constructor from the super class as their first statement; if you didn't do it, the compiler creates one; a call to the no-arg constructor: super(); your super class Person doesn't have a no-arg constructor and that's why you get a compilation error. Add an appropriate super class constructor in the constructor of your Employee class and everything will be fine.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-19-2013, 04:17 AM #12
Member
- Join Date
- Dec 2012
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
access denied("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve")
By klspepper in forum New To JavaReplies: 0Last Post: 12-07-2012, 08:29 AM -
"Exception in thread "main" java.lang.NoSuchMethodError: main"
By isnkumar in forum New To JavaReplies: 2Last Post: 06-20-2012, 12:18 AM -
Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError:
By m0dus in forum New To JavaReplies: 8Last Post: 07-04-2010, 07:40 AM -
Question about error "Exception in thread "main" java.lang.NoSuchMethodError: main
By ferdzz in forum New To JavaReplies: 5Last Post: 06-22-2010, 03:51 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks