我如何确保在没有获取nullpointerexception的情况下不存在添加到数组的对象ID?

时间:2019-07-30 10:19:33

标签: java arrays for-loop if-statement

我有一个任务,我要创建一个管理CD的程序。我正在使用switch语句,在第一个switch情况下,我必须将CD添加到阵列中,但前提是阵列中不存在具有相同ID的CD。

我尝试过

if (CDid != null && !CDid.equals(CDid.getCdId) {
    cdArray[counter] = cd_obj;
    counter++;
} else {
    counter = cdArray.length-1;
    System.out.println("The array is full");
}

我也曾在if语句之前尝试过for每个循环,但没有任何效果。我得到nullpointer异常和/或CD不会添加到数组中。 我不允许在代码中包含任何nullpointerexceptions,并且我在此问题上停留了2天,因此我希望有人在那里可以帮助我!

对不起,我所犯的任何错误是我第一次在Stacksoverflow上发布问题

以下代码是CD Main类

public class Cd_main {

    public static void main(String[] args){
    boolean running = true;
    String CDid, CDgenre, CDartist, CDalbum, CDstorageSpot, CDtrackAmount, 
        CDstorageAmount CDprice, searchGenre, deleteCD  ;
    CD[] cdArray = new CD[25];  
    int counter = 0;

    int choice; 
    Scanner scanner = new Scanner(System.in);

    while(running){
           ............................
           ............................
           choice = scanner.nextInt();

           switch(choice){
            case 1:
                System.out.println("......");
                System.out.println("............");
                CDid = scanner.next();  
                CDgenre = scanner.next();   
                CDartist = scanner.next();
                CDalbum = scanner.next();
                CDstorageSpot= scanner.next();
                CDtrackAmount= scanner.next();
                CDstorageAmount = scanner.next();
                CDprice = scanner.next();

                CD cd_obj = new CD(CDid, CDgenre, CDartist, CDalbum, 
                CDstorageSpot, CDtrackAmount, CDstorageAmount, CDprice);                  



               if(CDid.equalsIgnoreCase(CDid.getCdId())){    
               cdArray[counter] = cd_obj;
               counter++;
               }else{
                 counter = cdArray.length-1;
                 System.out.println("The array is full");
              }
             }
             break;
```

The CD class:
```
public class CD {

    public String cd_ID;
    public String genre;
    public String artist;
    public String album;
    public String storageSpot;
    public String trackAmount;
    public String storageAmount;
    public String price;

    public CD() {   
    }
       public CD(String cd_ID, String genre, String artist, String album, 
           String storageSpot, String trackAmount, String storageAmount, 
           String price){
           this.cd_ID = cd_ID;
           this.genre = genre;
           this.artist = artist;
           this.album = album;
           this.storageSpot = storageSpot;
           this.trackAmount = trackAmount;
           this.storageAmount = storageAmount;
           this.price = price;  
    }
    public String getCdId() {
        return this.cd_ID;
    }
    public void setCdId(String cd_ID) {
        this.cd_ID = cd_ID;
    }
    public String getGenre() {
        return this.genre;
    }
    public void setGenre(String genre) {
        this.genre = genre;
    }

    public String getArtist() {
        return this.artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }

    public String getAlbum() {
        return this.album;
    }

    public void setAlbum(String album) {
        this.album = album;
    }

    public String getStorageSpot() {
        return this.storageSpot;
    }

    public void setStorageSpot(String storageSpot) {
        this.storageSpot = storageSpot;
    }

    public String getTrackAmount() {
        return this.trackAmount;
    }

    public void setTrackAmount(String trackAmount) {
        this.trackAmount = trackAmount;
    }

    public String getStorageAmount() {
        return this.storageAmount;
    }

    public void setStorageAmount(String storageAmount) {
        this.storageAmount= storageAmount;
    }

    public String getPrice() {
        return this.price;
    }

    public void setPrice(String price){
        this.price = price;
    }

}

2 个答案:

答案 0 :(得分:1)

CDidString,所以CDid.getCdId()毫无意义。您需要遍历整个数组以确保ID不会退出。

例如:

boolean found = false;
for (int i = 0; i < counter; ++i) {
    if (cdArray[i].getCdId().equalsIgnoreCase(CDid)) {
        found = true;
        break;
    }
}
if (found) {
    System.out.println(CDid + " already exists");
} else if (counter >= cdArray.length) {
    System.out.println("The array is full");
} else {
    cdArray[counter] = new CD(CDid, CDgenre, CDartist, CDalbum, 
            CDstorageSpot, CDtrackAmount, CDstorageAmount, CDprice);
    ++counter;
}

还请注意,除非您知道可以将其添加到数组中,否则无需创建CD

答案 1 :(得分:0)

问题是,我想您是错误地阅读了它。在处理字符串时,没有分隔符(在这种情况下为换行符)无法将它们彼此分开。您应该使用Scanner.nextLine,而不要使用Scanner.nextScanner.next一次只能读取一个字符。

现在,对于应该检查数组中现有ID的方式:

//For loop to check if any of the elements match the CDId. The variable exists will be
//set to true if it already exists in the array.
boolean exists = false;
if(CDId != null) {
    for(int i = 0; i < cdArray.length; i++) {
        if(cdArray[i] == null) continue;
        if(CDId.equalsIgnoreCase(cdArray[i].getCdId())) {
            exists = true;
            break;
        }
    }
}
if(exists) {
    //If the CDId already exists... (put your own code if you want, such as an error message)
} else {
    //If the id doesn't exist in the array yet, add the cd_obj to the array...
    boolean added = false;
    for(int i = 0; i < cdArray.length; i++) {
        if(cdArray[i] == null) {
            cdArray[i] = cd_obj;
            added = true;
            break;
        }
    }
    //if the cd_obj wasn't added to the array, notify the user.
    if(!added) {
        System.out.println("The array is full");
    }
}