Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Array List Help - Can't find where problem is!!

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array List Help - Can't find where problem is!!

    Hi, I'm having the following error with the below code, and I have no idea how to fix it. Any suggestions would be greatly appreciated.

    Exception in thread "main" java.lang.NullPointerException
    at Experiment.runExperimentOnce(Experiment.java:29)
    at Experiment.runExperimentManyTimes(Experiment.java: 37)
    at PT5.main(PT5.java:24)



    import java.awt.Color;
    import java.util.ArrayList;
     
    public class Experiment {
    private Aviary av;
    private int[] results = new int[11];
     
    public Experiment (Aviary x) {
    x = av;
    }
     
    public void printResults() {
    for(int i=0; i<=11; i++){
    System.out.println(results[i]);
    }	
    }
     
    public int howManyRed(ArrayList<Bird> x) {
    int count = 0;
    for(Bird i : x) {
    if (i.getMyColor()==Color.red) {
    count++;
    }
    }
    return count;
    }
     
    public void runExperimentOnce() {
    results[howManyRed(av.see10Birds())]++;
    }
     
    public void runExperimentManyTimes(int w) {
    int x = w;
    int avg = 0;
    results = new int[11];
    for(int i = 0; i<=x; i++) {
    runExperimentOnce();
    avg = avg + results[i];
    }
    System.out.println(1.0 * avg + " red birds out of 10");
    for(int i = 0; i<=x; i++) {
    int maxindex = results[0];
    if (results[i]> maxindex){
    maxindex = results[i];
    }
    }
    }
     
    }
    Last edited by helloworld922; February 21st, 2011 at 10:55 PM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Array List Help - Can't find where problem is!!

    My guess is that your Aviary class contains an ArrayList<Bird> that has not been initialized.

    OR

    The Aviary av variable is null.


    Most likely the latter. Those are the only two suspects that I would consider to throw a null pointer in this situation.

    Also, please put code tags around your code so it is more readable.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Array List Help - Can't find where problem is!!

    public Experiment (Aviary x) {
    x = av;
    }
    I think you have this switched around? Java takes the value on the right and assigns it to the one on the left.

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array List Help - Can't find where problem is!!

    Hi, I switched around the variables in my constructor, setting av = x;, instead of x = av;.

    I'm not sure if that makes a difference but now I am getting an array out of bounds error, which I understand means its trying to access an array position that doesn't exist.

    Here is the error.


    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11
    at Experiment.runExperimentManyTimes(Experiment.java: 38)
    at PT5.main(PT5.java:24)



    I don't see what the issue is, because the code should accept the length of the array of the array as a parameter and test it up to that many times.

    Any additional help would be great, thanks!
    Last edited by helloworld922; February 22nd, 2011 at 12:06 AM.

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array List Help - Can't find where problem is!!

    Yes, I flipped that. But now I'm struggling to do these for loops. I need them to run through the array x number of times, x being the int parameter going into the method runExperimentManyTimes. I need to be able to check the color of however many birds come into the method with the x parameter. But I need to access the results array only between 0 and 10. Putting i from the for loop into the for results parameter is causing the error when the loop tries to run more than 10 times, as it has to when the experiment is run 100000 times.

    Any ideas?

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Array List Help - Can't find where problem is!!

    edit: nvm, I didn't look closely at the code. Editing and coming up with the new solution now

    edit2: here's the new solution:

    You shouldn't be summing up the average while running the experiment x number of times, but rather at the end. Same thing goes for the finding which bird count came up the most often.

    // this goes after you've run the experiment x number of times
    for(int i = 0; i <= 10; ++i)
    {
        // TODO: sum up the total number of red birds seen
        // TODO: find the max index in results
    }
    // TODO: using the sum, calculate the average
    Last edited by helloworld922; February 22nd, 2011 at 12:13 AM.

Similar Threads

  1. Array List Doubt
    By mhnganesh in forum Collections and Generics
    Replies: 2
    Last Post: July 12th, 2010, 10:21 AM
  2. how to find an element in an array
    By humdinger in forum Collections and Generics
    Replies: 8
    Last Post: April 9th, 2010, 05:22 PM
  3. FAQ: find variable in an array of objects
    By dalek in forum Object Oriented Programming
    Replies: 1
    Last Post: April 5th, 2010, 12:40 PM
  4. Replies: 1
    Last Post: March 15th, 2010, 10:03 PM
  5. Replies: 1
    Last Post: November 22nd, 2008, 01:32 PM