0

I just added a method to an enum. Whenever I invoke that method I get a NoSuchMethodError:

public enum PHASE {
  PHASE1,
  PHASE2(false),
  PHASE3;

  private boolean present = true;

  PHASE() {
  }

  PHASE(boolean present) {
    this.present = present;
  }

  public boolean isPresent() {
    return this.present;
  }
}

public void foo(PHASE phase) {
  if (phase.isPresent()) {
...

Here phase.isPresent throws a NoSuchMethodError after clean/build. What am I missing?

--

UPDATE: Netbeans has two cache folders. One was empty, the other one was not. That is my bad, apparently I didn't put enough effort into the caching issue. Unfortunately I cannot downvote my own question...

András Hummer
  • 960
  • 1
  • 17
  • 35

2 Answers2

4

This is probably an issue between your compile-time classpath and you run-time classpath. Your classes/jars files at runtime are not the same that you used at compile time.

Clean and rebuild your project to be sure.

TheEwook
  • 11,037
  • 6
  • 36
  • 55
  • I already have, at least a dozen times. `if (phase.present)` works neither, in this case a `java.lang.NoSuchMethodError: MyClass$ERASE_PHASE.access$000(LMyClass$ERASE_PHASE;)Z` is thrown. – András Hummer Apr 04 '13 at 10:37
  • Thank you, I take a look at the compile and runtime classpath differences, whether any. – András Hummer Apr 04 '13 at 10:38
  • What if you try to use a different IDE. It might be an issue about your IDE. You can try to clear manually the generated classes and check the file generated (with a java decompiler you will be able to check the differences) – TheEwook Apr 04 '13 at 11:07
  • 1
    @TheEwook - I would use `javap` for that. – Stephen C Apr 04 '13 at 11:11
2

It is possible that this is Netbeans specific issue. Especially if you use "Compile on save option" and have a big project with a lot of dependencies. See here for details.

I had a very similar problem with such (maven-based) project almost on a daily basis, and found a solution using the above link. Better to say variations of solution. Try it like this:

please feedback.

Community
  • 1
  • 1
linski
  • 5,046
  • 3
  • 22
  • 35
  • Thank you, I already have the ScanOnDemand plugin added as due to a known issue 7.2's background scan can fall into an infinite loop (already fixed in 7.2.1, I just haven't got the time to upgrade yet). This by default disables the compile on save feature, so after each modification I always run a full clean/build before starting the next debug session. The cache folder is empty (has been empty for weeks, which is also a bit odd). – András Hummer Apr 04 '13 at 10:55
  • yes, that's definetley odd. Whenever I looked at it, regardless of options, it always had something. Hm. Is it a maven project? – linski Apr 04 '13 at 10:58
  • No, plain old Java project checked out via SVN. – András Hummer Apr 04 '13 at 11:07
  • you might wanna try running the project via ant from command line, it should do less processing that way if I remeber correctly. If you find a solution please feedback. PS I moved to eclipse because it got to irritating after some time. Now I got irritated by it too, so I'm moving to IDEA :) – linski Apr 04 '13 at 11:17