java数组输出数组中已经赋值的部分总是出错
一个已经初始化数组长度的数组,我给了前几个赋值,其余的都没有赋值
输出数组中已经赋值的部分总是出错
没有赋值的程序应该默认给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();
}
}
```
```