2

I have tried to implement this many ways, but this is the way that makes the most sense to me, and I am still unable to return anything from my resource. I added the resource with the GlassFish admin GUI (essentially, i am trying to save username and passwords on the local server).

While I am getting a null pointer exception (NPE), please do not point me here, it doesn't help me at all. What is a NullPointerException, and how do I fix it?

Here are my supporting classes...

Creating the bean

   @LocalBean
   @Stateless

public class JndiProperties {

    @Resource(name="jndiCreds")
    private Properties properties;

    public String getUser() {
        return properties.getProperty("UserName");
    }
    public String getPass() {
        return properties.getProperty("UserPass");
    }
}

This is my bean manager:

 @ManagedBean
 @ViewScoped
    public class GetCreds {
        @Inject
        private JndiProperties property;
        public String getUserName(){
            return property.getUser();
        }
        public String getPassword(){
            return property.getPass();
        }
    }

And this is how I call them

GetCreds creds = new GetCreds();
String username = creds.getUserName();
String pass =  creds.getPassword();

I named the resource jndiCreds and have the names UserName and UserPass with the values containing respective data.

Here is the view from the GlassFish GUI:

Enter image description here

Have any idea WHY it won't return my requested information? I AM receiving an NPE when I try to call the resource when I call either function from getCreds.

Help would be appreciated; I am very stuck.

I decided to step away from trying to use a bean and just accessing it directly (although I am giving up some security here). I am trying to access the data in a contextual manner. BUT! I still can not do it! Here is my NEW supporting class:

public class JndiProperties {

    public Properties getProperties(String jndiName) {
        Properties properties = null;
        try {
            InitialContext context = new InitialContext();
            properties = (Properties) context.lookup(jndiName);
            context.close();
        }
        catch (NamingException e) {

            return null;
        }
        return properties;
    }

And this is how I grab the information:

JndiProperties creds = new JndiProperties();

String username = creds.getProperties("jndiCreds").getProperty("UserName");
String pass =  creds.getProperties("jndiCreds").getProperty("UserPass");

String credentials = String.join(System.getProperty("line.separator"),
                                 "user=" + username,
                                 "password=" + pass);
System.out.print(credentials);

I am using the same resource shown above. I am STILL ending up with null pointer... ANY help would be greatly appreciated.Feel free to answer what was wrong with my bean implementation also.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jason V
  • 623
  • 1
  • 10
  • 30
  • Try using `lookup` instead of `name`... `@Resource(lookup="jndiCreds")` I believe the `lookup` attribute will do a global lookup, though I can't find a source for that offhand. – Mike Jun 09 '17 at 14:27
  • 1
    Here's the Javadoc: http://docs.oracle.com/javaee/7/api/javax/annotation/Resource.html#lookup-- – Mike Jun 09 '17 at 14:27
  • It appears that 'property' never takes on the resource values. idk how to fix that either – Jason V Jun 09 '17 at 16:52

1 Answers1

0

I have figured it out!

What was happening is first, I was a complete idiot, and I was trying to test my program with JUnit (IT DOES NOT RUN ON THE SERVER!)

Since the program wasn't being run on GlassFish, it couldn't access the resource.

Secondly, (and most importantly) I was missing the appserver-rt.jar- very important.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jason V
  • 623
  • 1
  • 10
  • 30
  • other than these problems, the program runs great! so feel free to use the setup as a template! – Jason V Jun 12 '17 at 17:06