Home > OS >  Java: after constructor method is run, method for finding an object is run as well despite not being
Java: after constructor method is run, method for finding an object is run as well despite not being

Time:09-15

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

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

  public class Dog{
    int id;
    boolean deleted;
    String name;
    String breed;
    int age;
    String color;
    int hp;
    int attack;

  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.hp =100; this.attack = 11; this.deleted = false; 
  }
  public int GetId(){
    return id;
  }
  public boolean getDeleted(){
    return deleted;
  }
  public String getName(){
    return name;
  }
  public String getBreed(){
    return breed;
  }
  public int getAge(){
    return age;
  }
  public String getColor(){
    return color;
  }
  public void setDeleted(boolean x){
    this.deleted = x;
  }
  public String toString()
  {
      return("Hi my name is "  this.getName() 
            ".\nMy breed,age and color are "  
            this.getBreed() ","   this.getAge() 
            ","  this.getColor()  " AND he has " this.hp  " hp, and " this.attack " damage\n");
  }
  public void attack(Dog x){ 
    System.out.println(this.name   " is attacking "   x.name   "\n" 
      this. name  " has "   this.hp  " hp and " this.attack   " attack\n"
      x.name   " has "   x.hp   " hp and "   x.attack   " attack\n");
  x.hp = x.hp - this.attack;
  System.out.println(x.name  " has suffered "  this.attack  " damage and now has "  x.hp   "hp\n" );
  }
  }
  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(){ //what it continues into
    System.out.println("Choose dogs name\n");
    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 exist\n");}
    else{this.SetSelected(check);}
    
  }
  public void Attack(){ 
    int selected1=this.GetSelected();
    System.out.println("Who are you attacking? :");
    this.CheckDog();
    int selected2 = this.GetSelected();
    obj[selected1].attack(obj[selected2]);

  }
  public void run6(){
 }
  }

   public void thegame(CPanel NewGame)
   { 
    String volba2 ="0";
    System.out.println("Welcome to the dog fight\n1: Select your dog\n2: Attack\n3:Play\n4: Leave\n");
    while(volba2!="4"){
      volba2 = scan.nextLine();
      switch(volba2){
      case("1"):
      NewGame.FindDog();
      case("2"):
      NewGame.Attack();
      case("3"):
      NewGame.run6();
      }
    }
    System.out.println("Ending game\n");
    }
    public void  mainn()  
  { 
    CPanel NewGame =new CPanel();
    String volba="0";
    System.out.println("Welcome to the dog game\n1: Create new dog\n2: Delete a dogy\n3:Show all dogs\n4:Play the game dogs\n5: Exit\n");
    while(volba!="5"){
    volba = scan.nextLine();
    switch(volba){
    case("1"):
    NewGame.CreateDog();
    case("2"):
    NewGame.DeleteDog();
    case("3"):
    NewGame.ShowDogs();
    case("4"):
    thegame(NewGame);
    }
    System.out.println("Ending program\n");
                    }
   }
   
   public static void main(String[] args) {
 friendlycode o = new friendlycode();
 //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)

CodePudding user response:

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.
  }

CodePudding user response:

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
}
  • Related