java 怎么定义一个对象数组

rgliuxu 2014-05-24 09:57:07
class student{
public
char code; /* 学号*/
char name; /* 姓名*/
int age; /* 年龄*/
char sex; /* 性别*/
float score[]= new float[3];

}


public class lianxi {
public static lianxi student1;
public static int n = 0;
public static int m = 0;
public static student[] b = new student[10];
{for(int i=0;i<10;i++)

b[i]=new student();
}
public static void main (String args[]){

int j=n,zhongjian1;
Scanner sc=new Scanner(System.in);
System.out.println("请输入待增加的学生数:\n");

m = sc.nextInt();
do
{ for (int i = 0 ; i < b.length;i++)
b[i] = new student();
System.out.println("请输入第"+(j-n+1)+" 个学生的年龄:\n");
b[j].age = sc.nextInt();
j++;
}while(j<n+m);
n+=m;
System.out.println("信息增加完毕!\n\n");
// sort();
System.out.println("按回车返回主菜单\n");
student1.qq(); //////////////第63行
}
public void qq()
{ System.out.println("...............");
System.out.println(b[0].age);
}
}
为什么运行后会显示

Exception in thread "main" java.lang.NullPointerException
at lianxi.main(lianxi.java:63)
我的想法是定义一个b【】对象
比如先输入b【0】.age 之后用另一个函数输出b[0].age
必须要用另一个函数输出b[0].age
刚刚开始学java 以前学C的~~
...全文
7586 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
rgliuxu 2014-05-25
  • 打赏
  • 举报
回复
引用 7 楼 u012339743 的回复:
帮顶
大水逼你好~大水逼再见
qingdujun 2014-05-25
  • 打赏
  • 举报
回复
帮顶
rgliuxu 2014-05-24
  • 打赏
  • 举报
回复
引用 2 楼 u010931121 的回复:
看错了,b[0].age,age属性又不是static的,建议把student写成标准的bean形式,有set和get方法的
额 是 public static吗??这个不是定义全局变量的吗??携程类了还需要用那个吗??我以前学C的 java不是很明白
rgliuxu 2014-05-24
  • 打赏
  • 举报
回复
引用 1 楼 u010931121 的回复:
student1.qq();中的student1哪来的,根本没有啊
我有啊public static lianxi student1;
rgliuxu 2014-05-24
  • 打赏
  • 举报
回复
我发现了~~我去修改下试试
二萌无双 2014-05-24
  • 打赏
  • 举报
回复
student1未初始化诶。。。 类成员变量默认初始化是null的。。 最好在main里初始化lianxi.student1 = new lianxi(); 虽然没太看明白逻辑。。。但Exception in thread "main" java.lang.NullPointerException 表示在main函数中访问到了空引用
很伤 2014-05-24
  • 打赏
  • 举报
回复
看错了,b[0].age,age属性又不是static的,建议把student写成标准的bean形式,有set和get方法的
很伤 2014-05-24
  • 打赏
  • 举报
回复
student1.qq();中的student1哪来的,根本没有啊

50,530

社区成员

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

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