Results 1 to 12 of 12
  1. #1
    wayenng is offline Member
    Join Date
    Dec 2012
    Posts
    18
    Rep Power
    0

    Default 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);
      }
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,302
    Rep Power
    20

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    wayenng is offline Member
    Join Date
    Dec 2012
    Posts
    18
    Rep Power
    0

    Default 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)

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,302
    Rep Power
    20

    Default Re: why it throw a "NoSuchMethodError"?

    Do you have a class of your own somewhere, (badly) named String?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    wayenng is offline Member
    Join Date
    Dec 2012
    Posts
    18
    Rep Power
    0

    Default Re: why it throw a "NoSuchMethodError"?

    i beg your pardon, can you state an example?

  6. #6
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default 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...

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,302
    Rep Power
    20

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    wayenng is offline Member
    Join Date
    Dec 2012
    Posts
    18
    Rep Power
    0

    Default Re: why it throw a "NoSuchMethodError"?

    Quote Originally Posted by milovan View Post
    Your above code looks ok. As Darryl pointed there must be something else we can't see from your example...
    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...

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.)

  10. #10
    wayenng is offline Member
    Join Date
    Dec 2012
    Posts
    18
    Rep Power
    0

    Default 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.


    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);
      }
    }
    here are the error message:
    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.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,560
    Blog Entries
    7
    Rep Power
    21

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    wayenng is offline Member
    Join Date
    Dec 2012
    Posts
    18
    Rep Power
    0

    Default Re: why it throw a "NoSuchMethodError"?

    Quote Originally Posted by JosAH View Post
    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,

    Jos

    Got it!
    Thanks very much Jos!

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 2
    Last Post: 06-20-2012, 12:18 AM
  3. Replies: 8
    Last Post: 07-04-2010, 07:40 AM
  4. Replies: 5
    Last Post: 06-22-2010, 03:51 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •