ArrayIndexOutOfBoundsException和NullPointerException导致我的JAVA程序出现问题

时间:2015-05-10 23:17:00

标签: 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 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");
}

}

try-catch块指示ArrayIndexOutOfBoundsException和NullPointerException发生的位置。

如何解决这些问题,以免发生异常?

我期待尽快收到您的回复。

提前谢谢!

1 个答案:

答案 0 :(得分:0)

目前尚不清楚你在这里要做什么:

//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;
    }

我认为你得到一个空指针异常,因为你没有初始化每个ArrayList。你只是将它设置为null。

尝试用以下内容替换空行:

selected_universities[i]= new ArrayList();

我很难说出你之后想要做什么,但是你可能需要在此之后调整(leave!= 0)代码。