ClassB中能那样定义一个ClassC的变量,然后仅仅用哪个变量就可以调用ClassC中的实例方法而非成员方法?!
NullPointerException是因为什么引起的呢?
- Java code
class ClassA { public void methodA() { ClassB classB = new ClassB(); classB.getValue(); }}class ClassB { public ClassC classC; public String getValue() { return classC.getValue(); }}class ClassC { public String value; public String getValue() { value = "ClassB"; return value; }}public class Question { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ClassA a = new ClassA(); a.methodA(); }}//Exception in thread "main" java.lang.NullPointerException// at ClassB.getValue(Question.java:11)// at ClassA.methodA(Question.java:4)// at Question.main(Question.java:31)
------解决方案--------------------