I have got through most of my code for a Student Registration Interface without difficultly, however I am getting this error.
non-static method cannot be referenced from a static context.
Now, I know this kind of problem is quite common, but it is still not clicking and none of the ones I have found seems to relate similarly enough to my own code to get to a solution myself, as this error is going of with methods inside another method of the same class.
Here is the entire code, with the main areas marked with a comment:
import java.util.Scanner;
public class RegistryInterface
{
private Registry theRegistry = null;
public RegistryInterface(Registry theRegistry)
{
}
public void doMenu()
{
boolean menuClose = false;
while (menuClose != true)
{
Scanner in = new Scanner(System.in);
System.out.println("Registry Main Menu");
System.out.println("******************");
System.out.println("");
System.out.println("1) Add a Student");
System.out.println("2) Delete a Student");
System.out.println("3) Print Registry");
System.out.println("4) Quit");
System.out.println("Select option [1, 2, 3, 4] :> ");
int option = in.nextInt();
if (option == 1)
{
boolean anotherStudent = false;
while (anotherStudent != true)
System.out.println("");
System.out.println("Add New Student");
System.out.println("***************");
System.out.println("");
System.out.println("Enter forename :> ");
String aForeName = in.nextLine();
System.out.println("Enter surname :> ");
String aSurName = in.nextLine();
System.out.println("Enter student ID :> ");
String aID = in.nextLine();
System.out.println("Enter degree scheme :> ");
String aDegreeScheme = in.nextLine();
RegistryInterface.doAddStudent(aForeName, aSurName, aID, aDegreeScheme); // static error here
System.out.println("Enter another (1 for Yes/2 for No) :> ");
int nextStudent = in.nextInt();
if (nextStudent == 1)
{
anotherStudent = false;
}
else if (nextStudent == 2)
{
anotherStudent = true;
}
else
{
System.out.println("No correct number entered, exiting menu...");
anotherStudent = true;
}
}
if (option == 2)
{
boolean anotherStudent2 = false;
while (anotherStudent2 != true)
{
System.out.println("");
System.out.println("Delete a Student");
System.out.println("****************");
System.out.println("");
System.out.println("Enter Student ID :> ");
int studentID = in.nextInt();
RegistryInterface.doDeleteStudent(studentID); // Static error here.
System.out.println("Delete another (1 for Yes/2 for No) :> ");
int nextStudent2 = in.nextInt();
if (nextStudent2 == 1)
{
anotherStudent2 = false;
}
else if (nextStudent2 == 2)
{
anotherStudent2 = true;
}
else
{
System.out.println("No correct number entered, exiting menu...");
anotherStudent2 = true;
}
}
if (option == 3)
{
System.out.println("");
RegistryInterface.doPrintRegistry(); // static error here.
}
if (option == 4)
{
menuClose = true;
}
}
}
}
private void doAddStudent(String aForeName, String aSurName, String aID, String aDegreeScheme)
{
theRegistry.addStudent(new Student(aForeName, aSurName, aID, aDegreeScheme));
}
private void doDeleteStudent(int studentID)
{
theRegistry.deleteStudent(studentID);
}
private void doPrintRegistry()
{
System.out.println(theRegistry.toString());
}
}
So, for the first error, it would be:
non-static method doAddStudent(String,String,String,String) cannot be referenced from a static context.
What is the simplest way to overcome this problem? Additional details can be provided if needed.