class A{
B b;
}
class B{
int age;
}
class Entrance{
public static void main(String[] args){
A a1=new A();
System.out.println(a1.b.age);
}
}
原因分析:
在生成A类对象的时候没有生成B类对象,所以访问不到B内的参数,显示空指针错误
解决方案: class A中B b;改成B b=new B();

class A{
B b;
}
class B{
int age;
}
class Entrance{
public static void main(String[] args){
A a1=new A();
System.out.println(a1.b.age);
}
}
在生成A类对象的时候没有生成B类对象,所以访问不到B内的参数,显示空指针错误