Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Getting Command Line Arguments outside of Main() method?

807588Jan 19 2009 — edited Jan 22 2009
Hi Everybody,
I can't continue my programming because of the following problem:
Eclipse shows me this problem=> The local variable args may not have been initialized

Here's the code

package de.uka.xml.project.Filter;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


public class XMLSubtreeHandler extends DefaultHandler {
private int indentation = 0;

public void startElement(String namespaceUri,
String localName,
String qualifiedName,
Attributes attributes)
throws SAXException {
indent(indentation);

String qName= qualifiedName;
Attributes atts = attributes;

if (qName.equals("method")) {
System.out.println("< " + "root" + ">");
indentation =+2;
indent(indentation);
System.out.print("< " + qName);

String n = atts.getValue("","name"); // String-Wert von Attribut name
int numAttributes = atts.getLength();// # Attribute die der Tag hat

** String[] args ;**

for (int j=1; j < args.length; j++){
if (n.contains(*args*[j])){
for (int i=0; i < numAttributes; i++){
if (i==0){
int counter = 0;
System.out.print(attributes.getQName(i) + "=" +
attributes.getValue(counter));
counter++;
}

if (i==1){
System.out.print(attributes.getQName(i) +
attributes.getValue(i));
}
System.out.println();
indentation = indentation + 2;
}
}
}
}
}


public void endElement(String namespaceUri,
String localName,
String qualifiedName)
throws SAXException {
indentation = indentation - 2;
indent(indentation);
System.out.println("</ " + qualifiedName + ">");
}

private void indent(int indentation) {
for(int i=0; i<indentation; i++) {
System.out.print(" ");
}
}

public static void write2NewXMLData(){
//create new XML file & write to new XML file;
}

public static void main(String[] args) {
String jaxpPropertyName =
"javax.xml.parsers.SAXParserFactory";
// Pass the parser factory in on the command line with
// -D to override the use of the Apache parser.
if (System.getProperty(jaxpPropertyName) == null) {
String apacheXercesPropertyValue =
"org.apache.xerces.jaxp.SAXParserFactoryImpl";
System.setProperty(jaxpPropertyName,
apacheXercesPropertyValue);
}
String filename;
if (args.length > 0) {
filename = args[0];
} else {
String[] extensions = { "xml", "tld" };
WindowUtilities.setNativeLookAndFeel();
filename = ExtensionFileFilter.getFileName(".",
"XML Files",
extensions);
if (filename == null) {
filename = "test.xml";
}
}
printOutline(filename);
System.exit(0);
}

public static void printOutline(String filename) {
DefaultHandler handler = new XMLSubtreeHandler();
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
SAXParser parser = factory.newSAXParser();
parser.parse(filename, handler);
} catch(Exception e) {
String errorMessage =
"Error parsing " + filename + ": " + e;
System.err.println(errorMessage);
e.printStackTrace();
}
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 19 2009
Added on Jan 19 2009
43 comments
3,791 views