本文介绍了使用对象数组发送参数方法时,“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 stubClassList[] 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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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 blocke.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.推荐答案 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 waycobjt[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 initialisedClassList[] cobjt = new ClassList[10];// ...// what is this line for? arList is never used anywhereArrayList<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> 我建议通过 Java教程 [ ^ ]。 这篇关于使用对象数组发送参数方法时,“Thread dispatchuncaughtexception throwable”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-26 21:08