日期:2014-05-17  浏览次数:20651 次

java.lang.NullPointerException 这个问题怎么解决
我写了一个JAVA小程序,不知道哪错了,请大家帮忙看看!
报错为:
Exception in thread "main" java.lang.NullPointerException
at mypack.Count.countd(Count.java:25)
at mypack.Getall.main(Getall.java:16)

所有的代码如下:
People.java
Java code

package mypack;

public class People {

    String name;
    String hometown;            
    String local;               
    String university;         
    String highschool;          
    String major;             
    String grade;             
    
    public void set(People A)
    {
        this.name=A.getName();
        this.hometown=A.getHometown();
        this.local=A.getLocal();
        this.university=A.getUniversity();
        this.highschool=A.getHighschool();
        this.major=A.getMajor();
        this.grade=A.getGrade();
    }
    
    public void setName(String name)
    {
        this.name=name;
    }
    public String getName()
    {
        return name;
    }
    
    public void setHometown(String hometown)
    {
        this.hometown=hometown;
    }
    public String getHometown()
    {
        return hometown;
    }
    
    public void setLocal(String local)
    {
        this.local=local;
    }
    public String getLocal()
    {
        return local;
    }
    
    public void setUniversity(String university)
    {
        this.university=university;
    }
    public String getUniversity()
    {
        return university;
    }
    
    public void setHighschool(String highschool)
    {
        this.highschool=highschool;
    }
    public String getHighschool()
    {
        return highschool;
    }
    
    public void setMajor(String major)
    {
        this.major=major;
    }
    public String getMajor()
    {
        return major;
    }
    
    public void setGrade(String grade)
    {
        this.grade=grade;
    }
    public String getGrade()
    {
        return grade;
    }
}




Count.java
Java code

package mypack;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import mypack.People;

public class Count {
    
    People sb;
    int dist;
    public Count(People A)
    {
        sb=new People();
        sb.set(A);
        
    }
    public int countd(People B)
    {
        int flag,i;
        int dist2=0;
        int[] buff ={0,0,0,0,0,0};
        int[] md={3,4,6,5,2,1};

        if (sb.getHometown().equals(B.getHometown()))  //这报错
            buff[0]=3;
        if (sb.getLocal().equals(B.getLocal()))
            buff[1]=4;
        if (sb.getUniversity().equals(B.getUniversity()))
            buff[2]=6;
        if (sb.getHighschool().equals(B.getHighschool()))
            buff[3]=5;
        if (sb.getMajor().equals(B.getMajor()))
            buff[4]=2;
        if (sb.getGrade().equals(B.getGrade()))
            buff[5]=1;
        
        for (i=0;i<6;i++)
        {
            dist=dist+(buff[i]-md[i])*(buff[i]-md[i]);
            dist2=dist2+buff[i]*buff[i];
        }
        
        if (dist<dist2)
            flag=1;
        else
            flag=0;
        
        return flag;
    }
    
    public int getDist()
    {
        return dist;
    }
 
}



Getall.java
Java code

package mypack;

public class Getall {
    
    public static void main(String[] ar