user2234
user2234

Reputation: 1302

Issue with @Inject for Constructor

I am new to robojuice, but i need to work on a piece of code which was already built by someone else. I am facing issue if I add an extra parameter to the constructor of a class which already has @Inject. My android application crashes giving the below error with no detail description about the issue:

"java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.app.envTest/com.my.app.navigation.NavigActivity}: com.google.inject.ConfigurationException: Guice configuration errors:"

I am sure this error is not with the activity, but with the new parameter i added to the constructor. If i remove that param it works fine.

Previously:

 @Inject
public PlotRepo(RuntimeExceptionDao<Plot, String> plotDao, RuntimeExceptionDao<LocalPlotData, Long> localPlotDataDao) {
    this.plotDao = plotDao;
    this.localPlotDataDao = localPlotDataDao;
   }

Facing issue for:

 @Inject
public PlotRepo(RuntimeExceptionDao<Plot, String> plotDao, RuntimeExceptionDao<LocalPlotData, Long> localPlotDataDao, RuntimeExceptionDao<LocalSelPlotData, Long> localSelPlotDataDao) {
    this.plotDao = plotDao;
    this.localPlotDataDao = localPlotDataDao;
    this.localSelPlotDataDao = localSelPlotDataDao;
   }

After Debugging i got this error:

1) Could not find a suitable constructor in     com.j256.ormlite.dao.RuntimeExceptionDao. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
  at com.j256.ormlite.dao.RuntimeExceptionDao.class(Unknown Source)
  while locating     com.j256.ormlite.dao.RuntimeExceptionDao<com.myapp.s.b.sets.domain.LocalSelPlotData, java.lang.Long>
  for parameter 2 at com.tp.my.sets.PlotRepo.<init>(Unknown Source)
  while locating com.myapp.s.b.sets.PlotRepo
  for parameter 1 at com.myapp.s.b.GroupingManager.<init>(Unknown Source)
  while locating com.myapp.s.b.GroupingManager
  for field at com.myapp.s.b.navigation.NavigActivity.groupingManager(Unknown    Source)
   while locating com.myapp.s.b.navigation.NavigActivity

Not sure where I am going wrong, also could not find much help on this. Can someone help me figure out the issue.

Thanks

Upvotes: 0

Views: 1967

Answers (2)

user2234
user2234

Reputation: 1302

There were two things that missed in my code. My database classes were missing default constructors. Ormlite needs default constructors, which were missing in some of my classes. After that, I had missed the bindings for the Db annotated dependency like this:

    bind(new TypeLiteral<RuntimeExceptionDao<MyTable, Long>>() {}).toProvider(new DaoProvider<MyTable, Long>(MyTable.class));

After these changes the issue got fixed.

Thanks all for the help!

Upvotes: 1

Grogi
Grogi

Reputation: 2265

Firstly - though this does not explicitly answer your question, but cannot have those two constructors - after Type Erasure they are identical and indistinguishable.

You'd need to explicitly define classes such as

RuntimePlotExceptionDao extends RuntimeExceptionDao<Plot, String>
RuntimeLocalPlotDateExceptionDao extends RuntimeExceptionDao<LocalPlotData, Long>

Secondly - why do you want to have two @Inject constructors? Which one should the container pick when creating the object?

Upvotes: 0

Related Questions