I am getting a NullPointerException in the statement which I've hidden. Can some1 tell me why is it so ?

import java.io.*;


class College
{
	String branch,name = new String();
	int avg_marks,rno;
	
	
	InputStreamReader in= new InputStreamReader(System.in);
	BufferedReader buf= new BufferedReader(in);
	String s=new String();

	void read()throws IOException
	{
	  System.out.println("Enter name");
	  name=buf.readLine();

	  System.out.println("Enter roll no.");	
          s=buf.readLine();

	  rno=Integer.parseInt(s);
	  System.out.println("Enter branch");
	  branch=buf.readLine();

	  System.out.println("Enter average marks [out of 100] ");
	  s=buf.readLine();
	  avg_marks= Integer.parseInt(s);

	}

	void display()
	{
	
	  System.out.println("NAME:          "+name);  
	  System.out.println("ROLL NO:       "+rno);  
	  System.out.println("BRANCH:        "+branch);  
	  System.out.println("AVERAGE MARKS: "+avg_marks);
	}
}  


class CRCE
{
	public static void main(String args[])throws IOException
	{
		
		InputStreamReader in= new InputStreamReader(System.in);
		BufferedReader buf= new BufferedReader(in);
		String s=new String();

	
		int i;
		System.out.println("Enter total number of students in college");
		s=buf.readLine();
		int n=Integer.parseInt(s);
		
		College c[]=new College[n];
		
		for(i=0;i<n;i++)
		
		c[i].read();
		
		for(i=0;i<n;i++)
		{
		    if(((c[i].branch.compareToIgnoreCase("COMPUTER"))==0)||(c[i].avg_marks>60))
			{
			  c[i].display();
			}
		}
	}
}

The exception occurs in line 62

You declared an array of "n" College Objects, but you did not initialize it. You must create a new Object at each index before you can call methods on it. Also, post a regular thread in the future, not a code snippet.

You declared an array of "n" College Objects, but you did not initialize it. You must create a new Object at each index before you can call methods on it. Also, post a regular thread in the future, not a code snippet.

Sorry to say, but I didn't quiet get what you are trying to say

u got nothing in the array..
but u trying to do something with the things inside the array

College c[]=new College[n];

for(i=0;i<n;i++)
c.read();

what is c?

u got nothing in the array..
but u trying to do something with the things inside the array

College c[]=new College[n];

for(i=0;i<n;i++)
c.read();

what is c?

c is an array of objects and I'm trying to read data into the objects by the read() method

No, c is an array of objects. c is the object which is null because you haven't initialized it. c is null, c is a College object, c is an array

No, c is an array of objects. c is the object which is null because you haven't initialized it. c is null, c is a College object, c is an array

ok..but I'm trying to initialise c using the read(); is there something wrong with that ?

well if you want to insert values try this

c[i]="hello";

or if you are trying to get the value from the user try this

Scanner c = new Scanner(System.in)
c[i]=c.nextXXX();//xxx can be replaced with any datatype like int, String etc...

well if you want to insert values try this

c[i]="hello";

or if you are trying to get the value from the user try this

Scanner c = new Scanner(System.in)
c[i]=c.nextXXX();//xxx can be replaced with any datatype like int, String etc...

But, I'm doing the same thing; instead of using scanner class, I'm using InputStreamReader. Are you suggesting that I take input within the main function and not in the other class ?

ok..but I'm trying to initialise c using the read(); is there something wrong with that ?

How are you going to call the read() method if the c doesn't exist!
Study how object are initialized. Obj o = new Obj()

you must do this befor you try to read in the array

for(int i = 0 ; i < c.length() ; i++)
c = new Collage() ;

check out line no. 55. you will require a similar statement to read data from the user. its not necessary to take the input in the main function

sorry

for(int i = 0 ; i < c.length() ; i++)
c = new Collage() ;

<<< Study how object are initialized.

The best suggestion yet. As was said previously, you cannot call a method without an Object of the corresponding class. The only exception to this rule is when the method is declared as 'static'. You need to understand that there is a distinction between declaring and initializing. You declared an array of "n" College Objects. All this does is tell Java to create an empty container that can hold "n" Objects. To initialize a variable means to put something into it. So to initialize your array would mean to fill the slots of the array with College Objects. Initially, your declaration of College[] array = new College[n]; causes Java to create an array of size n with the value "null" in each slot of the array. Attempting to reference (call a method on) null gives you a NullPointerException, hence your error initially. If this info is a lot right now, that's fair, just remember that you must say "new Object()" before you are allowed to call a method on an index of an array or on any variable. It may help if you think about an array as a collection of variables of the same type (such as College), and then think of a variable as only being able to call methods after you say yourVariable = new College().

You don't initialize c by calling its methods. You need first to initialize it then call them. c is null, you only created the array, not its elements:

c = new College()

Hi
Made this change to avoid the ewception

for (i = 0; i < n; i++) {
            c[i] = new College();
            c[i].read();
        }

hope it helps.

nono =.=" thats not ur problem..
what is happening
u made an array of College object, that can hold n-items. thats all you did.

is this what you trying to do?
[IMG]http://i40.tinypic.com/1z6y53t.jpg[/IMG]

if yes, you need a constructor, read should be a static object(in ur case), need a College return type not void,check your private fields...it is so messy~~

one last time.. if u try to store an object in the array.. it is c = read();

Thank you every1 for your valuable inputs

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.