简体   繁体   中英

Warning: field is used but is never assigned a non-“null” value

I have implemented a Java EE page which connects to an inventory webservice. I have implemented an inventory controller as shown below:

public class InventoryController {
    @Autowired
    private WebServiceTemplate inventoryWsTemplate;

...

}

inventoryWsTemplate is declared as a bean in web-servlet.xml.

The program works but I'm getting a warning that says:

field is used but is never assigned a non-"null" value

What should I do?

Just add a SuppressWarnings annotation:

@Autowired
@SuppressWarnings("null")
private WebServiceTemplate inventoryWsTemplate;

Normally I'd add a comment to justify the annotation (eg // Used by guice ) but in this case coming hot on the heels of an Autowired annotation, I don't think it's necessary.

EDIT: I can't reproduce the problem on Eclipse to start with, so I'm not sure which annotation value is required. You should check the list of supported annotation values for your compiler.

If you don't like suppressing warnings, you can change private to any other access modifier. Package-private probably being the best choice (besides private).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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