You are having trouble referring to a method in another class

I am almost completely new to Java and programming in general (my main degree is in Law, but I hope to open myself up to programming, as I really believe that this will be an important skill for a couple of years).

I created two classes, LabClass and Student , and I need to point students to the classroom, providing them loans for registration.

Here is the method in my Student class that I am trying to access:

 public void addCredits(int additionalPoints) { credits += additionalPoints; } 

And here is the method in my LabClass class that I am trying to use:

 public void enrollStudent(Student newStudent) { if(students.size() == capacity) { System.out.println("The class is full, you cannot enrol."); } else { students.add(newStudent); } students.addCredits(50); } 

I do not understand why BlueJ says that it cannot find the "addCredits" method. I am a real beginner, so I'm sorry if this is a stupid question, but any help would be greatly appreciated!

+5
source share
2 answers

You never show what students variable is. However, it is an array from Student s. You call addCredits in the students array, and not in the newStudent variable, which I assume you want to do. The correct way to do this is:

 public void enrollStudent(Student newStudent) { if(students.size() == capacity) { System.out.println("The class is full, you cannot enrol."); } else { newStudent.addCredits(50); students.add(newStudent); } } 
+4
source

Students are an ArrayList or something else ... not a student. I think maybe you want to say newStudent.addCredits(50);

+4
source

Source: https://habr.com/ru/post/1272969/


All Articles