Ver Mensaje Individual
  #3 (permalink)  
Antiguo 19/02/2014, 09:48
SilverDante
 
Fecha de Ingreso: diciembre-2013
Mensajes: 36
Antigüedad: 10 años, 4 meses
Puntos: 1
Pregunta Respuesta: Insertar elemento creado en una clase desde otra.

Ahora el código lo tengo así: (¿porque me falla?)
Mensaje de error
Exception in thread "main" java.lang.NullPointerException
at zoo.Zoologico.anyadeAnimal(Zoologico.java:39)
at zoo.Aplicacion.main(Aplicacion.java:52)
Java Result: 1


Aplicación
Código:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package zoo;

import java.util.ArrayList;
import java.util.Scanner;

/**
 *
 * @author Joan
 */
public class Aplicacion {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner teclado=new Scanner(System.in);
        
        Zoologico zoo= new Zoologico();
        ArrayList <Animal> Bestario=new ArrayList<Animal>();
        
        int op=-1, edad;
        String nombre, pais;
        double peso;
        
        /*--------MENU--------*/
        while (op!=0)
        {
            
            do{

System.out.println("\n"+"        MENU        ");
verMenu();
op=teclado.nextInt();


switch(op){

                case 1: Animal a= new Animal();
                
                        /*System.out.println("Introduce el nombre.");
                        a.nombre=teclado.next();
                        System.out.println("Introduce el pais.");
                        a.pais=teclado.next();
                        System.out.println("Introduce el peso.");
                        a.peso=teclado.nextDouble();
                        System.out.println("Introduce la edad.");
                        a.edad=teclado.nextInt();*/
                        zoo.anyadeAnimal(a);
                        break;

                case 2: zoo.mostrarAnimales(Bestario);
                        break;

                case 3: ;
                        break;

                case 4: ;
                        break;

                case 0: System.out.println("Se ha cerrado el programa.");
                        break;
    
default: System.out.println("Error" ); break;

}
}while(op!=0);
            
    }
        
    }
    public static void verMenu(){
         System.out.println("1.Añadir  Animal.");
         System.out.println("2.Borrar  Persona");
         System.out.println("3.Buscar  Persona");
         System.out.println("4.Mostrar Persona");
         System.out.println("0.Salir");
         
   }
}
Zoológico
Código:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package zoo;
import java.util.*;

/**
 *
 * @author Joan
 */
public class Zoologico {
    //Vector v = new Vector (3/1);
    ArrayList <Animal> Bestario;
    
    public Zoologico(){
    }
    
    public void anyadeAnimal(Animal a){
        Animal ficha;
        ficha=new Animal();
        //v.addElement(a);
        
        Scanner leer = new Scanner(System.in);

        System.out.println("Introduce su nombre: ");
        ficha.nombre=leer.nextLine();
        
        System.out.println("Introduce su país de procedencia: ");
        ficha.pais=leer.nextLine();
        
        System.out.println("Introduce su peso: ");
        ficha.peso=leer.nextDouble();
        
        System.out.println("Introduce su edad:");
        ficha.edad=leer.nextInt();

        
        Bestario.add(ficha);
    }
    
    public static void mostrarAnimales(ArrayList <Animal> Bestario){
        int i=0;

        Iterator it = Bestario.iterator(); //Porque utilizar esto --> http://goo.gl/EfvDx0
        if (!it.hasNext())
            System.out.println("No Hay Contactos en La Agenda."+"\n");
        else
         while (it.hasNext())
         {
            it.next();
            System.out.println("Contacto: " +(i+1)                      +"\n"+
                               "Nombre:   " +Bestario.get(i).nombre    +"\n"+
                               "Pais: " +Bestario.get(i).pais +"\n"+
                               "Peso: " +Bestario.get(i).peso +"\n"+
                               "Edad: " +Bestario.get(i).edad  +"\n\n");
            i++;
         }
    }
    
}
Animal:
Código:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package zoo;

/**
 *
 * @author User
 */
public class Animal {
    
    String nombre, pais;
    double peso;
    int edad;
    
    public Animal(){
        this.nombre="";
        this.pais="";
        this.peso=0.00;
        this.edad=0;
    }
    
    public Animal(String nom, String pais, double peso, int edad){
        this.nombre=nom;
        this.pais=pais;
        this.peso=peso;
        this.edad=edad;
    }
    
    /*----------GETS-SETS----------*/

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getPais() {
        return pais;
    }

    public void setPais(String pais) {
        this.pais = pais;
    }

    public double getPeso() {
        return peso;
    }

    public void setPeso(double peso) {
        this.peso = peso;
    }

    public int getEdad() {
        return edad;
    }

    public void setEdad(int edad) {
        this.edad = edad;
    }
    /*------------------------------------------------------------------------*/
    
}