使用对象数组发送参数方法时,“Thread dispatchuncaughtexception throwable” [英] "Thread dispatchuncaughtexception throwable"while sending argumentsto methods using object array

查看:94
本文介绍了使用对象数组发送参数方法时,“Thread dispatchuncaughtexception throwable”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);
				
		}
		}
}





当它到达调用显示null的方法时指针exception.on调试可以看到错误消息作为线程dispatchuncaughtexception throwable



我尝试过:



尝试创建对象数组并在运行时使用参数调用方法'toEnter'。



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.

推荐答案

你的问题还有一些问题。码。您正在创建ClassList为10的数组,然后您正在从键盘读取num,但您没有验证。



但是你错误的原因是:

当你这样创造时

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
cobjt = new ClassList[10];



您正在创建10的数组。但您还没有创建单个对象。

所以,当你试图访问cobjt [0]时,它是null,因此NullPointerException

所以,你试试这样的方式


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

cobjt[i] = new ClassList();
cobjt[i].toEnter(rollNo, name1);





在你的toEnter函数中,你已编写代码退出从你的运行程序。



你正在尝试使用try catch读取name1,但是你没有为rollNo做同样的事情



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


// 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>





我建议通过< a href =http://download.oracle.com/javase/tutorial/index.html> Java教程 [ ^ ]。


这篇关于使用对象数组发送参数方法时,“Thread dispatchuncaughtexception throwable”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆