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 5 of 5

Thread: Null Pointer Exception. Why?

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

    Default Null Pointer Exception. Why?

    I have the following code and keep getting a null pointer exception at the last line of this code. I cannot figure out why. If anyone can help me out it would be appreciated

    This is my class where it messes up. It messes up when trying to load the temp variable into the matchups array list. I'll also show the part in main where I use it below

    package generator;
     
    import java.util.ArrayList;
    import java.util.Random;
     
    import matchups.Matchups;
    import teams.Teams;
     
     
    public class Generator {
     
    	private ArrayList<Matchups> matchups;
    	private ArrayList<Teams> usedTeams;
    	Random rand = new Random();
    	Matchups temp;
    	private int id;
    	int i;
     
    	public Generator(ArrayList<Matchups> list, int num){
    		this.id = num;
    		temp = list.get(rand.nextInt(list.size()));
    		matchups.add(temp);
    		usedTeams.add(temp.getTeamOne());
    		usedTeams.add(temp.getTeamTwo());
    		for(i = 0; i < 4; i++){
    		do{
    				temp = list.get(rand.nextInt(list.size()));
    			}while(usedTeams.contains(temp.getTeamOne()) || usedTeams.contains(temp.getTeamTwo()));
    			usedTeams.add(temp.getTeamOne());
    			usedTeams.add(temp.getTeamTwo());
    			matchups.add(temp);
    		}
     
     
    	}
     
    	public int getId(){
    		return this.id;
    	}
    	public ArrayList<Matchups> getWeek(){
    		return this.matchups;
    	}
    }


    Main:
    	Matchups current;
    		ArrayList<Matchups> matchups = new ArrayList<Matchups>();
    		for(i = 0; i < 10; i++){
    			for(j = i + 1; j < 10; j++){
    				matchups.add(new Matchups(teams.get(i), teams.get(j)));
    			}
    		}
     
     
     
    		System.out.println("WEEK ONE");
    		Generator weekOne;
    		weekOne = new Generator(matchups, 1);


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,606
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Null Pointer Exception. Why?

    keep getting a null pointer exception at the last line of this code.
    You get a NullPointerException because a variable has a null value. Look at the line where the error happens, find the variable with the null value and backtrack in the code to see why the variable does not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Null Pointer Exception. Why?

    Quote Originally Posted by Norm View Post
    You get a NullPointerException because a variable has a null value. Look at the line where the error happens, find the variable with the null value and backtrack in the code to see why the variable does not have a valid value.
    I just printed all the information from temp and it prints out all the correct information.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,606
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Null Pointer Exception. Why?

    it prints out all the correct information.
    Does that mean the problem is solved now?

    Can different values from Random change how the code executes?
    Try executing it several times.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Null Pointer Exception. Why?

    It is now. I didn't initialize the ArrayLists. Thanks for the help

Similar Threads

  1. Null Pointer Exception
    By kendraheartt in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 15th, 2012, 10:33 PM
  2. Need Help with Null Pointer Exception
    By kendraheartt in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 23rd, 2012, 02:20 PM
  3. [SOLVED] Null Pointer Exception
    By wltrallen2 in forum Object Oriented Programming
    Replies: 7
    Last Post: May 27th, 2012, 10:21 AM
  4. Help with null pointer exception
    By Dr.HughMan in forum Exceptions
    Replies: 35
    Last Post: November 30th, 2011, 08:00 PM
  5. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM