java对象数组初始化空指针异常 50
publicclassTest{publicstaticvoidmain(Stringargs[]){int[]id1={1,2,3,4,5,6,7,8,9,10};St...
public class Test{
public static void main(String args[]){
int[] id1 = {1,2,3,4,5,6,7,8,9,10};
String[] name1 = {"a","b","c","d","e","f","g","h","i","j"};
for(int i=0;i<10;i++){
Student.stu[i] = new Student(id1[i],name1[i]);
}
}
}
public class Student{
private int id;
private String name;
static Student[] stu;
public Student(int i,String n){
this.id=i;
this.name=n;
}
} 展开
public static void main(String args[]){
int[] id1 = {1,2,3,4,5,6,7,8,9,10};
String[] name1 = {"a","b","c","d","e","f","g","h","i","j"};
for(int i=0;i<10;i++){
Student.stu[i] = new Student(id1[i],name1[i]);
}
}
}
public class Student{
private int id;
private String name;
static Student[] stu;
public Student(int i,String n){
this.id=i;
this.name=n;
}
} 展开
6个回答
展开全部
我把你的类调正了一下,因为你的student里面的数组没有没有初始化,所以空指针了。
public class TestMain {
public static void main(String args[]) {
Student[] studentGroup = null;
int[] id1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
String[] name1 = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" };
studentGroup = new Student[name1.length];
for (int i = 0; i < 10; i++) {
studentGroup[i] = new Student(id1[i], name1[i]);
}
for (int i = 0; i < studentGroup.length; i++) {
System.out.print(studentGroup[i].getId());
System.out.println(studentGroup[i].getName());
}
}
}
public class Student {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int id;
private String name;
public Student(int i, String n) {
this.id = i;
this.name = n;
}
}
public class TestMain {
public static void main(String args[]) {
Student[] studentGroup = null;
int[] id1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
String[] name1 = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" };
studentGroup = new Student[name1.length];
for (int i = 0; i < 10; i++) {
studentGroup[i] = new Student(id1[i], name1[i]);
}
for (int i = 0; i < studentGroup.length; i++) {
System.out.print(studentGroup[i].getId());
System.out.println(studentGroup[i].getName());
}
}
}
public class Student {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int id;
private String name;
public Student(int i, String n) {
this.id = i;
this.name = n;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你没有给static Student[] stu;
初始化啊
public static void main(String args[]){
int[] id1 = {1,2,3,4,5,6,7,8,9,10};
String[] name1 = {"a","b","c","d","e","f","g","h","i","j"};
Student.stu[]=new Student[10];
for(int i=0;i<10;i++){
Student.stu[i] = new Student(id1[i],name1[i]);
}
}
初始化啊
public static void main(String args[]){
int[] id1 = {1,2,3,4,5,6,7,8,9,10};
String[] name1 = {"a","b","c","d","e","f","g","h","i","j"};
Student.stu[]=new Student[10];
for(int i=0;i<10;i++){
Student.stu[i] = new Student(id1[i],name1[i]);
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Student这个类里的public Student(int i,String n){.....}是用来初始化的,而你这个语句Student.stu[i] = new Student(id1[i],name1[i]);明显有错,要想用Student.stu[i]必须使用结构体。你可以在Test这个类里定义Student []stu=new Student[10];然后再在for循环里进行初始化:stu[i] = new Student(id1[i],name1[i]);这样就可以了。不懂可以继续追问
更多追问追答
追问
上面的改好了,然后下面这个还是报错求解~
public boolean checkExist(int i,String n){
boolean e=false;
for(int j=0;j<stu.length;j++){
if((stu.id==i)&&(stu.name==n)){ e = true; break; }
}
return e;
}
追答
if((stu.id==i)&&(stu.name==n)){ e = true; break; 这里出错了,应该是if((stu[j].id==i)&&(stu[j].name==n)){ e = true; break;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Student.stu没有初始化
public class Test{
public static void main(String args[]){
int[] id1 = {1,2,3,4,5,6,7,8,9,10};
String[] name1 = {"a","b","c","d","e","f","g","h","i","j"};
Student.stu = new Student[10];
for(int i=0;i<10;i++){
Student.stu[i] = new Student(id1[i],name1[i]);
}
}
}
class Student {
private int id;
private String name;
static Student[] stu;
public Student(int i,String n){
this.id=i;
this.name=n;
}
}
public class Test{
public static void main(String args[]){
int[] id1 = {1,2,3,4,5,6,7,8,9,10};
String[] name1 = {"a","b","c","d","e","f","g","h","i","j"};
Student.stu = new Student[10];
for(int i=0;i<10;i++){
Student.stu[i] = new Student(id1[i],name1[i]);
}
}
}
class Student {
private int id;
private String name;
static Student[] stu;
public Student(int i,String n){
this.id=i;
this.name=n;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
static Student[] stu;
改成static Student[] stu= new Student[10];就好了
改成static Student[] stu= new Student[10];就好了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询