0

I have an interface called Namable

public interface Namable { public String getName(); public void setName(String name); };

Several classes will implement this.

When I make code like this:

Namable foo = new X(); foo.getName();

(X is an implementing class)

I get the error:

java.lang.NoSuchMethodError: Namable.getName()Ljava/lang/String;

This is very strange to me and I'm wondering why this is happening, any thoughts?

Compilable example:

Namable class:

public interface Namable 
  {
    public String getName();
  }

Extending class X:

public class X extends java.util.ArrayList implements Namable 
{
  private String name;
  public X() 
  {
    this.name = "bar";
  }
  public String getName() 
  {
    return name;
  }
}

Testing class:

public class Test 
{
  public static void main() 
  {
    Namable foo = new X();
    foo.getName();
  }
}

After running this, I get the same error. I am using BlueJ

4
  • Are you missing a new in Namable foo = X() or is X a static factory method? Are you doing anything exotic with proxy classes? Are you using an exotic framework, like a DI framework or test stubbing framework that uses proxy classes? Commented Feb 8, 2012 at 22:18
  • 1
    Please provide more code (and correct code) for this issue. I think you're being too brief to accurately describe the problem. Commented Feb 8, 2012 at 22:19
  • Ok, There is a "new" in the code and X is just a class (X also happens to extend ArrayList, could this be part of it?). I am not doing anything fancy... In fact, I took the problem out of context and tried to debug it as shown above and its still giving the same error. Commented Feb 8, 2012 at 22:27
  • The problem goes away once I change "Namable foo" to "X foo". But its not realistic with my project architecture to do this since many classes implement Namable I need a way to generalize all of them Commented Feb 8, 2012 at 22:41

4 Answers 4

3

I have run an example with the classes you have given here and it works as expected once the main method is changed to:

public static void main(String[] args)

However, as you are using BlueJ this may not be a problem (I remember BlueJ using a special mechanism to run classes).

Do you have another class Nameable in your classpath? Check the package import and make sure it is the interface you have defined. It appears the error is with your environment and not with the code.

Sign up to request clarification or add additional context in comments.

1 Comment

This seemed to do the trick. The code had nothing to do with it and once i tweaked these the problem went away. thanks!
2

You need to call

foo.getName()

not

X.getName()

You are trying to call a class (static) method that doesn't exist.

And it should be Namable foo = new X(); - your example code shouldn't even compile as shown.

Can you provide a SSCCE ?

Updated following corrections and SSCCE: Just a wild guess now, but does BlueJ define a Namable already, or do you have a Namable in another package that might be imported instead? What happens if you rename Namable to Namable2 throughout, in the example above?

1 Comment

Oh dear.. I'm sorry that was a typo I am calling the method by its instance name
0

As stated by DNA your post would be better with a SSCCE

My guess would be that maybe you could have missed a "static" keyword in main function

public static void main(String args[]) 

not

public void main(String args[]) 

Could be something a classpath problem too... Or a file name and class mismatch. Or something else.

1 Comment

If the problem is a classpath or name mismatch issue what should I do to fix it. I've never encountered something like this
0

When I ran your code snippet, the error shown is

Error: Main method not found in class Test, please define the main method as: public static void main(String[] args)

Change your main method to

public static void main(String[] args)

I am not getting

java.lang.NoSuchMethodError: Namable.getName()Ljava/lang/String;

with your given code snippet.

1 Comment

I'm starting to believe this is a problem with BlueJ... I created a completely new project to test my code snippet and got the same error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.