Reputation: 536
In the lines marked with $ show the following message:
Multiple markers at this line - Null pointer access: The variable InformeSalvaguardasAGR can only be null at this location - Null pointer access: The variable InformeSalvaguardasAGR can only be null at this location
But i dont know why exactly, InformeAmenazasAGR is a superior variable and they can access. Why shows this message and how i can i make it correctly?
List<Object> InformeAmenazasAGR = null;
List<Object> InformeSalvaguardasAGR = null;
try {
Locale locale = (Locale)request.getSession().getAttribute(Globals.LOCALE_KEY);
if (locale == null){
locale = Locale.getDefault();
}
ResourceBundle recursos = ResourceBundle.getBundle(ProcopGlobals.FICHERO_MULTILENGUAJE,locale);
manager = new AGRInformesManager();
// EXTRAER TODAS LAS AMENAZAS.
InformeAmenazasAGR = manager.preparaInformeRiesgoActivos(idDimension, tipoActivo, idActivo, tipoActivoTexto, nombreActivo, recursos);
for (int i = 0; i < InformeAmenazasAGR.size()-1; i++) {
String Amenaza = InformeAmenazasAGR.get(i).toString();
Amenaza = Amenaza.substring(1,3);
Object resultadoQuery = manager.SalvaguardaPorAmenaza(Amenaza);
if(resultadoQuery!= null){
$ InformeSalvaguardasAGR.add(resultadoQuery);
} else {
}
}
if (InformeSalvaguardasAGR != null) {
//Prueba
String informetexto =InformeSalvaguardasAGR.toString();
}
} catch( Exception e ) {
log.error( "No fue posible generar el informe AGR para el Tipo: "+tipoActivo+" Activo: "+idActivo+" en Dimension: "+idDimension+".EX:" + e.toString());
e.printStackTrace();
} finally {
if( manager != null ) {
try {
manager.closeDAO();
} catch( Exception e ) {
e.printStackTrace();
}
manager = null;
}
}
JSONArray JSonArray = new JSONArray();
JSonArray.put(InformeSalvaguardasAGR);
String InformeSalvasAGR = JSonArray.toString();
InformeSalvasAGR = InformeSalvasAGR.substring(1, InformeSalvasAGR.length()-1);
request.setAttribute("InformeAmenazasAGR", InformeSalvasAGR);
Thank you in advance.
Upvotes: 0
Views: 206
Reputation: 11244
If something happens at the try-catch-finally
block, both InformeSalvaguardasAGR
and InformeAmenazasAGR
won't be initialized and remain null
. In future if they are null
, you can get NPE
trying to call their methods. I suggest you to replace this:
List<Object> InformeAmenazasAGR = null;
List<Object> InformeSalvaguardasAGR = null;
with this:
List<Object> InformeAmenazasAGR = new ArrayList<Object>();
List<Object> InformeSalvaguardasAGR = new ArrayList<Object>();
Upvotes: 1
Reputation: 3682
Though it is global it is never initialized. You cannot add to a list which is null. So initialize the list to :
List<Object> InformeSalvaguardasAGR = new ArrayList<Object>();
instead of
List<Object> InformeSalvaguardasAGR = null;
Upvotes: 1