首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用数组作为参数实例化对象

用数组作为参数实例化对象
EN

Stack Overflow用户
提问于 2014-02-08 00:54:06
回答 2查看 86关注 0票数 0

在我的class类中,我有这个参数构造函数:

代码语言:javascript
复制
public class Cassette {

private int cass_Num;
private String cass_Titre;
private String cass_Realisat;
private int nbCopie;
private int [] cass_Emprunteur;

//...code
public Cassette(int num, String titre, String real, int copies, int[] nbEmp){
    this.cass_Num = num;
    this.cass_Titre = titre;
    this.cass_Realisat = real;
    this.nbCopie = copies;
    for(int i=0;i<nbEmp.length;i++){this.cass_Emprunteur[i] = nbEmp[i];}
}
//set methods...
//get methods...
}//end

我想在Main中实例化卡带类的几个对象,以测试我的任务中的一些函数。为了我的一生,我无法在没有null.Pointer.Exception的情况下找到正确的方法来做这件事

以下是我的主题曲:

代码语言:javascript
复制
//...code
Cassette [] tabCas = new Cassette[MAX_CASSETTES];
for(int i=0;i<tabCas.length;i++){tabCas[i]= new Cassette();}
tabCas[0] = new Cassette(0001,"Jurassic Pork","Steven Swineberg",7,new int[] {11111,44444});    //<--- error here
//...code

谢谢你帮忙!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-08 01:04:08

Cassette构造函数中,您希望:

代码语言:javascript
复制
cass_Emprunteur = nbEmp;

而不是行:

代码语言:javascript
复制
for(int i=0;i<nbEmp.length;i++){this.cass_Emprunteur[i] = nbEmp[i];}

或者,您还需要在for循环中使用int[]之前初始化它。

票数 0
EN

Stack Overflow用户

发布于 2014-02-08 01:01:03

查看构造函数"this.cass_Emprunteur“为null,然后尝试使用this.cass_Emprunteuri访问

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21640444

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档