Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6857937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:05:41+00:00 2026-05-27T02:05:41+00:00

Hi i get a null pointer where marked in the process. The tester class

  • 0

Hi i get a null pointer where marked in the process. The tester class is at the bottom. It happens when trying to printout the zoo.
It seems to be the no name is set for “Animal animal” but I have created animals.

It could be accessing the wrong animal i want, but then how do I access the animals in the list I’ve made (please no using the “:” in the for parameter)!
i know this is wrong but something like animal_list.printdetails?

 public class Zoo { 
    private Animal animal;  
    public int number_animals;//# animals allowed in zoo
    private List<Animal> animal_list;
    private String zoo_name;


public Zoo(String name){
    this.zoo_name = name;
    animal_list = new ArrayList<Animal>();
}

 public void addAnimal(Animal obj) {
        animal_list.add(obj);
 }
 public String toString(){
    String s = "In zoo " + zoo_name + " there are the following animals:\n";
    for (int i = 0; i < animal_list.size(); i++){
        s += animal.getName() + " who weighs " + animal.getWeight() + " kg.";//null pointer on this line why??? i have made an animal. How do I access this animals in the list (please no using the ":" in the for parameter)!
    }
    return s;
 }



public class Animal {

public String name;
public int weight;
private String food_type;
private int space_requirement;
private Zoo zoo;
private Animal animal;  

public Animal(String name, int weight){
    this.name = name;
    this.weight = weight;
}    
public String getName(){
    return this.name;
}
public int getWeight(){
    return this.weight;
}



public class Test {
public static void main(String[] args) {        
    Zoo diego = new Zoo("San Diego");
    Animal giffy = new Animal("Giffy", 950);
    Animal gunther = new Animal("Gunther", 950);    
    diego.addAnimal(giffy);
    diego.addAnimal(gunther);
    System.out.println(diego);

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:05:41+00:00Added an answer on May 27, 2026 at 2:05 am
    for (int i = 0; i < animal_list.size(); i++){
        s += animal.getName() + " who weighs " + animal.getWeight() + " kg.";
    }
    

    Because you aren’t using your List, you’re trying to reference the animal field in your Zoo (which you don’t actually use anywhere). You have to get your Animals from your animal_list

    for (int i = 0; i < animal_list.size(); i++){
        s += animal_list.get(i).getName() + " who weighs " + 
             animal_list.get(i).getWeight() + " kg.";
    }
    

    Note also that you really should be using a StringBuilder here rather than creating new String objects with the += and + operators:

    public String toString() {
        StringBuilder s = new StringBuilder("In zoo "); 
        s.append(zoo_name).append(" there are the following animals:\n");
        for (int i = 0; i < animal_list.size(); i++){
            s.append(animal_list.get(i).getName());
            s.append(" who weighs ");
            s.append(animal_list.get(i).getWeight());
            s.append(" kg.\n");
        }
        return s.toString();
    }
    

    Strings are immutable in Java; you can’t alter them. When you concatenate them using += or + you’re actually creating new String objects and discarding the old ones. The compiler will actually optimize this away where it can to a StringBuilder, but good practice is not to leave it to the compiler.

    Edit to add:
    In case you’re not familiar, the above is an example of method chaining

    When you say something like:

    String name = animal_list.get(i).getName();
    

    It’s the equivalent of:

    Animal a = animal_list.get(i);
    String name = a.getName();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was just trying to figure out if I could get a NULL pointer
I'm trying to get a reference to cell and it appears null. If I'm
I get null pointer exception at line mService.start() when i try to bind to
When I try to use java.lang.System.console(), I get a null pointer. I can still
I get a null pointer exception when a call a method on a custom
Some times when running my app I get a null pointer when retrieving a
Possible Duplicate: Why do I get a null pointer exception from TabWidget? I have
I'm trying to set the lat/lng values for the item marked <device:GeoCoordinate... dynamically. I
When invoking this helper from another cshtml I get a Null Pointer Exception on
I'm having trouble reproducing a bug where I get a null pointer exception when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.