我用构造函数创建了一个类构建:
public class Building {
private String Adress;
private String Town;
public Building(String A, String T) throws NullPointerException {
if (A == null || T == null) {
throw new NullPointerException();
}
else{
Adress = A;
Town = T;
System.out.println(Adress+Town);}
public String getAdress() {
return Adress
}
}
在主要内容之后我执行此操作:
public static void main(String[] args) {
try{Building MyBuilding = new Building("Prelungirea Ghencea", "Tulcea");}
catch(NullPointerException e){
System.out.println("Adresa sau Orasul lipsesc");
}}
当我想打电话时
`MyBuilding.getAdress()`
,copilator说对象MyBuilding不存在。来自try / catch的语句不是创建对象吗? 我应该在所有尝试并捕获块后创建对象构建?