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)