Java Constructor & Arrays

601 Views Asked by At

I am learning java. This is for a class that I take online. My assignment is finished. I am trying to figure out the cause for the following.

The two main issues I have are:

1)It seems to be storing only one flower and ignoring the other inputs. How can I correct this?

2) Display flower (e.g Roses - 2): Immediately after it displays the flowers and their count it will give me the following error message.

Flower Pack: 

Daffodils  - 1

Roses - 1

Exception in thread "main" java.lang.NullPointerException
    at Assignment2.displayFlowers(Assignment2.java:203)
    at Assignment2.<init>(Assignment2.java:44)
    at Assignment2.main(Assignment2.java:9)

I believe it's origination from this statement

flowerName = searchingPack[i].getName();

Here is my code:

    public class Flower {
    private String name;
   private String color;
   private String thorns;
   private String smell;    

    public Flower(){

    }

    public Flower(String flowerName, String flowerColor, String flowerThorns, String flowerSmell){

      name = flowerName;
      color = flowerColor;
      thorns = flowerThorns;
      smell = flowerSmell;

   }

   public String getName(){

      return name;
   }

   public void setName(String flowerName){
      name = flowerName;  
   }

   public String getColor(){

      return color;
   }

   public void setColor(String flowerColor){
      color = flowerColor;  
   }

   public String getThorns(){

      return thorns;
   }

   public void setThorns(String flowerThorns){
      thorns = flowerThorns;  
   }

   public String getSmell(){

      return smell;
   }

   public void setSmell(String flowerSmell){
      smell = flowerSmell;  
   }

}




import java.util.Scanner;

public class Assignment2 {
    public static void main(String[] args){
        new Assignment2 ();
    }

    // This will act as our program switchboard
    public Assignment2 (){

        Scanner input = new Scanner(System.in);
        Flower[] flowerPack = new Flower[25];

        System.out.println("Welcome to my flower pack interface.");
        System.out.println("Please select a number from the options below");
        System.out.println("");

        while(true){
            // Give the user a list of their options
            System.out.println("1: Add an item to the pack.");
            System.out.println("2: Remove an item from the pack.");
            System.out.println("3: Search for a flower.");
            System.out.println("4: Display the flowers in the pack.");
            System.out.println("0: Exit the flower pack interfact.");

            // Get the user input
            int userChoice = input.nextInt();

            switch(userChoice){
                case 1: 
                    addFlower(flowerPack);
                    break;
                case 2: 
                    removeFlower(flowerPack);
                    break;
                case 3: 
                    searchFlowers(flowerPack);
                    break;
                case 4: 
                    displayFlowers(flowerPack);
                    break;
                case 0: 
                    System.out.println("Thank you for using the flower pack interface. See you again soon!");
                    System.exit(0);
            }
        }

    }
     //ADD FLOWERS
    private void addFlower(Flower[] flowerPack) { //This is storing 1 flower but not anything else*********


         Scanner add = new Scanner(System.in);
         String name,color, thorns, smell;

         System.out.println("\nEnter the name of the flower to add: ");
         name = add.nextLine();

         System.out.println("\nEnter the color of the flower: ");
         color = add.nextLine();

         System.out.println("\nAre there thorns present: YES or NO ");
         thorns = add.nextLine();

         System.out.println("\nDoes the flower smell: YES or NO ");
         smell = add.nextLine();

         boolean flowerInserted = false;
         for(int i = 0; i < flowerPack.length; i++){
            if(flowerPack[i] == null){
               Flower newFlower = new Flower(name, color, thorns, smell);
               flowerPack[i] = newFlower;
               flowerInserted = true;
               break;
            }
         } 

         if(flowerInserted){
            System.out.println("Flower inserted.");
            System.out.println();
             }else{
              System.out.println("\nFlower pack is full and could not add another flower.\n");
            }
   }

      //REMOVE FLOWERS
    private void removeFlower(Flower[] flowerPack) {


        Scanner remove = new Scanner(System.in);
        String removeName, removeColor;

        for(int i = 0; i < flowerPack.length; i++){
            System.out.println("\nEnter the name of the flower to remove: ");
            removeName = remove.nextLine();

            System.out.println("\nEnter the color of the flower: ");
            removeColor = remove.nextLine();

            if (flowerPack[i] != null)
            if (flowerPack[i].getName().equalsIgnoreCase(removeName) && flowerPack[i].getColor().equalsIgnoreCase(removeColor)){
               for(int j = i; j < flowerPack.length - 1; j++) {       
                  flowerPack[j] = flowerPack[j + 1];      
                  }      
                  flowerPack[flowerPack.length - 1] = null;
                  System.out.println("\nFlower removed.\n");
                  break;
                  }

            else{
               System.out.println("\nThat flower was not found.\n");
               }
       }
   }

      //SEARCH FOR FLOWERS
    private void searchFlowers(Flower[] flowerPack) {

      Scanner flowerSearch = new Scanner(System.in);
      String name, color, thorns, smell;
      int options;

      System.out.println("1: Seach by name.");
      System.out.println("2: Seach by color.");
      System.out.println("3: Seach for flowers with thorns.");
      System.out.println("4: Seach for flowers that smell.");
      options = flowerSearch.nextInt();

      flowerSearch.nextLine();      

      boolean found = false;
      for(int i = 0; i < flowerPack.length; i++){
         if(options == 1){
            System.out.println("\nEnter the name of the flower: ");
            name = flowerSearch.nextLine();
            if (flowerPack[i] != null) {
            if (flowerPack[i].getName().equalsIgnoreCase(name)) {
               found = true;
               System.out.println("\nFlower was found at index " + i + ".");
               break;
            }
            }
         }
         if(options == 2){
            System.out.println("\nEnter the color of the flower: ");
            color = flowerSearch.nextLine();
            if (flowerPack[i] != null) {
            if (flowerPack[i].getColor().equalsIgnoreCase(color)) {
               found = true;
               System.out.println("\nFlower was found at index " + i + ".");
               break;
            }
            }
         }
         if(options == 3){
            System.out.println("\nFlowers with thorns or no thorns: YES or NO ");
            thorns = flowerSearch.nextLine();
            if (flowerPack[i] != null) {
            if (flowerPack[i].getThorns().equalsIgnoreCase(thorns)) {
               found = true;
               System.out.println("\nFlower was found at index " + i + ".");
               break;
            }
            }
         }
         if(options ==4){
            System.out.println("\nFlower with aroma or no aroma: YES or NO ");
            smell = flowerSearch.nextLine();
            if (flowerPack[i] != null) {
            if (flowerPack[i].getSmell().equalsIgnoreCase(smell)) {
               found = true;
               System.out.println("\nFlower was found at index " + i + ".");
               break;
                  }
                  }
         }

      if(!found){
        System.out.println("\nSorry, no match found.");      
      }

      }
   }

      //DISPLAY FLOWERS 
    private void displayFlowers(Flower[] flowerPack) {

      System.out.println("Flower Pack: \n");   
      Flower[] searchingPack = new Flower[flowerPack.length];
        for (int i = 0; i < searchingPack.length; i++) {    
                 searchingPack[i] = flowerPack[i];   

        }

      String flowerName = null;   
      int count = 0;   
         for(int i = 0; i < searchingPack.length; i++) {   
                i = 0;   
                flowerName = searchingPack[i].getName(); // this line is giving me an error!***********************************
                if (searchingPack[i].getName() == null || searchingPack[i].getName().equals(null)) {     
                break;

                }

                for (int j = 0; j < flowerPack.length; j++) {    
                     if (flowerPack[j] != null) {     
                        if (flowerPack[j].getName().equalsIgnoreCase(flowerName)) {       
                           count++;       
                           }     
                        }    
                     }

                      for (int k = 0; k < count; k++) {     
                           silentRemove(searchingPack, flowerName);    
                       }

                        System.out.println(flowerName + " - " + count);
                        System.out.println();    
                        count = 0;

                     }
                  }


      private void silentRemove(Flower flowerPack[], String flowerName) {   
         for (int i = 0; i < flowerPack.length; i++) {   
            if (flowerPack[i] != null) {     
               if (flowerPack[i].getName().equalsIgnoreCase(flowerName)) {    
                  for (int j = i; j < flowerPack.length - 1; j++) {       
                     flowerPack[j] = flowerPack[j + 1];      
                     }      

                     flowerPack[flowerPack.length - 1] = null;      
                     break;     
                     }   
                  }   
               }
            }
         }
2

There are 2 best solutions below

2
On

Does it need to be an array? A List<Flower> is more appropriate here. What is happening is you're only adding 2 flowers, but looping over all 25 elements in the array, some of which are still null

0
On

The problem is with

flowerName = searchingPack[i].getName();

because searchingPack[i] might be null and then you're trying to reach an address that doesn't exists.

do something like this:

if(searchingPack[i]==null)
    flowerName="";
else
   flowerName = searchingPack[i].getName();

If you are planning on doing the same kinds of actions you might consider putting an "empty" flower at each index in the array so it won't throw an error.