1

I have a Java class in a xtend file that uses Guice, like this:

class myClass {
  @Inject private extension classA
  @Inject private extension classB

  // methods
  // ...
}

I want to add an integer field and modify the default constructor to set it by using a helper class IdProvider, doing something like this:

import some.package.IdProvider

class myClass {
  @Inject private extension classA
  @Inject private extension classB

  private long mMaxId

  new() {
    var IdProvider provider
    mMaxId = provider.getMaxId("Student")
  }

  // methods
  // ...
}

However this doens't work, I get this error:

com.google.inject.ProvisionException: Guice provision errors. Error injecting constructor, java.lang.NullPointerException

I have also tried this, but I get the same error:

import some.package.IdProvider

class myClass {
  @Inject private extension classA
  @Inject private extension classB

  private long mMaxId
  @Inject private IdProvider provider

  new() {
    mMaxId = provider.getMaxId("Student")
  }

  // methods
  // ...
}

I am new to xtend and Guice, so any help understanding how to get this to work would be appreciated.

Renoa
  • 385
  • 1
  • 4
  • 10

1 Answers1

1

I think I found an answer to this. I tracked the origin of the error to the constructor of IdProvider. In its constructor, it gets a file using getResource() like so:

URL theResource = getClass().getResource("aFile.txt");
File aFile = new File(FileLocator.toFileURL(theResource).getPath());

I found this bug report with the same error message as I am seeing. My understanding is that FileLocator will work if you use Eclipse, but not if the code/tests are run from the command line (which is what I am doing).

I have changed the way I get the resource to this:

InputStream theResource = getClass().getResourceAsStream("aFile.txt");

and the error has gone away, so I think Guice/xtend had nothing to do with it.

Renoa
  • 385
  • 1
  • 4
  • 10