-1

I am used the following code in java.I don't know what I did Wrong here.

My main file is:look and check

package com.sample.test;    
import com.google.inject.Guice;
import com.google.inject.Injector;

public class mymain {    
public static void main(String[] args) {    
Injector injector = Guice.createInjector(new AppInjectory());
ApplicationExample obj =                               injector.getInstance(ApplicationExample.class);
obj.sendMessage();    
}    
}

My interface is:look and check

package com.sample.test;    
public interface MessageService {
boolean sendMessage(String msg, String receipient);
}

My config file is look and check

package com.sample.test;
import com.google.inject.AbstractModule;    
public class AppInjectory extends AbstractModule {    
@Override
protected void configure() {     
bind(MessageService.class).to(EmailService.class);    
}    
}

my application file is:look and check

package com.sample.test;
import javax.inject.Inject;    
    public class ApplicationExample {        
    private MessageService service;        
    @Inject
    public void setService(MessageService svc){
    this.service=svc;
    }        
    public void sendMessage() {
    System.out.println(“I am here”);
    service.sendMessage(“welcome”, “java”);        
    }
    }

my service class is :look and check

package com.sample.test;
//import com.google.inject.Singleton;
import javax.inject.Singleton;

@Singleton
public class EmailService implements MessageService {

public boolean sendMessage(String msg, String receipient) {
//some fancy code to send email
System.out.println(“Email Message sent to “+receipient+” with message=”+msg);
return true;
}    
}

Here I am getting NUll pointer exception .What wrong I did here.?please help to fix this issue.I added the error stack trace here. please look at it.

ERROR:

Exception in thread “main” I am here java.lang.NullPointerException at com.sample.test.ApplicationExample.sendMessage(ApplicationExample.java:16) at com.sample.test.mymain.main(mymain.java:13)

free user
  • 11
  • 1
  • 2
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – sidgate Aug 16 '16 at 13:20
  • Please format your code (and make it fit). – zhon Aug 16 '16 at 17:43
  • Is there a reason why `ApplicationExample` is using setter injection and not constructor injection? Constructor injection would make it easier to reason about the invariant that the `MessageService` must not be null. That said, I believe the code you've shown should work. Is this your actual code that's causing the error? – Daniel Pryden Aug 20 '16 at 06:40

1 Answers1

-1

The problem lies in this line:

ApplicationExample obj = injector.getInstance(ApplicationExample.class);

In your AppInjectory module you haven't bound your ApplicationExample interface to an implementation. Did you perhaps mean to do this (deducted from your module):

MessageService obj = injector.getInstance(MessageService.class);
nbokmans
  • 5,492
  • 4
  • 35
  • 59
  • http://www.journaldev.com/2403/google-guice-dependency-injection-example-tutorial#comment-36062 Look at this example.Here they followed what I did here. – free user Aug 16 '16 at 13:48
  • Please explain to me where I did mistake. In the example (http://www.journaldev.com/2403/google-guice-dependency-injection-example-tutorial#comment-36062) .it is working – free user Aug 16 '16 at 14:45
  • Add the stacktrace to your post, and make sure to mention which lines of code the stacktrace entails. – nbokmans Aug 16 '16 at 14:50
  • ERROR: Exception in thread “main” I am here java.lang.NullPointerException at com.sample.test.ApplicationExample.sendMessage(ApplicationExample.java:16) at com.sample.test.mymain.main(mymain.java:13) – free user Aug 17 '16 at 05:53
  • `ApplicationExample` appears to be a class, not an interface. It has a parameterless constructor and an `@Inject`-annotated setter, so it should be recognized as a JIT binding by Guice, so `injector.getInstance(ApplicationExample.class)` should work fine with this code. – Daniel Pryden Aug 20 '16 at 06:37