日期:2014-05-20  浏览次数:20893 次

进来帮帮看看,为什么在类的本身创立自己对象不可以这样用的
import java.util.*;
class ManOne{
Scanner scanner=new Scanner(System.in);
String name;
boolean sex;
long num;
ManOne ob[]=new ManOne[2];
String informationgroup[]=new String[2];
void getin(){
for(int i=0;i<ob.length;i++){
System.out.println("Input the man's name");
ob[i].name=scanner.nextLine();
System.out.println("Input the man's sex");
ob[i].sex=scanner.nextBoolean();
scanner.nextLine();
System.out.println("Input the man's num");
ob[i].num=scanner.nextLong();
scanner.nextLine();
}
}
public String toString(int i){
return "the name is"+ ob[i].name+"the sex is "+ob[i].sex+"the num is"+num;
}
void savetheinformation(){
for(int i=0;i<informationgroup.length;i++){
informationgroup[i]=ob[i].toString(i);
}
}
void shower(){
for(int i=0;i<informationgroup.length;i++){
System.out.println(informationgroup[i]);
}
}
void settheinformation(){
System.out.println("Input the man's name");
name=scanner.nextLine();
System.out.println("Input the man's sex");
sex=scanner.nextBoolean();
scanner.nextLine();
System.out.println("Input the man's num");
num=scanner.nextLong();
scanner.nextLine();
}
String getMenber(String name){
if(this.name.endsWith(name)){
return name;
}
else{
return null;
}
}

}
public class TestForManone {
public static void main(String args[]){
ManOne A=new ManOne();
A.getin();
A.savetheinformation();
A.shower();
}

}

------解决方案--------------------
Java code
ManOne ob[]=new ManOne[2];
String informationgroup[]=new String[2];

------解决方案--------------------
ManOne ob[] = new ManOne[2];
这个需要初始化里面的对象啊兄弟,你只是创建了一个数组,但是数据里面没有初始化,所以[null,null]然后你用null来获取name属性,肯定就会空指针了。帮你改了一下
Java code

class ManOne {
    Scanner scanner = new Scanner(System.in);
    String name;
    boolean sex;
    long num;
    ManOne ob[] = new ManOne[2];
    String informationgroup[] = new String[2];
    void getin() {
        for (int i = 0; i < ob.length; i++) {
            System.out.println("Input the man's name");
            ob[i].name = scanner.nextLine();
            System.out.println("Input the man's sex");
            ob[i].sex = scanner.nextBoolean();
            scanner.nextLine();
            System.out.println("Input the man's num");
            ob[i].num = scanner.nextLong();
            scanner.nextLine();
        }
    }

    public String toString(ManOne[] ob,int i) {
        return "the name is " + ob[i].name + " the sex is " + ob[i].sex
                + " the num is" + num;
    }

    void savetheinformation(ManOne[] ob) {
        for (int i = 0; i < informationgroup.length; i++) {
            informationgroup[i] = ob[i].toString(ob,i);
        }
    }

    void shower() {
        for (int i = 0; i < informationgroup.length; i++) {
            System.out.println(informationgroup[i]);
        }
    }

    void settheinformation() {
        System.out.println("Input the man's name");
        name = scanner.nextLine();
        System.out.println("Input the man's sex");
        sex = scanner.nextBoolean();
        scanner.nextLine();
        System.out.println("Input the man's num");
        num = scanner.nextLong();
        scanner.nextLine();
    }

    String getMenber(String name) {
        if (this.name.endsWith(name)) {
            return name;
        } else {
            return null;
        }
    }

}

public class TestForManone {
    public static void main(String args[]) {
        ManOne A = new ManOne();
        for(int i = 0; i < A.ob.length; i++){
            A.ob[i] = new ManOne();
        }
        A.getin();
        A.savetheinformation(A.ob);
        A.shower();
    }
}