Issue
i have this string declaration in a class
String username,ICNo,studentID,password;
and i have this method that reads from a txt file therefore assigning variables it got from the string
static void createFound(String ID){
try {File myObj = new File(infoFile);
Scanner sc = new Scanner(myObj);
boolean notFound=true;
while (sc.hasNextLine()&¬Found) {
String data = sc.nextLine();
String[] userInfo = data.split(Character.toString(seperator));
if(userInfo[2].equals(ID)){
notFound=false;
username=userInfo[0];
ICNo=userInfo[1];
studentID=userInfo[2];
password=findPass(userInfo[0]);
// System.out.println(foundUser.username);
}
}
sc.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
i have an error here (non-static variable this cannot be referenced from a static context) on line
username=userInfo[0];
ICNo=userInfo[1];
studentID=userInfo[2];
password=findPass(userInfo[0]);
i have trird adding static declaration to the line where i declare my variables but found that if i do in that way it returns a blank when i do this
jLabel9.setText(student.studentID);
Solution
Remove the static from method createFound() or make the variables static.
Answered By - iamdhavalparmar