-1

my file wont compile i keep getting a message saying:

"Cannot make a static reference to the non-static field employeeList" in the main method at //HERE.

What am i doing wrong?

Should the LinkedList data be String or Employee??

public class TrainingCourses {
/* this is the list of employees 
 * */
private LinkedList<Employee> employeeList; 
luli kuku
  • 21
  • 7
  • There are lots of resources, including several on SO, if you google the text of the exception: "[Cannot make a static reference to the non-static field](https://www.google.com/search?q=Cannot+make+a+static+reference+to+the+non-static+field)". – yshavit Nov 03 '14 at 17:55
  • Your employeeList method is not static that's why you are getting this message, make it static.. – Hello World Nov 03 '14 at 17:56

3 Answers3

2

You are accessing employeeList without creating object of TrainingCourses. Change your code as follow.

TrainingCourses objTrainingCourses  = new TrainingCourses ();
objTrainingCourses.employeeList(new Employee(i));

or make employeeList static variable

Vicky Thakor
  • 3,847
  • 7
  • 42
  • 67
0

You are calling instance methods and fields from within a static method

change this

private LinkedList<Employee> employeeList; 

to this :

private static LinkedList<Employee> employeeList; 
0

Your Trainingcourses class holds a LinkedList field, employeeList, but has no instance (non-static) methods that would allow users of Trainingcourses objects to access or change the state of the contained linked list.

While one solution that has been suggested here by others is to make the LinkedList static, perhaps a better solution is to make Trainingcourses an OOP compliant class by re-thinking your design and giving your Trainingcourses class some instance methods and possibly fields.

Then in main you can create an instance of Trainingcourses and call its methods.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373