0

I am having trouble figuring out why an EJB session bean is not working. The actual error message is an EJBException: NameNotFoundException, but that is not a very illuminating message.

I have traced it down to a exactly what line causes the problem, but have not figure out why. So, I wanted to create a session bean to keep track of the input values from a form.

A slimmed down version of the code is:

public class rrpInputField {
   public boolean isRequired;
   public int maxLength;
   public String inputValue;
   public String displayValue;
   public String formatMask;
   public rrpInputField() {
     isRequired = false;
     maxLength  = 64;
     inputValue = "";
     displayValue = "";
     formatMask   = "";
  }
}

I then created a interface dohicky...

@Local
public interface Test1  {
   public void   setAction(String action);
   public String getAction();
   public void   setName(String name);
   public String getName();
}

Then I created the test bean itself...

@Stateful
public class Test1Bean implements Test1 {
   private String         action;
   private rrpInputField  name;

   @PostConstruct
   public void initialize() {
        action = "initalValue";
        //name.currentValue = "TestValue";

    @Override
    public void setAction(String action){ this.action = action; }

    @Override
    public void getAction() { return this.action; }

    @Override
    public void setName(String name) { this.name.currentValue = name; }

    @Override
    public String getName() { return this.name.currentValue; }

}

In my test servlet I have

 @EJB
 private  Test1   t1;

If I un-comment the one line in the bean initialize method in the bean definition I get the failure. So I know it has something to do with that. //name.currentValue = "TestValue";

If I leave it commented out, as soon as I code t1.getName("New Value") in the servlet I'll get the same error.

If I leave it commented it out, then the bean works as anticipated - I can initialize, and use setAction and getAction just fine.

I am fairly sure the rrpInput class is correct, because I can code in the servlet:

   rrpInputField f1 = new rrpInputField();
   f1.currentValue  = "TestValue";

I figure it must have something to do with my input field class, but I have had no luck figuring out what.

RRP
  • 1
  • 1
  • 1
    sounds like a basic NullPointerException. Assign an instance to name before you access the fields. "name = new rrpInputField();" – k5_ Jul 01 '16 at 23:37

1 Answers1

0

I really don't understand why, but I got it to work by adding a "new" thingie tin the initialize method of the Table1Bean.

@PostConstruct    
public void initialize() {
   action = "initialValue";
   name = new rrpInputField();
   name.currentVAlue = "TestValue;
}

If someone could explain why I had to do this, it would be illuminating.

RRP
  • 1
  • 1
  • http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – k5_ Jul 03 '16 at 21:38