Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 循环中的Getter在赋值后返回null_Java_Oop_Loops_Null - Fatal编程技术网

Java 循环中的Getter在赋值后返回null

Java 循环中的Getter在赋值后返回null,java,oop,loops,null,Java,Oop,Loops,Null,我的程序有点小问题。简言之,我所做的是: 选择“添加新学生记录”选项 当我试图显示新添加的值(名字、姓氏、课程)时,它都返回为null 我不知道出了什么问题,但我会尽力发布所需的代码,以便大家都能更好地看到 主要类别: public static void main(String[] args) throws Exception{ String cont1=" "; String X; int x1,arr_count=1; String[]

我的程序有点小问题。简言之,我所做的是:

  • 选择“添加新学生记录”选项

  • 当我试图显示新添加的值(名字、姓氏、课程)时,它都返回为null

  • 我不知道出了什么问题,但我会尽力发布所需的代码,以便大家都能更好地看到

    主要类别:

    public static void main(String[] args) throws Exception{
    
    
        String cont1=" ";
        String X;        
        int x1,arr_count=1;
        String[] option = {"First Name","Last Name","Course"};
    
        BufferedReader reader1=new BufferedReader(new InputStreamReader(System.in));
        do{      
    
        System.out.println("\nWelcome! Please select a task: \nA. View a record.\nB. Add a record.\nC. Edit a record.\nD. Exit the program.");
        String option1 = reader1.readLine();
    
        if("a".equals(option1)|| "A".equals(option1)){
            System.out.println("Please enter the Student ID for the record to view.");
            String a = reader1.readLine();
            x1 = Integer.parseInt(a);
            for(int y=0;y<3;y++){
            System.out.print(Records.getRecord(x1,y) + " ");
            }            
            System.out.print("\nSubjects and Grades: \n");
            for(int y=3;y<6;y++){
            System.out.print(Records.getRecord(x1,y) + " (" + Records.getRecord(x1,y+3) + ")\n");
            } 
    
            System.out.println("\nReturn to Main Menu? (Y/N)");
            cont1= reader1.readLine(); 
        }
    
        else if("b".equals(option1)||"B".equals(option1)){
            arr_count++;
            for (int y = 0; y<3;y++){
            System.out.println("Enter Value for "+ option [y]+":"  ); 
            X = reader1.readLine();
            X = Records.getRecord(arr_count,y);          
            }
    
            System.out.println("\nNew Student added:\nID number: "+ arr_count);
            for(int y=0;y<3;y++){
            X = Records.getRecord(arr_count,y);
            System.out.print(X + " ");
            }
    

    我曾尝试为records数组设置空值,并将向数组分配新值的函数放入records类,但我仍然找不到一种方法来实现这一点。

    您从未设置记录的值

    X = reader1.readLine();
    X = Records.getRecord(arr_count,y);  
    
    正在丢弃读取的值。在记录类中创建setValue方法-

    X = reader1.readLine();
    Records.setValue(arr_count, y, X);
    
    ---记录 设定值(x,y,val){ 记录[x][y]=val;
    }

    看来你的主要课程还没有完成。do while循环停止的条件是什么?main方法中除了此循环还有其他内容吗?

    记录中
    您可以在参数超出范围时添加自定义异常和自定义,而不是预定义的编译器验证代码,并检测潜在错误。我没有显示其余代码,因为它们与问题无关。do while只是一个“继续?(Y/N)”选项,用于返回开始。谢谢你的回复。任何一点帮助我都明白你的意思。我会试试看,然后把结果告诉你。谢谢,伙计!
    X = reader1.readLine();
    Records.setValue(arr_count, y, X);