0

I'm making a animal farm with each pigs, cows and horses has it's own name.

I choose which place of the array the animal will be placed, and then types the name.

But i can't print the array correctly.

How is it done?

Here is the error:

Exception in thread "main" java.lang.NullPointerException
at Bondegard.main(Bondegard.java:58)

I have only tried with one type of animal, so the other two is not done yet.

This is my 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<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<grisebinge.length; i++)
        {
            System.out.println(grisebinge[i].getGris());
        }   
    }
}

and my Gris class is

public class Gris {

     private String grisHeter;

     public String getGris()
     {
         return grisHeter;
     }

     public void setGrisNavn(String m)
     {
         grisHeter=m;
     }

}
4
  • where is Bondegard.java:58? Commented Sep 30, 2014 at 20:35
  • System.out.println(grisebinge[i].getGris()); Commented Sep 30, 2014 at 20:36
  • in grisebinge[i].getGris(), if grisebinge[i] is null, then you can't call the method getGris(). You need to check if this object is null or not before trying to call the method Commented Sep 30, 2014 at 20:45
  • Sidenote: Currently, the loop in your settInnGris()-method will stop if the user enters 10 taken indices in a row. If you want it to keep repeating until the he enters a vacant index, you should remove the condition i < grisebinge.length. But if you do that, you should probably also notify the user that there are no more vacant places left if he tries to add an 11th pig. (To avoid an infinite loop) Commented Sep 30, 2014 at 20:59

4 Answers 4

3

You only add 5 pigs to grisebinge, so the remaining 5 elements are still null. When you are trying to print their names at the end, you are trying to call getGris() on all 10 elements in grisebinge, even those that are null.

Calling a method on null will result in NullPointerException.

Sign up to request clarification or add additional context in comments.

Comments

1

Your array contains null elements so when you try to call the method getGris() on the null element you get the exception.

Try changing for printing loop from this:

for(int i=0; i<grisebinge.length; i++)
{
    System.out.println(grisebinge[i].getGris());
}   

To this:

for(int i=0; i<grisebinge.length; i++)
{
    Gris g = grisebinge[i];
    if( g!= null)
        System.out.println(g.getGris());
}

However this will only mask the fact you have null elements in the array (which is fine for the last elements that are not set), but looking at another comment you made after printing using Arrays.toString() you have the first element as null. You should look carefully at your logic that is putting the elements in the array also. (But I now see that the user can specify at which index to put it)

Comments

1

Well, if what you want to do is print all elements of the array:

Arrays.toString( array );

See http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

3 Comments

when I do that, I get [null, Gris@7d4991ad, null, null, null, null, null, null, null, null]
That's good! So at index 1 you've got a Gris! The other elements of the array have not been given any animals yet :) What do you expect?
@Candy Then implement a toString method into the class Gris. Your IDE should provide a feature to generate it.
0

As per my understanding based on your provided code, you are trying to say enter at which index you want to add the Gris object like - System.out.println("Hvor vil du sette in grisen? (0-9) ");

As in your main method you are calling settInnGris(g); five time. so I would say 1 when your code is asking for System.out.println("Hvor vil du sette in grisen? (0-9) "); and later case 3,5,2,4

So what is happening in that case Gris object is going to add in that index(1,3,5,2,4) position in array as I have entered these values but at the time below for loop - for(int i=0; i

this loop will iterate till grisebinge.length and you are adding few gris (less than you collection length and not sure which index position you are adding) object in your collection. That's why you are getting null pointer exception in you main class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.