自定义数组的空指针异常问题

lymne 2012-09-28 10:26:04
import java.util.*;
class Student{
String name;
String major;
int num;
int grade;
public Student(String name,String major,int num,int grade)
{
this.name=name;
this.major=major;
this.num=num;
this.grade=grade;
}


}
public class Team{
public static void main(String[]args)
{
Scanner in=new Scanner(System.in);
Student team[][]=new Student[3][];
for(int i=0;i<3;i++)
for(int j=0;j<team[i].length;)
{
team[i][j].name=in.nextLine();
team[i][j].major=in.nextLine();
team[i][j].num=in.nextInt();
team[i][j].grade=in.nextInt();
System.out.println("姓名"+team[i][j].name+ "专业" +team[i][j].major+ "项目号"+team

[i][j].num+" 项目得分 "+team[i][j].grade);

System.out.printf("\n");
}
}
}


cmd编译成功,但运行不了,提示NullPointerException
问题是怎么new?求高手解答
...全文
447 18 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
老猿的书房 2012-09-28
  • 打赏
  • 举报
回复
能给出全部的代替和错误堆栈信息么?
lymne 2012-09-28
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

team[i].length
这里空指针了,数组没有初始化
[/Quote]
不知这样算不算初始化了 for(int i=0;i<3;i++)
team[i] = new Student[3];
for (int j = 0; j < team[i].length;)
{
System.out.printf("请输入组员信息");
team[i][j] = new Student();
team[i][j].name=in.nextLine();
team[i][j].major=in.nextLine();
team[i][j].num=in.nextInt();
team[i][j].grade=in.nextInt();


还是编译不过去,显示找不到符号
wjzmr123 2012-09-28
  • 打赏
  • 举报
回复
team[i].length
这里空指针了,数组没有初始化
Firebirds 2012-09-28
  • 打赏
  • 举报
回复
找不到符号,看是哪里的名字写错了。
lymne 2012-09-28
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

看你的需求,根本没必要用二维数组
只需要
Student[] sts = new Students[3];这样就可以了
然后循环内直接用
Student st = new Student();
st.name=in.nextLine();
//...
最后吧sts[i] = st就可以了
[/Quote]
改成一维了,但是还是提示“找不到符号”
符号:构造函数Student();
位置:类Student

是不是构造函数那里出问题了?
lymne 2012-09-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

引用 2 楼 的回复:

1楼的代码能运行但什么都不出来啊啊啊,到底是哪里出问题了呢

你具体想要什么?(没太懂你的需求) 我只是给出了怎么去new ,如果你要new多个,那么你把它写到循环里呗
[/Quote]

具体就是能让我可以输入组员的名字等信息显示出来啊,可是在循环体加那个new显示“找不到符号“
安特矮油 2012-09-28
  • 打赏
  • 举报
回复
看你的需求,根本没必要用二维数组
只需要
Student[] sts = new Students[3];这样就可以了
然后循环内直接用
Student st = new Student();
st.name=in.nextLine();
//...
最后吧sts[i] = st就可以了

lymne 2012-09-28
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

team[i][j] = new Student();

[/Quote]

这段代码加入到循环后提示"找不到符号"
符号:构造函数Student();
位置:类Student
想喝咖啡的貓 2012-09-28
  • 打赏
  • 举报
回复
team[i][j] = new Student();
team[i][j].name=in.nextLine();
想喝咖啡的貓 2012-09-28
  • 打赏
  • 举报
回复
Student team[][]=new Student[3][];

数组是个对象,如果数组的元素也是对象的话,需要独立的初始化。
dengsilinming 2012-09-28
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

1楼的代码能运行但什么都不出来啊啊啊,到底是哪里出问题了呢
[/Quote]
你具体想要什么?(没太懂你的需求) 我只是给出了怎么去new ,如果你要new多个,那么你把它写到循环里呗
lymne 2012-09-28
  • 打赏
  • 举报
回复
1楼的代码能运行但什么都不出来啊啊啊,到底是哪里出问题了呢
dengsilinming 2012-09-28
  • 打赏
  • 举报
回复
Scanner in=new Scanner(System.in);
String name=in..nextLine();
String major=in..nextLine();
int num=in.nextInt();
int grade=in.nextInt();
Student student=new Student(name, major, num, grade);
zhaoming262350 2012-09-28
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 的回复:]

你的二维数组没有实例化,必须是报空指针异常啊。正确做法如下:
Java code

import java.util.*;
class Student{
String name;
String major;
int num;
int grade;

public Student(String name,String major,int num,int grade)
{
this……
[/Quote]
这个解释很正确
lymne 2012-09-28
  • 打赏
  • 举报
回复
谢谢你的回答,明白了[Quote=引用 16 楼 的回复:]

当然可以实例化的时候这样:
Java code

Student team[][];
team=new Student[3][];
for(int i=0;i<team.length;i++)
{
team[i]=new Student[1];
team[i][0]=new Student();
}
[/Quote]
shenhua 2012-09-28
  • 打赏
  • 举报
回复
当然可以实例化的时候这样:

Student team[][];
team=new Student[3][];
for(int i=0;i<team.length;i++)
{
team[i]=new Student[1];
team[i][0]=new Student();
}
shenhua 2012-09-28
  • 打赏
  • 举报
回复
你的二维数组没有实例化,必须是报空指针异常啊。正确做法如下:

import java.util.*;
class Student{
String name;
String major;
int num;
int grade;

public Student(String name,String major,int num,int grade)
{
this.name=name;
this.major=major;
this.num=num;
this.grade=grade;
}
//注意,没带参数的构造方法需要加上
public Student()
{

}

}
public class Team{
public static void main(String[]args)
{
Scanner in=new Scanner(System.in);
//动态实例开始
Student team[][];
team=new Student[3][];
team[0]=new Student[1];
team[1]=new Student[1];
team[2]=new Student[1];
for(int i=0;i<team.length;i++)
{
team[i][0]=new Student();
}
//动态实例结束
for(int i=0;i<3;i++)
for(int j=0;j<team[i].length;j++)
{
team[i][j].name=String.valueOf(in.nextLine());
team[i][j].major=in.nextLine();
team[i][j].num=in.nextInt();
team[i][j].grade=in.nextInt();
System.out.println("姓名"+team[i][j].name+ "专业" +team[i][j].major+ "项目号"+team
[i][j].num+" 项目得分 "+team[i][j].grade);
System.out.printf("\n");
}
}
}

dengsilinming 2012-09-28
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

引用 7 楼 的回复:

看你的需求,根本没必要用二维数组
只需要
Student[] sts = new Students[3];这样就可以了
然后循环内直接用
Student st = new Student();
st.name=in.nextLine();
//...
最后吧sts[i] = st就可以了

改成一维了,但是还是提示“找不到符号”
符号:构造函数……
[/Quote]
你的代码里根本没有Student()这个构造函数,不报错才怪了呢。你的构造函数是有参数的:public Student(String name,String major,int num,int grade)
{
this.name=name;
this.major=major;
this.num=num;
this.grade=grade;
}
用的时候是Student st = new Student(name, major, num, grade);其中name, major, num, grade需要自己定义。别人只是提供一个思路,你自己得去修改一些,不要学拿来主义。

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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