java - 类数组的空指针异常

标签 java arrays nullpointerexception

我的类不断出现空指针异常。

客户端类别:

public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);
    int count = 0;

    for (int i = 0; i < 100; i++){

        count ++;
        System.out.println(i);
        Clip[] newClip = new Clip[100];
        newClip[i] = new Clip();
        menu();
        int option = kb.nextInt();

        switch(option){
            case 1: System.exit(0);
                    break;

            case 2: newClip[i].Input();
                    newClip[i].Output();
                    break;

            case 3: int indexclip = 0;
                    int testclip = 0;
                    Scanner key = new Scanner(System.in);
                    System.out.println("Enter the index number of the clip: ");
                    testclip = key.nextInt();

                    for (int j = 0; j < 100; j++){

                        indexclip = newClip[j].getIndex(); // happens here
                        System.out.println(indexclip);
                        if(testclip == indexclip){
                            j = 120;
                            newClip[j].Output(); // and i would assume here too
                        }
                    }
                    break;
        }
    }
}

剪辑类别:

import java.util.*;

public class Clip {

private int index;
private String surname;        
private float length;
private float speed;
private String time = "testing";

public Clip(){
    index = 0;
    surname = "N/A";
    length = (float) 0.00;
    speed = (float) 0.00;
    time = "0:00AM";        
}

public void Output(){
    System.out.println("Index: "+ index);
    System.out.println("Surname: " + surname);
    System.out.println("Length: " + length);
    System.out.println("Speed: "+ speed + "m/s");
    System.out.println("Time: "+ time);


}
public void Input(){
    int testint;
    float testfloat;
    int spacePos;
    String testString;

    Scanner kb = new Scanner(System.in);
    Scanner key = new Scanner(System.in);

    System.out.println("Input an index number between 1 - 10000: ");
    testint = kb.nextInt();
    for (int i = 0; i < 100; i++){
        if (testint < 1 || testint > 10000){

            System.out.println("Input an index number between 1 - 10000: ");
            testint = kb.nextInt();

           }

        else {
            i = 120;
        }
    }
    index = testint;

    System.out.println("What is the competitor's Surname and their Given name: ");
    surname = key.nextLine();

    System.out.println("Length of the Tape in seconds: ");
    testfloat = kb.nextFloat();
    for (int i = 0; i < 100; i++){
        if (testfloat < 1 || testfloat > 60){

            System.out.println("Length of the Tape in seconds: ");
            testfloat = kb.nextFloat();

           }
        else {
            i = 120;
        }
    }
    length = testfloat;

    System.out.println("Estimated Speed of competitor in metres per second: ");
    testfloat = kb.nextFloat();
    for (int i = 0; i < 100; i++){
        if (testfloat < 7 && testfloat > 13){

            System.out.println("Estimated Speed of competitor in metres per second: ");
            testfloat = kb.nextInt();

           }
        else {
            i = 120;
        }
    }
    speed = testfloat;

    System.out.println("Time of recording between 0900 - 1700: ");
    testString = key.nextLine();

    for (int i = 0; i < 100; i++){
        if (testString.length() != 4){
            System.out.println("Time of recording between 09:00 - 17:00: ");
            testString = key.nextLine();
        }
        else {
            i = 120;
        }
    }
    time = testString;
}

public int getIndex(){
    return index;
}

public String getSurname(){
    return surname;
}

public float getLength(){
    return length;
}

public float getSpeed(){
    return speed;
}

public String getTime(){
    return time;
}

我知道 Clip 类必须初始化,我已经这样做了,并且我首先运行选项 2,以便不使用另一个类上的构造函数。我只是希望它读取索引号并扫描剪辑数组以查找匹配的索引号

任何帮助都会有帮助

干杯

最佳答案

您需要将数组的声明移到第一个 for 循环之外。

Clip[] newClip = new Clip[100];
for (int i = 0; i < 100; i++){
 /**/
}

因为当您处于 switch 语句的第 3 种情况时,您会循环遍历数组 Clip 的所有元素(但由于您只设置了一个元素数组的 99 个元素实际上被设置为 null)。

关于java - 类数组的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19568759/

相关文章:

javascript - 新插入的值将替换数组中的所有值

java.lang.runtimeException 无法启动 Activity componentinfo java.lang.nullpointerException android

java - setTime() 抛出空指针异常

java - 我可以获取相同的对象及其值并在不同的方法中使用它吗?

java - 如何在java中获取数组中的第一个和最后一个元素?

java - 如何使用restclient将字典传递到android中的Web服务?

Android: getWriteableDatabase() 错误

java - 如何使接口(interface)扩展与通用参数兼容?

arrays - sails.js Waterline 保存、读取、解析列表或数组

c - 将字符串从结构传递到字符串数组 - C