java数组输出数组中已经赋值的部分总是出错

fwyynl 2018-06-10 04:47:21
一个已经初始化数组长度的数组,我给了前几个赋值,其余的都没有赋值
输出数组中已经赋值的部分总是出错
没有赋值的程序应该默认给null,我是和null比较看是否有没有赋值的
结果出错nullpointer
求问什么解决方法?
我看了有arraylist还有其他的吗

```
for(int j = 0 ; ;j++){
if(allcourse[j].equals(null))
break;
allcourse[j].print();
}

```
下面是前面完整的代码
```



```

public class Course {
String Name;
int Cno;
String teacher;
int score;
static Course[] allcourse = new Course[2];//静态变量

Course(){
Name = null;Cno = 0; teacher = null; score = 0;
}

Course(String name,int cno,String tea,int sco){
Name = name;
Cno = cno;
teacher = tea;
score = sco;
}

public void print(){
JOptionPane.showMessageDialog(null, "Course info:"+'\n');
JOptionPane.showMessageDialog(null, "Course name:"+Name);
JOptionPane.showMessageDialog(null, "Course number:"+Cno);
JOptionPane.showMessageDialog(null, "Course teacher:"+teacher);
JOptionPane.showMessageDialog(null, "Course score:"+score+'\n');
}

public static void input_course(){

for(int i = 0;;i++)
{
String str = JOptionPane.showInputDialog("start,please input course info");
if(str.equals("#")) { JOptionPane.showMessageDialog(null,"jump out !");break ;}
else
{
String name = JOptionPane.showInputDialog("please input Course Name");
String str1 = JOptionPane.showInputDialog("please input Course number");
int cno = Integer.parseInt(str1);
String teach = JOptionPane.showInputDialog("please input Course teacher");
String str2 = JOptionPane.showInputDialog("please input Course score");
int score = Integer.parseInt(str2);
Course c1 = new Course(name,cno,teach,score);
allcourse[i] = c1;
}

}
for(int j = 0 ; ;j++){
if(allcourse[j].equals(null))
break;
allcourse[j].print();
}
}

```

```
...全文
858 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
stacksoverflow 2018-06-11
  • 打赏
  • 举报
回复
allcourse[j].equals(null)→allcourse[j] == null
夏讨厌 2018-06-10
  • 打赏
  • 举报
回复
for(int i = 0;i<allcourse.length;i++) for(int j = 0 ;j<allcourse.length ;j++)

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧