Member is class consisted of member informaiton such as firstname, lastname, phone etc.

would anyone please point out what this function gave me NullPointerException runtime error? I think I initialized everything.

thanks

public static void read(Member box[], int count)
	{
		Scanner reader = null;
		
		try
		{	
			reader = new Scanner(new FileReader("database.txt"));
			
			int id; 
			String type, fn, ln, ph;
			Double fee;
			
			while(reader.hasNextLine())
			{
				id= reader.nextInt();
				type= reader.next();
				fn= reader.next();
				ln= reader.next();
				ph= reader.next();
				fee= reader.nextDouble();
				
				System.out.println(count);
				box[count]= new Member(id, type, fn, ln, ph, fee); 
				box[count].output();
				//count++;
				
			}
			
	
		}
		
		catch(FileNotFoundException e)
		{
			System.out.println("File not Found!");
		}
		
		catch(IOException e)
		{
			System.out.println("Error reading");
		}
		
		catch(NoSuchElementException e)
		{
			System.out.println("done reading");
		}
	
	}

I expect that argument count is related to Member array box and is holding the actual size of the array ( correct me if I'm wrong)
Then you try to read file and store retrieved data in the box array.
You try to insert first set of data into array position of count.
However as count hold the array size the variable already exceeds array boundaries as Array.size() returns number of elements (lets say 20), however last element position is actually equal to count-1 ( that will be 19 as arrays starts from zero unlike vector for example). This kick off your NullPointerException.

If your method is supposed to read a file and return retrieved data you would be better of with method that will return the array then try to locally fill in and then re-use it.

Unfortunately you may not always know amount of data to be read so you should use something more "dynamic" like ArrayList or Vector. I re-do you read method little so now instead of void you get some return therefore to call it do as follows

ArrayList<Member> boxReturn = read();

and here is update read method

public static ArrayList<Memeber> read()
	{
		ArrayList<Member> box = new ArrayList<Member>();
		Scanner reader = null;
		
		try
		{	
			reader = new Scanner(new FileReader("database.txt"));
			
			int id; 
			String type, fn, ln, ph;
			Double fee;
			
			while(reader.hasNextLine())
			{
				id= reader.nextInt();
				type= reader.next();
				fn= reader.next();
				ln= reader.next();
				ph= reader.next();
				fee= reader.nextDouble();
				
				System.out.println(count);
				box.add( new Member(id, type, fn, ln, ph, fee)); 				
				//count++;				
			}	
		}
		
		catch(FileNotFoundException e)
		{
			System.out.println("File not Found!");
		}
		
		catch(IOException e)
		{
			System.out.println("Error reading");
		}
		
		catch(NoSuchElementException e)
		{
			System.out.println("done reading");
		}
		finally
		{
			reader.close();
		}
		
		return box;	
	}

PS: Don't forget to close your Scanner

peter_budo,
eventhou i never learned array list, it make sense to me using it. However, if i do know how many member are there? or if i want to assume there are 300 in the array and let it fill up (300 is a number that is way more than the total members) then could you suggest a way fixing my original code? i am just trying to see what went wrong and the solution of the nullpointerexception error. thanks

Any exception in Java comes with a stack trace. Try to read the entire exception trace and see on which line things are going wrong. If you are using an IDE like Eclipse, use the debugging feature to inspect the values at run time. If you are using a normal text editor, look into the command line debugger JDB. BTW, have you allocated memory to the Member array box?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.