title: How can I print out objects from array?
body: <p>I'm making a animal farm with each pigs, cows and horses has it's own name.</p>
  
  <p>I choose which place of the array the animal will be placed, and then types the name.</p>
  
  <p>But i can't print the array correctly.</p>
  
  <p>How is it done?</p>
  
  <p>Here is the error:</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
  at Bondegard.main(Bondegard.java:58)
  </code></pre>
  
  <p>I have only tried with one type of animal, so the other two is not done yet.</p>
  
  <p>This is my code:</p>
  
  <pre><code>import java.util.Scanner;
  
  public class Bondegard 
  {
       static Gris[] grisebinge = new Gris[10];
       static Hest[] stall = new Hest[5];
       static Ku[] fjos = new Ku[30];
  
       public static void settInnGris(Gris g)
       {
           Scanner in = new Scanner(System.in);
           System.out.println("Hvor vil du sette in grisen? (0-9) ");
           int index = in.nextInt();
  
           for(int i=0; i&lt;grisebinge.length; i++)
           {
               if(grisebinge[index] != null)
               {
                   System.out.println("Plassen er opptatt");
                   index = in.nextInt();
               }
               else if(grisebinge[index] == null)
               {
                    System.out.println("Hva heter grisen din?");
                    Scanner inn = new Scanner(System.in);
                    String temp = inn.nextLine();
                    grisebinge[index]=g;
                    grisebinge[index].setGrisNavn(temp);
                    break;
               }
           }
       }
  
      static void settInnHest(Hest h)  {  }
      static void settInnKu(Ku k) {  }
  
      public static void main(String[]args)
      {
           Gris g = new Gris();
          settInnGris(g);
          settInnGris(g);
          settInnGris(g);
          settInnGris(g);
          settInnGris(g);
          for(int i=0; i&lt;grisebinge.length; i++)
          {
              System.out.println(grisebinge[i].getGris());
          }   
      }
  }
  </code></pre>
  
  <p>and my Gris class is</p>
  
  <pre><code>public class Gris {
  
       private String grisHeter;
  
       public String getGris()
       {
           return grisHeter;
       }
  
       public void setGrisNavn(String m)
       {
           grisHeter=m;
       }
  
  }
  </code></pre>