mardi 30 juillet 2019

How do i make sure that the ID of object im adding to array doesn't exist without getting nullpointerexception?

I have an assignment where i am to create a program that manages CDs. I am using switch statement and in the first switch case I have to add a CD to the array but only if a CD with the same ID doesn't already exist in the array.

I've tried with 

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

I've also tried a for each loop before the if statement but nothing works. Either i get the nullpointer exception and/or the cd wont add to the array. I am not allowed to have any nullpointerexceptions in the code and I am stuck on this problem for 2 days now so im hoping someone out there can help me!

Sorry for any errors ive made this is my first time posting a question on Stacksoverflow

The following code is the CD Main class

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(cdArray[0].getCdId())){  
              cdArray[counter] = s_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;
    }

}

Aucun commentaire:

Enregistrer un commentaire