skip to Main Content

I have created a program, where I have the class dog, with many attributes, together with methods for creating an instance of the class, constructor and getting and setting the attributes.
Then I have class CPanel, which is supposed to work as a control panel for the user to access this class, it has array of objects dog and it lets the user allocate the objects into said array and find them, delete them and so on.

However I am having a problem with just said allocation as when creating a dog through said panel, one is supposedly created but right after its done the program continues to start the method Find/CheckDog via the dialogue "Choose dogs name"
I understand my code is rather imperfect however I have no clue as to how does the code continue there, also why does it crash right after providing the name of the dog

import java.util.Scanner;

public class friendlycode2 {
  Scanner scan = new Scanner(System.in);

  public class Dog{
    int id;
    boolean deleted;
    String name;
    String breed;
    int age;
    String color; //+standart get/set methods for these atributes
    
    public int GetId(){
        return id;
      }
    public boolean getDeleted(){
        return deleted;
      }
    public void setDeleted(boolean x){
        this.deleted = x;
      }
      public String getName(){
        return name;
      }  

  public Dog(int id, String name, String breed, int age, String color){
  this.name = name; this.breed = breed; this.age = age; this.color=color; this.deleted = false; 
  } 

  }
  public class CPanel{
      int nu2m;
      int Selected;
      Dog obj[ ]= new Dog[10];
  
      public CPanel(){
        System.out.println("Welcome to the dog games");       
        nu2m =0;
  }
  public void SetSelected(int x){
    this.Selected = x;
    }
    public int GetSelected(){ 
      return this.Selected;
    }
    public void CreateDog(){    //the important constructor method
        
        System.out.println("Choose dogs name, breed, age and color n"); 
        String userInput = scan.nextLine(); 
        String[] InputArray = userInput.split(",", 0);        
        obj[nu2m] = new Dog(nu2m, InputArray[0],InputArray[1],Integer.parseInt(InputArray[2]),InputArray[3]);        
        nu2m ++;   
        System.out.println("New dog created n");       
        }
  public void DeleteDog(){
    CheckDog();
    obj[Selected].setDeleted(true); 
  }
  public void ShowDogs(){
    for(int i=0;i<obj.length;i++)
    {
      if(obj[i].getDeleted()==false){
        obj[i].toString();}
       else{};
      }
    }  
  public int FindDog(){
    System.out.println("Choose dogs namen");
    String name = scan.nextLine();
    for(int i=0;i<obj.length;i++)
      {
        if(name ==obj[i].getName()){
          if(obj[i].getDeleted()==false){
        return obj[i].GetId();}
        }
        else{};
    }     
      return -1;
  }
  public void CheckDog(){
    int check = FindDog();
    if(check<0){System.out.println("Dog doesnt existn");}
    else{this.SetSelected(check);}    
      }
  }
    public void  mainn()  
  { 
    CPanel NewGame =new CPanel();
    String volba="0";
    System.out.println("Welcome to the dog gamen1: Create new dogn2: Delete a dogyn3:Show all dogsn4:Play the game dogsn5: Exitn");
    while(volba!="5"){
    volba = scan.nextLine();
    switch(volba){
    case("1"):
    NewGame.CreateDog();
    case("2"):
    NewGame.DeleteDog();
    case("3"):
    NewGame.ShowDogs();
    case("4"):
    System.out.println("Ending programn");
    //thegame(NewGame);
    }
    System.out.println("Welcome to the dog gamen1: Create new dogn2: Delete a dogyn3:Show all dogsn4:Play the game dogsn5: Exitn");
                    }
   }   
   public static void main(String[] args) {
 friendlycode2 o = new friendlycode2();
 //o.test();
 o.mainn();
  }
}

here is the output

Welcome to the dog game
1: Create new dog
2: Delete a dogy
3:Show all dogs
4:Play the game dogs
5: Exit

1
Choose dogs name, breed, age and color 

d,e,11,d
New dog created 

Choose dogs name

d
Exception in thread "main" java.lang.NullPointerException
        at friendlycode$CPanel.FindDog(friendlycode.java:104)
        at friendlycode$CPanel.CheckDog(friendlycode.java:114)
        at friendlycode$CPanel.DeleteDog(friendlycode.java:87)
        at friendlycode.mainn(friendlycode.java:159)
        at friendlycode.main(friendlycode.java:172)

2

Answers


  1. this is because some of your obj[i] is null.
    You need to check for null when iterating obj. e.g:

    for(int i=0;i<obj.length;i++){
      if(obj[i]==null){
        // do something here; either skip it or simply break.
      }
    
    Login or Signup to reply.
  2. Your case statement will run createDog immediately followed by deleteDog.
    It would be evident if you changed your prompts to
    ‘Enter dog name to be created’ or ‘Enter dog name to be deleted’.

    Modify your case statement by adding breaks:

    switch(volba){
        case("1"):
            NewGame.CreateDog();
            break;
        case("2"):
            NewGame.DeleteDog();
            break;
        case("3"):
            NewGame.ShowDogs();
            break;
        case("4"):
            thegame(NewGame);
            break; // not really required here
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search