Results 1 to 5 of 5
Thread: Why do I get this error?
- 07-10-2012, 09:00 PM #1
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Why do I get this error?
I'm revising polymorphic arrays and I get an error. I dont know why this is. Anyway my array just processes different employee types (they each inherit from the abstract Employee class and have their own implementation of earnings() - I also make changes to a commission employee
Eclipse says "currentEmployee cannot be resolved to a variable" in the if statement, but havent I already declared it in the enhanced for loop? the stack trace isJava Code:public static void main(String[] args) { SalariedEmployee salariedEmployee = new SalariedEmployee("John", 35); CommissionEmployee commissionEmployee = new CommissionEmployee("Haruka", 43, 0.5); Employee[] employees = new Employee[2]; employees[0] = salariedEmployee; employees[1] = commissionEmployee; for( Employee currentEmployee : employees ); { if( currentEmployee instanceof CommissionEmployee ) { CommissionEmployee employee = (CommissionEmployee) currentEmployee; employee.setCommissionRate(0.8); } System.out.println("Employee type: " + employees[currentEmployee].getClass(). getName() + " earns " + currentEmployee.earnings(); } }
what ")"? I've looked over this and dont know what I'm missingJava Code:Exception in thread "main" java.lang.Error: Unresolved compilation problems: currentEmployee cannot be resolved to a variable currentEmployee cannot be resolved to a variable currentEmployee cannot be resolved to a variable currentEmployee cannot be resolved Syntax error, insert ")" to complete Expression at Workshop.main(Workshop.java:184)
Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.
- 07-10-2012, 09:10 PM #2
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: Why do I get this error?
I had an extra semi-colon... why does this happen? I looked it over beforehand and then realise only after I've posted. typical. ugh if I hadnt stopped java this syntax wouldnt have happened... I also see how I cant use the enhanced loop for printing the class name and stuff at the end I need a normal loop
Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.
-
Re: Why do I get this error?
This:
Is equivalent to this:Java Code:for( Employee currentEmployee : employees ); { if( currentEmployee instanceof CommissionEmployee ) { CommissionEmployee employee = (CommissionEmployee) currentEmployee; employee.setCommissionRate(0.8); } System.out.println("Employee type: " + employees[currentEmployee].getClass(). getName() + " earns " + currentEmployee.earnings(); }
Java Code:for( Employee currentEmployee : employees ) { ; // this empty statement gets called in the loop. } // this block runs no matter what { if( currentEmployee instanceof CommissionEmployee ) { CommissionEmployee employee = (CommissionEmployee) currentEmployee; employee.setCommissionRate(0.8); } System.out.println("Employee type: " + employees[currentEmployee].getClass(). getName() + " earns " + currentEmployee.earnings(); }
- 07-10-2012, 09:38 PM #4
Senior Member
- Join Date
- Apr 2012
- Posts
- 115
- Rep Power
- 0
Re: Why do I get this error?
Yes I see thank you, to make this thread somewhat more productive I do have a question I would like to ask of you.
I'm implementing interface payable on Employee class.
payable contains as abstact(obviously) method called getpayment
now, because Employee class is abstract it doesnt provide an implementation (the method is only visable in payable and the subclasses of Employee.
Is this how it should be? can I write
public abstract double getpayment()
in Employee class even know I dont need to (to make it clearer that this is being passed down) or do I just leave it out? what is best practice.Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.
- 07-11-2012, 10:00 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Why do I get this error?
Leave it out.
Java doc would pick up the existence of the inherited method, and the compiler (or IDE) would pick up the lack of an implementation in any of the Employee subclasses. No need to fill up Employee with irrelevant stuff.
Though I would call it Payable, and getPayment()...naming standards and all that.Please do not ask for code as refusal often offends.
Similar Threads
-
java.lang.NullpointerException error in tomcat 5.5 / http status 500 error
By rahil in forum Apache CommonsReplies: 3Last Post: 05-08-2012, 05:26 PM -
> Operator cannot be applied error and return incompatible types error
By corney_16 in forum New To JavaReplies: 1Last Post: 03-10-2010, 01:53 PM -
Thread: Error 500--Internal Server Error java.lang.NullPointerException
By jackdear44 in forum New To JavaReplies: 1Last Post: 12-05-2009, 07:28 AM -
Diference Between compiler error Garbage collection and Runtime Error?
By makpandian in forum New To JavaReplies: 3Last Post: 01-23-2009, 08:53 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks