java bean null pointer exception - java

This is my first post, so hopefully I live up to the high standards of the community.
I'm trying to learn Enterprise Java beans and I've been thrown into the deep end and asked to debug an EAR file that's throwing a NullPointerException. This is the line that's throwing the Exception:
private CrashMonitorTimer crash;
...
/*THE NEXT LINE THROWS THE NULL POINTER EXCEPTION*/
crash.createTimer(Long.parseLong(sCrashMonitorInterval),"CrashMoniterScheduler");
This is the code for the interface being called:
import javax.ejb.Local;
#Local
public interface CrashMonitorTimer
{
public abstract void createTimer(long l, String s);
public abstract void cancelTimer(String s);
}
And this is the code for the Java bean:
#Stateless(name = "CrashMonitorBean", mappedName = "CrashMonitorBean")
#Local(CrashMonitorTimer.class)
public class CrashMonitorBean
implements CrashMonitorTimer
{
#Resource
SessionContext sessionCtx;
TimerService timerService;
Timer timer;
int iMsgCntBeforeCtxRenew;
int iCtxReusedCount;
Context _context;
CrashMonitorInfoUtil crashMonitorRMIContext[];
public CrashMonitorBean()
{
iMsgCntBeforeCtxRenew = 10;
iCtxReusedCount = 0;
_context = null;
crashMonitorRMIContext = null;
}
#Override
public void createTimer(long lInterval, String sName)
{
//CrashMonitorInfoUtil leaves context open for reuse
System.out.println((new StringBuilder("Creating ")).append(sName).append(" timer").toString());
timerService = sessionCtx.getTimerService();
timer = timerService.createTimer(lInterval, lInterval, sName);
String sPorts = SystemConfigurator.getConfigValue("RMIPorts");
String saPorts[] = sPorts.split(",");
String server = SystemConfigurator.getConfigValue("host");
crashMonitorRMIContext = new CrashMonitorInfoUtil[saPorts.length];
for(int i = 0; i < saPorts.length; i++)
{
crashMonitorRMIContext[i] = new CrashMonitorInfoUtil(server, saPorts[i]);
}
}
...
}
I've poked around at this, but having had zero experience with Java beans or interfaces (or the new annotations) I'm kind of at a loss on what I should even try. Any explanation or direction would be greatly appreciated.

crash variable is null.
If sCrashMonitorInterval were null, then it would have thrown NumberFormatException

Related

Java annotation not working on Callable

I have writen an annotation (in a spring boot application) and try to apply it to the call() method of Callable, but it doesn't work, yet on the other hand, when applied to a normal method (please see code below), it works, this keeps bothering me, could you please give me some clues? Thank you very much.
Here are my code,
#Target(ElementType.METHOD)
#Retention(RetentionPolicy.RUNTIME)
public #interface LogExecutionTime {
}
#Aspect
#Component
public class LogExecutionTimeAspect {
private static final Logger logger = LoggerFactory.getLogger(LogExecutionTimeAspect.class);
#Around("#annotation(LogExecutionTime)")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
final long start = System.currentTimeMillis();
final Object proceed = joinPoint.proceed();
final long executionTime = System.currentTimeMillis() - start;
logger.info(joinPoint.getSignature() + " executed in " + executionTime + "ms");
return proceed;
}
}
public class DummyCallable implements Callable<Integer> {
private Integer start, count;
DummyCallable() {}
DummyCallable(Integer start, Integer count) {
this.start = start;
this.count = count;
}
#LogExecutionTime // not working...
#Override
public Integer call() throws Exception {
Thread.sleep(start * 1000);
Integer sum = 0;
for (Integer i = start; i <= start + count; i++) {
sum += i;
}
return sum;
}
}
#LogExecutionTime // This will work...
public List<Integer> getAllUserScores() {
Callable c1 = new DummyCallable(1, 100000);
Callable c2 = new DummyCallable(2, 100000);
Callable c3 = new DummyCallable(3, 100000);
// .... run them ...
return result;
}
Inspired by #sbjavateam, I realize that three things,
spring aop work only with object that are managed by spring container. To apply aspect for your class it should be a bean or component and instantiated by spring context. (All right, this is copied from #sbjavateam's answer.)
Based on the former statement, Callable c1 = new DummyCallable(1, 100000); is wrong by nature, since we must create the DummyCallable from the spring context (so that bean will be properly injected with it's dependencies), calling new is not capable.
The DummyCallable class needs to have a scope of prototype so that it is not a singleton. The singleton scope is the default scope for a Spring bean. As a result, this class must have this annotation: #Scope("prototype").
Below is my fix,
#Component
#Scope("prototype")
public class DummyCallable implements Callable<Integer> {}
private DummyCallable createDummyCallable(Integer start, Integer end) {
return context.getBean(DummyCallable.class, start, end);
}
Besides, you might want this configuration as well,
spring.aop.proxy-target-class=true
Last but not least, thank you very much, #sbjavateam.

Xpages: How to ensure my cacheBean only loads once

Still struggling with properly making a cacheBean. I think I want the bean to be a singleton, from what I have read. Will only need
one instance of it. Use it to get often used keywords and so on.
http://blog.defrog.nl/2013/02/prefered-way-for-referencing-beans-from.html
I used this pattern to make my CacheBean (and used a utility method).
If I make this a managedBean by putting it into Faces-config, then I can easily get the value of models
<xp:text escape="true" id="computedField1"
value="#{CacheBean.models}"></xp:text>
The JSF takes care of instantiating the bean for me.
But I don't want it to reload the same values (like models) over and over. I thought that to get that to happen I needed to make
a POJO and grab the currentInstance of the bean, as in the url.
However, when I made this change (taking the bean out of the faces-config file, I cannot seem to get a handle on the properties.
This won't even compile:
<xp:text escape="true" id="computedField1"
value="#{Cache.getCurrentInstance().models}">
</xp:text>
What am I doing wrong?
================================
package com.scoular.cache;
import java.io.Serializable;
import org.openntf.domino.xsp.XspOpenLogUtil;
import com.scoular.Utils;
public class CacheBean implements Serializable {
private static final long serialVersionUID = -2665922853615670023L;
public static final String BEAN_NAME = "CacheBean";
private String pcDataDBpath;
private Vector<Object> models = new Vector<Object>();
public CacheBean() {
initConfigData();
}
private void initConfigData() {
try {
loadModels();
loadDBPaths();
} catch (Exception e) {
XspOpenLogUtil.logError(e);
}
}
// Getters and Setters
public static CacheBean getInstance(String beanName) {
return (CacheBean) Utils.getVariableValue(beanName);
}
public static CacheBean getInstance() {
return getInstance(BEAN_NAME);
}
public String getPcDataDBpath() {
return pcDataDBpath;
}
public void setPcDataDBpath(String pcDataDBpath) {
this.pcDataDBpath = pcDataDBpath;
}
public void loadDBPaths() {
Session session = Factory.getSession();
Database tmpDB = session.getCurrentDatabase();
pcAppDBpath = (tmpDB.getServer() + "!!" + "scoApps\\PC\\PCApp.nsf");
pcDataDBpath = (tmpDB.getServer() + "!!" + "scoApps\\PC\\PCData.nsf");
compDirDBpath = (tmpDB.getServer() + "!!" + "compdir.nsf");
}
public void loadModels() {
try {
Session session = Factory.getSession();
Database tmpDB = session.getCurrentDatabase();
Database PCDataDB = session.getDatabase(tmpDB.getServer(), "scoApps\\PC\\PCData.nsf");
ViewNavigator vn = PCDataDB.getView("dbLookupModels").createViewNav();
ViewEntry entry = vn.getFirst();
while (entry != null) {
Vector<Object> thisCat = entry.getColumnValues();
if (entry.isCategory()) {
String thisCatString = thisCat.elementAt(0).toString();
models.addElement(thisCatString);
}
entry = vn.getNextCategory();
}
} catch (Exception e) {
XspOpenLogUtil.logError(e);
}
}
p
ackage com.scoular;
import javax.faces.context.FacesContext;
public class Utils {
public static Object getVariableValue(String varName) {
FacesContext context = FacesContext.getCurrentInstance();
return context.getApplication().getVariableResolver().resolveVariable(context, varName);
}
}
When the bean has the right scope you can access the bean directly if is created.
private static final String BEAN_NAME = "CacheBean";
//access to the bean
public static CacheBean get() {
return (CacheBean) JSFUtil.resolveVariable(BEAN_NAME);
}
//in my JSFUtil class I have the method
public static Object resolveVariable(String variable) {
return FacesContext.getCurrentInstance().getApplication().getVariableResolver().resolveVariable(FacesContext.getCurrentInstance(), variable);
}
so in a Java Class you can call
CacheBean.get().models
in EL you can use
CacheBean.models
I can tell you why it's not compiling at least.
value="#{Cache.getCurrentInstance().models}"
That's EL. So there should not be a get or a (). You want
value="#{Cache.currentInstance.models}"
And check your var name as I thought you were using CacheBean and not Cache.

Akka: Testing Supervisor Recommendations

I am very new to Akka and using Java to program my system.
Problem definition
- I have a TenantMonitor which when receives TenantMonitorMessage(), starts a new actor DiskMonitorActor.
- The DiskMonitorActor may fail for various reasons and may throw DiskException. The DiskMonitorActor has been Unit Tested.
What I need?
- I want to test behavior TenantMonitorActor, so that when DiskException happens, it takes correct action like stop(), resume() or any (depending upon what my application may need)
What I tried?
Based on the documentation, the closest I could perform is the section called Expecting Log Messages.
Where I need help?
- While I understand the expecting the correct error log is important, it just asserts first part, that exception is thrown and is logged correctly, but does not help in asserting that right strategy is called
Code?
TenantMonitorActor
public class TenantMonitorActor extends UntypedActor {
public static final String DISK_MONITOR = "diskMonitor";
private static final String assetsLocationKey = "tenant.assetsLocation";
private static final String schedulerKey = "monitoring.tenant.disk.schedule.seconds";
private static final String thresholdPercentKey = "monitoring.tenant.disk.threshold.percent";
private final LoggingAdapter logging = Logging.getLogger(getContext().system(), this);
private final Config config;
private TenantMonitorActor(final Config config) {
this.config = config;
}
private static final SupervisorStrategy strategy =
new OneForOneStrategy(1, Duration.create(1, TimeUnit.SECONDS),
new Function<Throwable, Directive>() {
public Directive apply(final Throwable param) throws Exception {
if (param instanceof DiskException) {
return stop();
}
return restart();
}
});
public static Props props(final Config config) {
return Props.create(new Creator<TenantMonitorActor>(){
public TenantMonitorActor create() throws Exception {
return new TenantMonitorActor(config);
}
});
}
#Override
public void onReceive(final Object message) throws Exception {
if (message instanceof TenantMonitorMessage) {
logging.info("Tenant Monitor Setup");
setupDiskMonitoring();
}
}
#Override
public SupervisorStrategy supervisorStrategy() {
return strategy;
}
private void setupDiskMonitoring() {
final ActorRef diskMonitorActorRef = getDiskMonitorActorRef(config);
final FiniteDuration start = Duration.create(0, TimeUnit.SECONDS);
final FiniteDuration recurring = Duration.create(config.getInt(schedulerKey),
TimeUnit.SECONDS);
final ActorSystem system = getContext().system();
system.scheduler()
.schedule(start, recurring, diskMonitorActorRef,
new DiskMonitorMessage(), system.dispatcher(), null);
}
private ActorRef getDiskMonitorActorRef(final Config monitoringConf) {
final Props diskMonitorProps =
DiskMonitorActor.props(new File(monitoringConf.getString(assetsLocationKey)),
monitoringConf.getLong(thresholdPercentKey));
return getContext().actorOf(diskMonitorProps, DISK_MONITOR);
}
}
Test
#Test
public void testActorForNonExistentLocation() throws Exception {
final Map<String, String> configValues =
Collections.singletonMap("tenant.assetsLocation", "/non/existentLocation");
final Config config = mergeConfig(configValues);
new JavaTestKit(system) {{
assertEquals("system", system.name());
final Props props = TenantMonitorActor.props(config);
final ActorRef supervisor = system.actorOf(props, "supervisor");
new EventFilter<Void>(DiskException.class) {
#Override
protected Void run() {
supervisor.tell(new TenantMonitorMessage(), ActorRef.noSender());
return null;
}
}.from("akka://system/user/supervisor/diskMonitor").occurrences(1).exec();
}};
}
UPDATE
The best I could write is to make sure that the DiskMonitor is stopped once the exception occurs
#Test
public void testSupervisorForFailure() {
new JavaTestKit(system) {{
final Map<String, String> configValues =
Collections.singletonMap("tenant.assetsLocation", "/non/existentLocation");
final Config config = mergeConfig(configValues);
final TestActorRef<TenantMonitorActor> tenantTestActorRef = getTenantMonitorActor(config);
final ActorRef diskMonitorRef = tenantTestActorRef.underlyingActor().getContext()
.getChild(TenantMonitorActor.DISK_MONITOR);
final TestProbe testProbeDiskMonitor = new TestProbe(system);
testProbeDiskMonitor.watch(diskMonitorRef);
tenantTestActorRef.tell(new TenantMonitorMessage(), getRef());
testProbeDiskMonitor.expectMsgClass(Terminated.class);
}};
}
Are there better ways?
I have the feeling that testing supervisor strategy is some sort of grey area -- it is up to personal opinion where we start testing Akka itself, instead of one's understanding of how the framework works. Testing validation of entities in ORM frameworks strikes me as a similar problem. We don't want to test whether email validation logic is correct (e.g. in Hibernate), but rather if our rule is correctly declared.
Following this logic, I would write the test as follows:
final TestActorRef<TenantMonitorActor> tenantTestActorRef =
getTenantMonitorActor(config);
SupervisorStrategy.Directive directive = tenantTestActorRef.underlyingActor()
.supervisorStrategy().decider().apply(new DiskException());
assertEquals(SupervisorStrategy.stop(), directive);

addition to Managed Bean Best Practice

This is an extension to ( Managed-Bean best practice ). I have written a class AppProperties that defines the various items in the Class:
public class AppProperties implements Serializable {
private static final long serialVersionUID = 1L;
//contents of AppProperties Object
private String appRepID;
private String helpRepID;
private String ruleRepID;
private String filePath;
private Vector formNames;
//end content
private Session s;
private String serverName;
public String getAppRepID() {
return appRepID;
}
public void setAppRepID(String appRepID) {
this.appRepID = appRepID;
}
//rest if getters and setters
}
In my bean I have the following:
import ca.wfsystems.core.AppProperties;
private final Map<String, AppProperties> internalMap = new HashMap<String, AppProperties>();
public ApplicationMap() {
this.buildMap(internalMap);
}
private void buildMap(Map<String, AppProperties> theMap) {
try{
AppProperties ap = null;
Session s = ExtLibUtil.getCurrentSession();
vwApps = s.getCurrentDatabase().getView("vwWFSApplications");
veCol = vwApps.getAllEntries();
ve = veCol.getFirstEntry();
tVE = null;
while (ve != null){
Vector colVal = ve.getColumnValues();
String tAppRepID = colVal.get(2).toString();
ap.setAppRepID(colVal.get(2).toString());
ap.setHelpRepID(colVal.get(3).toString());
ap.setRuleRepID(colVal.get(4).toString());
theMap.put(colVal.get(0).toString(), ap);
}
}catch(Exception e){
System.out.println(e.toString());
}finally{
Utils.recycleObjects(s,vwApps);
}
}
everything seems to be ok except at ap.setAppRepID(colVal(2).toString()) there is a compiler error that "Null pointer access The variable ap can only be null at this point" the code for the setAppRepID and setHelpRepID are identical and there is no compiler error of either setHelpRepID or setRuleRepID. I'm not sure if the problem is in the setting AppProperties ap = null tried to create AppProperties ap = new AppProperties but it doesn't like that. I think I'm really close to making this work but ....
Thanks to all who have been very patient with me as I climb up the JAVA slope.
the compiler error is correct, your variable ap can only be null at that point.
Follow each statement from
AppProperties ap = null;
to
ap.setAppRepID(colVal.get(2).toString());
and at no point is ap initialised to an object, it will still be null.
You would also see the compiler error on setHelpRepID or setRuleRepID as well but it doesn't bother showing you because it is already a problem with the first statement. You can try this by commenting out the setAppRepID line and you should see the same error on the next line.
make a public constructor in your AppProperties class
public AppProperties() {};
and then try to change
AppProperties ap = null;
to
AppProperties ap = new AppProperties();
It's definitely the "AppProperties ap = null" line. When you say that you tried "AppProperties ap = new AppProprties", did you include "()" at the end (i.e. "AppProperties ap = new AppProperties()")? It looks like your AppProperties bean has the default argumentless constructor, so that should work. Specifically, guessing from your code, I expect you'll want to move that line to just after the opening of the while loop.
You also have an infinite loop lying in wait: you never set the entry to the next one in the while loop. If you're not using the OpenNTF Domino API, I suggest an idiom like this:
ViewEntry entry = veCol.getFirstEntry();
while(entry != null) {
Vector<?> colVal = ve.getColumnValues();
...
entry.recycle(colVal);
ViewEntry tempEntry = entry;
entry = veCol.getNextEntry();
tempEntry.recycle();
}

Java access a public variable outside a class, SecurityException: MIDlet not constructed by createMIDlet

I'm a newbie in java and I have a small problem. I want to access a variable in one class from another. I have three classes and I want to be able to access a variable in the main class to enable me read the array.
The error I am getting is
java.lang.SecurityException: MIDlet not constructed by createMIDlet
Please see the example below. Please bear in mind they're all in the same package.
package tungPackage;
import com.sun.lwuit.*;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import javax.microedition.midlet.MIDlet;
public class TungMidlet extends MIDlet implements ActionListener {
private Command back = new Command("Back");
private Command ok = new Command("Ok");
public ActionListener commandlistListener = new ActionListener() {
public void actionPerformed(ActionEvent cmd) {
// check which command cliked
if (cmd.getCommand() == back) {
// go back to previous form
mainForm.show();
} else if (cmd.getCommand() == ok) {
// go forward
}
}
};
private List list;
private Form mainForm;
private Label promptLabel;
private housesClass houseClassObject = new housesClass();
public int counter; //this is the variable I want to access in a class called calculate class object.
private int sumAmmt;
public TungMidlet tungMidletObject;
public calculateClass calculateClassObject;
public TungMidlet() {
Display.init(this);
}
private ActionListener applistListener = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
if(list.getSelectedIndex()==0){
counter++;
if (counter>5)
{
//check sum price.
sumAmmt = calculateClassObject.calculateSum();
Dialog x = new Dialog("info");
Label label = new Label("Maximum reached.");
Label label2 = new Label("Sum ammt = "+sumAmmt);
x.addComponent(label);
x.addComponent(label2);
x.addCommand(ok);
x.show();
}
else
{
//calculate the price
String info = houseClassObject.randomHouse();
Dialog x = new Dialog("info");
Label label = new Label(info);
x.addComponent(label);
x.addCommand(ok);
x.show();
}
}
}
};
public void startApp() {
//calculateClassObject = new calculateClass();
//sumAmmt = calculateClassObject.calculate(sumAmmt);
mainForm = new Form("Investment Categories");
promptLabel = new Label("choose category");
list = new List();
list.addItem("House");
list.addItem("Cars");
list.addItem("Schools");
list.addItem("Schools");
list.addItem("Supermarkets");
list.addItem("Stocks");
list.addItem("Land");
list.addActionListener(applistListener);
mainForm.addComponent(promptLabel);
mainForm.addComponent(list);
mainForm.addCommand(back);
mainForm.addCommandListener(commandlistListener);
mainForm.setTransitionInAnimator(CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, true, 1000));
mainForm.show();
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
public void actionPerformed(ActionEvent ae) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
The class I want to access the "counter" variable using is shown below.
package tungPackage;
import java.util.Random;
public class housesClass {
public Random generator = new Random();
public String[] houseArray = new String[5];
public housesClass housesClassObject;
public calculateClass calcobj;// = new calculateClass();
public housesClass()
{
}
public String randomHouse() {
housesClassObject = new housesClass();
houseArray[0] = "Bungalow - 20,000,000 Shillings";
houseArray[1] = "Microhouse - 10,000,000 Shillings";
houseArray[2] = "Flat - 200,000,000 shillings";
houseArray[3] = "Garage apartment - 7,000,000 shillings";
houseArray[4] = "Studio apartment - 13,000,000 shillings";
int rnd = generator.nextInt(houseArray.length);
housesClassObject.housePrices(rnd);///noma
String house = houseArray[rnd];
return house;
}
void housePrices(int houseNumber) {
calcobj = new calculateClass();
TungMidlet tungmidobj = new TungMidlet();
int counter = tungmidobj.counter;
int[] housePriceArray = new int[5];
housePriceArray[0] = 20000000;
housePriceArray[1] = 10000000;
housePriceArray[2] = 200000000;
housePriceArray[3] = 7000000;
housePriceArray[4] = 13000000;
int price = housePriceArray[houseNumber];
calcobj.storePrice(counter,price);
}
}
The other supporting class is shown below.
package tungPackage;
public class calculateClass {
int[] storeArray = new int[5];
public calculateClass()
{
}
public void storePrice(int counter, int number2)
{
storeArray[counter] = number2;
}
public int calculateSum()
{
int sum =0;
for(int i=1; i<6; i++){
sum= sum+storeArray[i];
}
return sum;
}
}
Are you getting an error? It looks like your access code should work.
I can't seem to find anywhere that you actually initialise counter though, so maybe your problem is that you need to put counter = 0; somewhere in your code.
Java is also object oriented so you should avoid accessing like the above and make some 'getter and setter' methods:
public int getCounter() {
return counter;
}
and then call int counter = tungmidobj.getCounter();
remove TungMidlet constructor. If there was something useful to do there, you could also declare it protected - but this is not the case with your code snippet, see below.
Wherever you try to invoke that constructor directly, remove code that does this and find another way to do what you need. If needed, study code examples provided in LWUIT Tutorial - Introduction for how typical things are done in LWUIT.
put statement Display.init() in the beginning of the startApp method,
just like it is done in LWUIT Tutorial - Hello, LWUIT! example code
The reason why you are getting SecurityException is because you invoke TungMidlet constructor directly. Don't do that.
MIDP API documentation for MIDlet constructor states:
Throws:
SecurityException - unless the application management software is creating the MIDlet.
one way is
TungMidlet tungMidlet=new TungMidlet();
System.out.println(tungMidlet.counter);
but know encapsulation
second way is
you can make counter private variable and provide setter and getters.
private int counter;
public void setCounter(int counter){
this.counter=counter;
}
public int getCounter(){
return counter;
}
second way is preferred way as it achieves encapsulation

Categories

Resources