Click here to Skip to main content
15,896,489 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
package learn;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;

public class ClassList {
	int rollNo;
	 String name;

	void toEnter(int rollNo, String name1) throws IOException{
		if(name1!=null){

			this.name = name1;
			this.rollNo = rollNo;	
		}
		else 
			System.out.println("Name is null");
		System.exit(0);
	}

	public static void main(String[] args) throws IllegalArgumentException,
			IOException {
		// TODO Auto-generated method stub
		ClassList[] cobjt = new ClassList[10];
		int num = 0;
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Enter the number");
		try {
			num = Integer.parseInt(in.readLine());
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String name1=null;
		int rollNo;
		ArrayList<ClassList> arList = new ArrayList<ClassList>();
		for (int i = 0; i < num; i++) {
			System.out.println("Enter the Name");
			try {
				name1 = in.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("Enter the rollNo");
			rollNo = Integer.parseInt(in.readLine());

		cobjt[i].toEnter(rollNo, name1);
				
		}
		}
}


When it reaches calling the method showing null pointer exception.on debug could see error msg as thread "dispatchuncaughtexception throwable"

What I have tried:

trying to create object array and call method 'toEnter' with arguments at run time.
Posted
Updated 5-Jul-16 21:57pm

You have few more problems with your code. You are creating array of ClassList of ten and then you are reading num from keyboard, but you are not verifying.

But the reason for your error is:
when you create like this
Java
cobjt = new ClassList[10];

you are creating array of 10. But You haven't create the individual objects.
So, When you are trying to access cobjt[0], which is null, hence the NullPointerException
So, you try this way
Java
cobjt[i] = new ClassList();
cobjt[i].toEnter(rollNo, name1);


In your toEnter function, you have written code to exit from your running program.

You are reading name1 with try catch, but you are not doing the same for rollNo
 
Share this answer
 
Java
// create an array of 10 references, but note that each reference is not initialised
		ClassList[] cobjt = new ClassList[10];
// ...
// what is this line for? arList is never used anywhere
		ArrayList<classlist> arList = new ArrayList<classlist>();
// ...
// you try to call a method on a null reference. 
		cobjt[i].toEnter(rollNo, name1);

// You must first initialise that reference with a 
// new instance of a ClassList object thus:
		cobjt[i] = new ClassList();
		cobjt[i].toEnter(rollNo, name1);


</classlist></classlist>


I suggest worling through The Java Tutorials[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900