Hi, can someone please help me, I am new to Java and I am getting a NullPointerException error and cant figure out why
Here is the code

class TestBirthday
{
public static void main(String[]args)
{
    int num;


    System.out.println("Enter number of people to test");
    num = Console.readInt();

    Birthday2 Experiment = new Birthday2(num);

    System.out.println("The number of duplicates when testing " +num+ " people is "+ Experiment.countDuplicates());


}


}

class Birthday2

{   
    private int num;
    private int[]birthday;


public Birthday2(int number)
{
    num = number;

    int[]birthday = new int[num];

}


public int countDuplicates()
{

    boolean duplicate = false;
    int index = 0;
    int dup = 0;
    int day;


    while (index < num)
    {   

        day = 1 + (int)(Math.random()*365); 

        {
            duplicate = searchBirthdays(day,index);
            if (duplicate)
            dup ++;
        }               

        birthday[index] = day;      
        index ++;

    }//end while

    return dup;

}//end selectBirthday

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Apologies, I made a mess of posting that code
Here it is again

class TestBirthday
{
public static void main(String[]args)
{
	int num;
	
	System.out.println("Enter number of people to test");
	num = Console.readInt();

	Birthday2 Experiment = new Birthday2(num);
	
	System.out.println("The number of duplicates when testing " +num+ " people is "+ Experiment.countDuplicates());
	
} 
	  
}
 
class Birthday2

{	
	private int num;
	private int[]birthday;
	
	
public Birthday2(int number)
{
	num = number;

    int[]birthday = new int[num];

}
	
public int countDuplicates()
{    
	boolean duplicate = false;
	int index = 0;
	int dup = 0;
	int day;
		
	
	while (index < num)
	{	
		
		day = 1 + (int)(Math.random()*365);	
						
		{
			duplicate = searchBirthdays(day,index);
			if (duplicate)
			dup ++;
		}				
		
		birthday[index] = day;		
		index ++;
		
	}//end while
	
	return dup;
			
}//end countDuplicates

	
private boolean searchBirthdays(int bday, int end)
{
	boolean found = false;
	int index = 0;
	
	while ((index < end) && (!found))
	  {
	  	if(bday == birthday[index])
	  	
	  	found = true;
	  	index ++;
	  }//end while
	  
	return found;  
	
}
	
}//end class Birthday2

I would be interested how possible you got that error as the code as submitted will not compile because first of missing import statement/s and secondly there is no readInt() method in Console class.
So please sort out your code if you do not want to look like one of these students that try to trick us into finishing their assignment/homeworks

Here is the code for that class again

import java.util.*;

class TestBirthday
{
public static void main(String[]args)
{

	Scanner sc = new Scanner(System.in);
	
	int num;
	
	System.out.println("Enter number of people to test");
	num = sc.nextInt();

	Birthday2 Experiment = new Birthday2(num);
	
	System.out.println("The number of duplicates when testing " +num+ " people is "+ Experiment.countDuplicates());
	
}	  
	  
}

Still it will not compile as this section in Birthday2 does not make any sense

while (index < num)
	{	
		
		day = 1 + (int)(Math.random()*365);	
						
		{
			duplicate = searchBirthdays(day,index);
			if (duplicate)
			dup ++;
		}				
		
		birthday[index] = day;		
		index ++;
		
	}//end while

Thanks for your reply, I am not seeing where that is wrong, I am trying to count the number of duplicates, therefore calling the searchBirthdays method, can you give me another clue, I am staring at this for the last day and a half and hitting a brick wall!!

One there is no such method as searchBirthdays(). What is purpose of these curly brackets before calling above method? I think they are are some forgotten artefact

sorry I messed up the first post, the code for searchBirthdays is in the second post

You made double declaration

class Birthday2

{	
	private int num;
	private int[]birthday;
	
	
public Birthday2(int number)
{
	num = number;

    int[]birthday = new int[num];

}

in doing so birthday array is not accessible by other methods. Simple change need it

class Birthday2

{	
	private int num;
	private int[]birthday;
	
	
public Birthday2(int number)
{
	num = number;

	birthday = new int[num];

}
This article has been dead for over six months. Start a new discussion instead.