简体   繁体   中英

How can I print out objects from array?

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

}

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 .

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

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM