ArrayIndexOutOfBoundsException和NullPointerException会给我的JAVA程序带来问题

ArrayIndexOutOfBoundsException和NullPointerException会给我的JAVA程序带来问题,java,Java,我正在从事一个有关电子数据处理系统的项目,由于上述各种例外情况,我面临一些问题。在代码中,我使用列表数组。我将在下面向您展示代码: package panellinies; import java.util.ArrayList; import java.util.Scanner; public class Panellinies { private static int i; private static int j; private static int k; public sta

我正在从事一个有关电子数据处理系统的项目,由于上述各种例外情况,我面临一些问题。在代码中,我使用列表数组。我将在下面向您展示代码:

package panellinies;

import java.util.ArrayList;
import java.util.Scanner;



public class Panellinies 
{
private static int i;
private static int j;
private static int k;

public static void main(String[] args) 
{
    Scanner input = new Scanner(System.in);
    System.out.println("Give the number of candidates");
    System.out.println();
    int num_candidates = input.nextInt();
    System.out.println();
    System.out.println("Give the number of the universities");
    int num_universities = input.nextInt();
    System.out.println();
    System.out.println("Give the number of lessons");
    int num_lessons = input.nextInt();
    System.out.println();
    float[][] coefficients = new float [num_universities][num_lessons];
    for (i = 0; i < num_universities; i++)
    {
        for (j = 0; j < num_lessons; j++)
        {
            System.out.println("Give the coefficients of each lesson for each university");
            coefficients[i][j] = input.nextFloat();
        }
    }
    float[][] grades = new float [num_candidates][num_lessons];
    for (i = 0; i < num_candidates; i++)
    {
        for (j = 0; j < num_lessons; j++)
        {
            System.out.println("Give the grade of the candidate No. " + i+1 + " in lesson " + j+1);
            do 
            {
                grades[i][j] = input.nextFloat();
            } while (grades[i][j] > 100 || grades[i][j] < 0);
        }
    }
    int[] max_numberofcandidates = new int [num_universities];
    for (i = 0; i < num_universities; i++)
    {
        System.out.println("Give the number of candidates that each university " + (i+1) + " accepts");
        max_numberofcandidates[i] = input.nextInt();
    }
    int university_code = 0;
    ArrayList<Integer>[] selected_universities = new ArrayList [num_candidates];

    for (i = 0; i < num_candidates; i++)
    {
        try
        {
            selected_universities[i].add(0);
        }
        catch (NullPointerException e)
        {
            System.out.println("The exception: " + e + " has occured!");
        }
    }
    int leaver = 0;
    System.out.println("Breakpoint 1");
    for (i = 0; i < num_candidates; i++)
    {
        System.out.println("Candidate No. " + i+1 + " give in sorted sequence the increasing codes of the universities you wish to enter...");
        System.out.println("Press 0 to terminate your list");
        do
        {
            try
                {
                    if (leaver != 0) break;
                    System.out.println("Give your code now...");
                    university_code = input.nextInt();
                    selected_universities[i].add(university_code);
                    leaver++;
                }
            catch(NullPointerException e)
                    {
                        System.out.println("The exception: " + e + " has occured!");
                    }
        } while (university_code != 0);
    }
    System.out.println("Breakpoint 2");
    ArrayList<Integer>[] temporarily_success_candidates = new ArrayList [num_universities];
    for (i = 0; i < num_universities; i++)
    {
        try
        {
            temporarily_success_candidates[i].add(0);
        }
        catch (NullPointerException e)
        {
            System.out.println("The exception: " + e + " has occured!");
        }
    }
    float[][][] telikoi_vathmoi = new float [num_candidates][num_universities][num_lessons];
    float[][] average_grades = new float [num_candidates][num_universities];
    for (i = 0; i < num_candidates; i++)
    {
        for (j = 0; j < num_universities; j++)
        {
            average_grades[i][j] = 0;
            for (k = 0; k < num_lessons; k++)
            {
                telikoi_vathmoi[i][j][k] = 0;
            }
        }
    }
    for (i = 0; i < num_candidates; i++)
    {
        for (j = 0; j < num_universities; j++)
        {
            telikoi_vathmoi[i][j][0] = 0;
            for (k = 0; k < num_lessons; k++)
                telikoi_vathmoi[i][j][k] += grades[i][k] * coefficients[j][k];
            System.out.println("Breakpoint 3");
            try
            {
                average_grades[i][j] = telikoi_vathmoi[i][j][k] / num_lessons;
            }
            catch (ArrayIndexOutOfBoundsException e)
            {
                System.out.println("The exception: " + e + " has occured!");
            }
        }
    }
    System.out.println("Breakpoint 4");
    int highly_preferred_university;
    try
    {
        for (i = 0; i < num_candidates; i++)
        {   
        //while ((selected_universities[counter].get(0) != null) && (temporarily_success_candidates[counter].get(0) == null))
            if ((selected_universities[i].get(0) != null) && (temporarily_success_candidates[i].get(0) == null))
            {
                try
            {
                highly_preferred_university = selected_universities[i].get(0);
            if (temporarily_success_candidates[i].size() < max_numberofcandidates[i])
                temporarily_success_candidates[i].add(i);
            else
            {
                if (average_grades[i][highly_preferred_university] < average_grades[i - 1][highly_preferred_university])
                System.out.println("Candidate No. " + (i + 1) + " you are rejected!");
                else 
                {
                    for (j = i; j > 0; j--)
                    {
                        if (average_grades[j][highly_preferred_university] < average_grades[i][highly_preferred_university])
                            temporarily_success_candidates[highly_preferred_university].remove(j);
                    }

                }
                 temporarily_success_candidates[i].add(i);
            }
            System.out.println("Breakpoint 5");
            selected_universities[i].remove(highly_preferred_university);
            }
            catch (NullPointerException e)
            {
                System.out.println("The exception: " + e + " has occured!");
            }
            }

        }
    }
    catch (NullPointerException e)
    {
        System.out.println("The exception: " + e + " has occured!");
    }
    System.out.println("Breakpoint 6");
    for (i = 0; i < num_universities; i++)
    {
        try
        {
            System.out.println("The candidates having succeeded in entering the university with code: " + selected_universities[i] + " are the below ones: " + temporarily_success_candidates[i]); 
        }
        catch (NullPointerException e)
        {
            System.out.println("The exception: " + e + " has occured!");
        }
    }
    System.out.println("Breakpoint 7");
}

}
包装面板;
导入java.util.ArrayList;
导入java.util.Scanner;
公营小组委员会
{
私有静态int i;
私有静态int j;
私有静态int k;
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
System.out.println(“给出候选人数”);
System.out.println();
int num_candidates=input.nextInt();
System.out.println();
System.out.println(“给出大学数量”);
int num_universions=input.nextInt();
System.out.println();
System.out.println(“给出课程数量”);
int num_lessons=input.nextInt();
System.out.println();
浮动[][]系数=新浮动[num_大学][num_课程];
对于(i=0;i100 | |等级[i][j]<0);;
}
}
int[]max_numberofcandidates=新int[num_universions];
对于(i=0;i0;j--)
{
if(平均成绩[j][highly_preference_university]<平均成绩[i][highly_preference_university])
临时成功候选人[非常喜欢大学].删除(j);
}
}
暂时的成功
//YOUR CODE
int university_code = 0;
ArrayList<Integer>[] selected_universities = new ArrayList[num_candidates];
    for (i = 0; i < num_candidates; i++)
    {
        selected_universities[i] = null;
    }
selected_universities[i]= new ArrayList();