简体   繁体   English

为什么不能用Java访问此创建的对象?

[英]Why can't I access this created object in Java?

I have a problem relating to this simple Java object creation. 我有一个与此简单Java对象创建相关的问题。

My code looks like this: 我的代码如下所示:

@RequestMapping(value = "/salvaDatiContabiliEnte", method = RequestMethod.POST)
public String salvaDatiContabiliEnte(HttpServletRequest request, Model model) throws Exception {

    CodIbanEspanso ibanEspanso;

    Tid023Intervento progettoSelezionato = (Tid023Intervento) model.asMap().get("progettoSelezionato");
    System.out.println("INTO salvaDatiContabiliEnte, progettoSelezionato: " + progettoSelezionato.toString());

    Tid022Anagraficarup anagraficaRup = progettoSelezionato.getTid022Anagraficarup();               // Angrafica del RUP
    Tid018Entebeneficiario enteBeneficiario = progettoSelezionato.getTid018Entebeneficiario();      // Ente beneficiario

    DefaultMultipartHttpServletRequest multipartRequest = (DefaultMultipartHttpServletRequest) request;
    GestioneDatiContabiliForm gestioneDatiContabiliForm = new ObjectMapper().readValue(multipartRequest.getParameterMap().get("form")[0], GestioneDatiContabiliForm.class);

    if(gestioneDatiContabiliForm.getIban() != null) {       // Se l'IBAN inserito non è null ---> Se la regione non è il Friuli
        ibanEspanso = new CodIbanEspanso(gestioneDatiContabiliForm.getIban());  // Costruisce l'IBAN espanso a partire dal codice IBAN intero

    }

    return "blablabla";
}

As you can see, at the beginning of my method I declare a CodIbanEspanso ibanEspanso object and I don't initialize it by calling a constructor. 如您所见,在我的方法开始时,我声明了CodIbanEspanso ibanEspanso对象,并且我没有通过调用构造函数来对其进行初始化。

I then initialize it in this statement: 然后,我在以下语句中对其进行初始化:

if(gestioneDatiContabiliForm.getIban() != null) {       // Se l'IBAN inserito non è null ---> Se la regione non è il Friuli
    ibanEspanso = new CodIbanEspanso(gestioneDatiContabiliForm.getIban());  // Costruisce l'IBAN espanso a partire dal codice IBAN intero
}

Using the debugger I see that it does enter in the if statement and it does check that the object fields are correctly initialized by its constructor. 使用调试器,我看到它确实输入了if语句,并且确实检查了其构造函数是否正确初始化了对象字段。

But then, after the if statement, if I try to see the content of the ibanEspanso object (that I had initialized), Eclipse debugger give me this error message: 但是然后,在if语句之后,如果我尝试查看ibanEspanso对象(已初始化)的内容,则Eclipse调试器会给我以下错误消息:

"ibanEspanso"    <error(s)_during_the_evaluation>   
ibanEspanso cannot be resolved to a variable    

How is it possible? 这怎么可能? Why can it not be found if this object was initialized when the program entered the previous if statement? 当程序进入前一个if语句时,为什么找不到该对象是否初始化? What am I missing? 我想念什么?

Maybe because your reference is not initialised. 可能是因为您的参考没有初始化。 You may try something like : 您可以尝试类似:

CodIbanEspanso ibanEspanso = null;

ibanEspanso is never used after the if statement. 如果使用if语句,则永远不要使用ibanEspanso put it somewhere after the if statement, anything would do: 将其放在if语句后的某处,任何事情都会做:

System.out.println(ibanEspanso);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM