• Post Reply Bookmark Topic Watch Topic
  • New Topic

First Java Program not running -NoSuchFieldError  RSS feed

 
Steven Greenbaum
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When I run I get this error message:
Exception in thread "main" java.lang.NoSuchFieldError: last_name
at onplanusermodule.OnPlanUserModule.main(OnPlanUserModule.java:29)
C:\Users\User\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)


Thanks In advance



First File Employee.java



Second File OnPlanUserModule.java



 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

The code you're showing us is not the same code that produced the exception. Did you recompile all your classes before you ran the main class?
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

If you see something unfamiliar like that, see if you can find its documentation (that is the old Java7 version). Then see what it says. That might help; of course, if you say things like, “I read the documentation for XYZ and couldn't understand the bit about clearing manure from the Ranch's pastures,” we shall be impressed about how much effort you have put into finding the solution. Then we shall see whether we know the solution, and sometimes we don't. Amongst the helpful things that link says, is this:-
that object no longer has that field.
It also tells you, as Stephan has hinted, that can usually only happen if compilation has introduced a mistake.

Let's say you have class A and objects of class A depend on class B's objects. So you compile classes A and B, then you introduce an error into class B, but the compiler doesn't pick it up. Had you compiled class A, you would have noticed, but the change was only in class B, so you only compiled class B, and that is how you got that exception; there are others like it: look at the java.lang package (again Java7) and scroll down to the Exceptions and Errors tabs. I think the following can be thrown if you make similar mistakes in compilation or loading classes:-
ClassNotFoundException EnumConstantNotPresentException NoSuchFieldException ClassFormatError IncompatibleClassChangeError NoClassDefFoundError NoSuchFieldError NoSuchMethodError UnsupportedClassVersionError
Note those exception names automatically turn into links on the forum.

Actually, it may be possible that you introduced a change in class B and B compiled into its new version, but A failed to compile. In that case, the old version of A will remain, and that occurrence can also cause such errors.
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran your code without error.  It looks like you executed the code in NetBeans.  Can you execute the code from the command line?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. Employee class. Please remind yourself from that website, what is the style of naming variable names in Java. They supposed to follow camel case convention. firstName (not first_name).

2. Employee/OnPlanUserModule classes. While package names supposed to follow convention for mix word separated by underscore on_plan_user_model, but that name is also questionable.

3. Employee class. If you don't write comments, better delete that template completely, as it makes source file longer and don't say much anyway in your case. author user - not that useful.

4. Employee class. Make fields private and have getters to access them. Have fields private almost always.

5. MainIsAPain <- read this link. Don't follow comment on line 17. Incorporate into that line findings from the given link.

6. Try to use more methods to accomplish singular tasks.

Note: i'm quite surprised this code compiles because of lines 19 and 22.
 
Steven Greenbaum
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I think the problem was that I did not recompile after moving Employe3e.java into a separate file. Problem solved.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Note: i'm quite surprised this code compiles because of lines 19 and 22.

What is wrong with lines 19 and 22?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:
Liutauras Vilda wrote:Note: i'm quite surprised this code compiles because of lines 19 and 22.

Whst is wrong with lines 19 and 22?

I didn't say something is wrong, i said surprised what is that bit before Employee(), package name? why is so?
 
Stephan van Hulst
Saloon Keeper
Posts: 7987
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
onplanusermodule.Employee is the fully qualified name of the Employee class. We generally use import statements to avoid having to identify types by their fully qualified names, but in this case neither is needed because the OnPlanUserModule class is in the same package as the Employee class. Writing onplanusermodule.Employee is not illegal, it's just redundant.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!