Exception in thread "main"
java.lang.NullPointerException怎么解决???
class Student{
Subject a;
Subject b;
Subject c;
class Subject{
String a="JAVA 考察";
String b="SQL 考试";
String c="J2EE 考察";
int Atten=0;
int Assign=0;
int Lab=0;
int Fin=0;
public Subject() {
// TODO Auto-generated constructor stub
Atten=0;
Assign=0;
Lab=0;
Fin=0;
}
}
void setStudent(int a1,int a2,int a3,int a4,int b1,int b2,int b3,int b4,int c1,int c2,int c3,int c4){
a.Atten=a1;
a.Assign=a2;
a.Lab=a3;
a.Fin=a4;
b.Atten=b1;
b.Assign=b2;
b.Lab=b3;
b.Fin=b4;
c.Atten=c1;
c.Assign=c2;
c.Lab=c3;
c.Fin=c4;
}
static class SubjectType{
double attenRatio;
int i;
static final double assignRatio=0.1;
static final double labRatio=0.1;
double finalRatio;
SubjectType(int k)
{i=k;}
void setRatio(){
if(i==1)
{
attenRatio=0.1;
finalRatio=0.7;
}
if(i==2)
{
attenRatio=0.2;
finalRatio=0.6;
}
}
}
void printInfo(){
class CalcScore{
SubjectType java=new SubjectType(2);
SubjectType SQL=new SubjectType(1);
SubjectType JE22=new SubjectType(2);
double a1=a.Atten*java.attenRatio+a.Assign*java.assignRatio+a.Lab*java.labRatio+a.Fin*java.finalRatio;
double b1=b.Atten*SQL.attenRatio+b.Assign*SQL.assignRatio+b.Lab*SQL.labRatio+b.Fin*SQL.finalRatio;
double c1=c.Atten*JE22.attenRatio+c.Assign*JE22.assignRatio+a.Lab*JE22.labRatio+c.Fin*JE22.finalRatio;
}
CalcScore cal=null;
System.out.println("课程 性质 出勤 作业 实验 期末 总分");
System.out.println("====================================");
System.out.println(a.a+" "+a.Atten+" "+a.Assign+" "+a.Lab+" "+a.Fin+cal.a1);
System.out.println(b.b+" "+b.Atten+" "+b.Assign+" "+b.Lab+" "+b.Fin);
System.out.println(c.c+" "+c.Atten+" "+c.Assign+" "+c.Lab+" "+c.Fin);
System.out.println("************************************");
}
}
public class t3 {
static Student S1 = null;
public static void main(String[]args){
S1.setStudent(90,85,75,80,80,90,82,75,78,70,65,70);
}
}
完全想不到要怎么改才好,求帮忙!
------解决方案--------------------这么简单的问题也需要问?
public class t3 {
static Student S1 = null;
public static void main(String[]args){
S1.setStudent(90,85,75,80,80,90,82,75,78,70,65,70);
}
}
楼主的S1变量是=null的,还没有初始化,也就是空的,但是你却在下面调用了S1变量的方法,当然会报
空指针啦,就好像楼主拿了一个空杯子在喝水是一个道理。
------解决方案--------------------实在找不到问题出在哪,你可以在这个方法任意一行进行bug单步跟踪