You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by "Steele, Richard" <ri...@steelezone.net> on 2011/09/28 13:56:36 UTC

NPE thrown from ProjectHelper2.parse() when parsing from URL

Running Ant 1.8.2 on Windows XP, I'm writing a task that returns a string
resource collection of target names in a project.  Well, attempting to write
one.

I'm running into problems when the build file I'm parsing is from a URL
instead of a File.  The main method that I have looks like this:

    private Collection<String> getTargetNames() {

        if (!projectFile.isExists()) {
            throw new BuildException("Cannot find resource " + projectFile);
        }

        Project childProject = project.createSubProject();
        childProject.setJavaVersionProperty();
        project.copyUserProperties(childProject);
        childProject.initProperties();

        ProjectHelper helper =
ProjectHelperRepository.getInstance().getProjectHelperForBuildFile(projectFile);
        childProject.addReference(ProjectHelper.PROJECTHELPER_REFERENCE,
helper);
        helper.parse(childProject, projectFile);

        Collection<String> names = childProject.getTargets().keySet();

        return names;
    }

The projectFile variable is a URLResource.  However, I'm getting a NPE
thrown from helper.parse(Project, Object):

java.lang.NullPointerException
        at
org.apache.tools.ant.helper.ProjectHelper2$ProjectHandler.onStartElement(ProjectHelper2.java:845)
        at
org.apache.tools.ant.helper.ProjectHelper2$RootHandler.startElement(ProjectHelper2.java:600)
        at
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:501)
        at
com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:400)
        at
com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:626)
        at
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3103)
        at
com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:922)
        at
com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
        at
com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
        at
com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
        at
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
        at
com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
        at
com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
        at
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
        at
com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
        at
org.apache.tools.ant.helper.ProjectHelper2.parse(ProjectHelper2.java:307)
        at
org.apache.tools.ant.helper.ProjectHelper2.parse(ProjectHelper2.java:178)

The relevant code in ProjectHelper2 is:

            // set explicitly before starting ?
            if (project.getProperty("basedir") != null) {
                project.setBasedir(project.getProperty("basedir"));
            } else {
                // Default for baseDir is the location of the build file.
                if (baseDir == null) {

project.setBasedir(context.getBuildFileParent().getAbsolutePath());
                } else {
                    // check whether the user has specified an absolute path
                    if ((new File(baseDir)).isAbsolute()) {
                        project.setBasedir(baseDir);
                    } else {

project.setBaseDir(FILE_UTILS.resolveFile(context.getBuildFileParent(),
                                                                  baseDir));
                    }
                }
            }

The problem appears to be that build files will have a parent build file,
but in this case that's null.

Setting the basedir attribute in the build file does fix the problem, and I
can use it as a workaround for now.  But since I can't always depend on that
being set I need to know if is this a Bug or a Feature?  If the former I'll
submit a bug report.

Thanks,
Rich

Re: NPE thrown from ProjectHelper2.parse() when parsing from URL

Posted by Stefan Bodewig <bo...@apache.org>.
On 2011-09-28, Steele, Richard wrote:

> On Wed, Sep 28, 2011 at 11:48 AM, Stefan Bodewig <bo...@apache.org> wrote:

>> I'm pretty sure I recall this being discussed on the dev list, so it is
>> something the dev community is aware of.  ProjectHelper2 still mostly
>> deals with files only, URL support is largely there for antlibs and
>> import/include.

> That would explain why ProjectHelper.configureProject() isn't overloaded to
> take a Resource in addition to a File?

I think so, yes.

>> Despite some searching around I couldn't find any documentation of this
>> fact, though, so you may want to raise a bug against the Documentation
>> component.

> Is there a better way I should be parsing a project build file given a
> URLResource?

I had a quick look at how ImportTask does it, but it is pretty specific
to import as it provides a parent project, which you don't have.

Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org


Re: NPE thrown from ProjectHelper2.parse() when parsing from URL

Posted by "Steele, Richard" <ri...@steelezone.net>.
On Wed, Sep 28, 2011 at 11:48 AM, Stefan Bodewig <bo...@apache.org> wrote:

> I'm pretty sure I recall this being discussed on the dev list, so it is
> something the dev community is aware of.  ProjectHelper2 still mostly
> deals with files only, URL support is largely there for antlibs and
> import/include.
>

That would explain why ProjectHelper.configureProject() isn't overloaded to
take a Resource in addition to a File?


> Despite some searching around I couldn't find any documentation of this
> fact, though, so you may want to raise a bug against the Documentation
> component.
>

Is there a better way I should be parsing a project build file given a
URLResource?

Thanks,
Rich

Re: NPE thrown from ProjectHelper2.parse() when parsing from URL

Posted by Stefan Bodewig <bo...@apache.org>.
Hi Rich

On 2011-09-28, Steele, Richard wrote:

> The problem appears to be that build files will have a parent build file,
> but in this case that's null.

> Setting the basedir attribute in the build file does fix the problem, and I
> can use it as a workaround for now.  But since I can't always depend on that
> being set I need to know if is this a Bug or a Feature?  If the former I'll
> submit a bug report.

I'm pretty sure I recall this being discussed on the dev list, so it is
something the dev community is aware of.  ProjectHelper2 still mostly
deals with files only, URL support is largely there for antlibs and
import/include.

Despite some searching around I couldn't find any documentation of this
fact, though, so you may want to raise a bug against the Documentation
component.

Stefan

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org