我想在一个类的构造方法中传一个对象 a,然后在类中声明一个与该对象同类型的变量 b,把a付给b,怎么做? 10
代码如下:publicclassOperation{Bankbank;publicOperation(){}publicOperation(Bankb){this.ban...
代码如下:
public class Operation {
Bank bank;
public Operation() {
}
public Operation(Bank b) {
this.bank = b;
}
File file = new File(bank.path);
}
在这里,new Operation后想调用bank中的path变量时报NullPointerException。为什么? 展开
public class Operation {
Bank bank;
public Operation() {
}
public Operation(Bank b) {
this.bank = b;
}
File file = new File(bank.path);
}
在这里,new Operation后想调用bank中的path变量时报NullPointerException。为什么? 展开
展开全部
你肯定是调用唯慧的信裤无参构造函数,造成bank空指针。
public class Operation {
Bank bank;
public Operation(Bank b) {
this.bank = b;
}
File file = new File(bank.path);
}
这样就行了,不允许你用无参的。
当然这里也不是很正规,正规写法应如下:
public class Operation {
Bank bank;指坦答
public Operation() {
}
public Operation(Bank b) {
this.bank = b;
}
public void getFile(){
if(null!=bank&&null!=bank.path)
File file = new File(bank.path);
else
System.out.println("又空指针啦!!!!");
}
}
public class Operation {
Bank bank;
public Operation(Bank b) {
this.bank = b;
}
File file = new File(bank.path);
}
这样就行了,不允许你用无参的。
当然这里也不是很正规,正规写法应如下:
public class Operation {
Bank bank;指坦答
public Operation() {
}
public Operation(Bank b) {
this.bank = b;
}
public void getFile(){
if(null!=bank&&null!=bank.path)
File file = new File(bank.path);
else
System.out.println("又空指针啦!!!!");
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询