Reputation: 718
I have this class now working fine, but I've been struggling with it many hours to end of changing in it on a different logic from my first approach.
public class MyClass {
public static MyClass tablas;
public static String[] GROUPS;
public static String[] ESTATUS
public static String[] CLIENTS;
public void init(){
this.tablas = new MyClass();
this.setGroups();
CLIENTS=this.setAny("/servlets/CLIENTS","page_rows","nombre");
ESTADO_PEDIDO= new String[]{"str1","str2","str3","str4","str5"};
}
private String[] setAny(String sevlet,String bigNode,String smallNode){
String[] ret=null;
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("operation", "4");
parameters.put("avance", "0");
InputStream is = Connection.con.processRequest("GET", sevlet, parameters);
Document dom = null;
try {
dom = UtilesDom.parseXML(is);
NodeList lines = dom.getElementsByTagName(bigNode);
Element el = (Element)lines.item(0);
NodeList nlist = el.getElementsByTagName(smallNode);
ret = new String[nlist.getLength()];
for (int i = 0; i < nlist.getLength(); i++) {
ret[i] = nlist.item(i).getTextContent();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
return ret;
}
private void setGroups(){
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("operation", "4");
parameters.put("avance", "0");
InputStream is = Connection.con.processRequest("GET", "/servlets/GROUPS_CLIENTS", parameters);
Document dom = null;
try {
dom = UtilesDom.parseXML(is);
NodeList lines = dom.getElementsByTagName("lines");
Element el = (Element)lines.item(0);
NodeList nlist = el.getElementsByTagName("GROUP");
GROUPS = new String[nlist.getLength()];
for (int i = 0; i < nlist.getLength(); i++) {
GROUPS[i] = nlist.item(i).getTextContent();
}
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
As you can see there is two similar methods setGroups
and setAny
these are used to fill the Strings[]
on top.setGroups
was my original method but when I needed different Strings[]
thought that a "less hard-coded" and most flexible method would be nice, so I tried this:
private void setAny(String sevlet,String bigNode,String smallNode,String[] which){
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("operation", "4");
parameters.put("avance", "0");
InputStream is = Connection.con.processRequest("GET", sevlet, parameters);
Document dom = null;
try {
dom = UtilesDom.parseXML(is);
NodeList lines = dom.getElementsByTagName(bigNode);
Element el = (Element)lines.item(0);
NodeList nlist = el.getElementsByTagName(smallNode);
which = new String[nlist.getLength()];
for (int i = 0; i < nlist.getLength(); i++) {
which[i] = nlist.item(i).getTextContent();
System.out.println(which[i]);
}
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
Using the call like:
this.setAny("/principal/Clientes","page_rows","nombre",CLIENTS);
also
this.setAny("/principal/Clientes","page_rows","nombre",this.CLIENTS);
and
this.setAny("/principal/Clientes","page_rows","nombre",this.tablas.CLIENTS);
The problem with it is that the String[]
passed as parameter (aka CLIENTS
) just stay null
, and yes at the en of the for loop
its populated properly and the console shows what it supposed to.So the question is:
Why String[] CLIENTS
cant be populated when passed as a parameter,and just stay as null
?
PD: As you may notice English is not my language so please suggest any grammar/redaction/spelling... corrections.
Upvotes: 2
Views: 231
Reputation: 1498
Okay so I'm gonna pretend your parameter is a String[]
and not a String
here.
Your problem is that once you create a new array with the new operator, your reference changes to that new array. So the old one isn't affected.
So yes, you create a new array and fill it properly, but sadly it won't be CLIENTS
. If you do it like in your first example and return the String Array to save it, that will work.
Another option would be to create a static HashMap of String Arrays instead of just three different static String Arrays. Then you can pass the key to the method and just replace the Array at the given key. That way you don't need to work with a return value.
Upvotes: 1
Reputation: 121998
It is null
at runtime and the compile dont know about that. It strictly checks the type at compile time it self.
setAny(String sevlet,String bigNode,String smallNode)
That last parameter is a String and you are trying to pass an Array. Probably you need to change the signature as
setAny(String sevlet,String bigNode,String smallNode[])
So that it receives an array.
Upvotes: 0