简体   繁体   English

对象被视为未初始化的变量

[英]object being treated as uninitialised variable

I have a Student class and my main program, I create a new student object like so: 我有一个Student类和我的主程序,我创建了一个新的Student对象,如下所示:

Student newStudent = new Student(newStudentName, newMarkOne, newMarkTwo, newMarkThree);

However when I later try to access the printArrays method in the student class like so: 但是,当我稍后尝试像这样访问学生类中的printArrays方法时:

newStudent.printArrays();

I get a error saying with newStudent saying: 我收到一条错误消息,说newStudent说:

java: variable newStudent might not have been initialized

But Student.printArrays(); 但是Student.printArrays(); works fine. 工作正常。

Is this problem due to the object not being created until run time? 是否由于在运行时才创建对象而导致此问题? How can I fix this error? 如何解决此错误?

EDIT: 编辑:

Where Student is called: Student地方:

case 1:
  System.out.println("Enter the student's name in this format - Surname,Forename: ");
  newStudentName = input.next();
  while (!newStudentName.matches("[A-Z][a-zA-Z]*,[A-Z][a-zA-Z]*")) {
    System.out.println("Enter name in correct format - Surname,Forename (no whitespace and uppercase first letters)");
    newStudentName = input.next();
  }
  System.out.println("Enter the first mark: ");
  newMarkOne = input.nextInt();
  System.out.println("Enter the second mark: ");
  newMarkTwo = input.nextInt();
  System.out.println("Enter the third mark: ");
  newMarkThree = input.nextInt();
  Student newStudent = new Student(newStudentName, newMarkOne, newMarkTwo, newMarkThree);
break;
case 2:
  System.out.println("Which student would you like to delete?");
  newStudent.printArrays();
break;

Student Class: Student班:

public class Student {
  private String studentName;
  private int markOne, markTwo, markThree;
  private double score;
  private static String course = "French";
  private static String[] studentNamesArray = new String[10];
  private static int[][] studentMarksArray = new int[10][3];
  private static int nameArrayCount, markArrayCount;

  public Student(String newStudentName, int newMarkOne, int newMarkTwo, int newMarkThree) {
    if (nameArrayCount < 10) {
      this.studentName = newStudentName;
      this.markOne = newMarkOne;
      this.markTwo = newMarkTwo;
      this.markThree = newMarkThree;
      this.score = ((markOne + markTwo + markThree) / 3);
      studentNamesArray[nameArrayCount] = studentName;
      nameArrayCount = nameArrayCount + 1;
      studentMarksArray[markArrayCount][0] = markOne;
      studentMarksArray[markArrayCount][1] = markTwo;
      studentMarksArray[markArrayCount][2] = markThree;
      markArrayCount = markArrayCount + 1;
    }
    else if (nameArrayCount == 10) {
      System.out.println("******Array is full, please delete a student before adding another.*****");
    }
  }
}

You probably have something similiar to this in your code: 您的代码中可能与此类似:

Student newStudent;
if (condition) {
   newStudent = new Student(newStudentName, newMarkOne, newMarkTwo, newMarkThree);
}

newStudent.printArrays(); // here you get the "might not have been initialized"

It does not have to be an if block, could also be any other block like a try/catch. 它不必是if块,也可以是任何其他块,例如try / catch。 To fix it, change Student newStudent; 要解决此问题,请更改Student newStudent; to Student newStudent = null; Student newStudent = null; .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM