title: Returning a value from a custom JNDI resource in GlassFish 4.1
body: <p>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).</p>
  
  <p>While I am getting a null pointer exception (NPE), please do not point me here, it doesn't help me at all. <em><a href="https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it">What is a NullPointerException, and how do I fix it?</a></em></p>
  
  <p>Here are my supporting classes...</p>
  
  <p>Creating the bean</p>
  
  <pre><code>   @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");
      }
  }
  </code></pre>
  
  <p>This is my bean manager:</p>
  
  <pre><code> @ManagedBean
   @ViewScoped
      public class GetCreds {
          @Inject
          private JndiProperties property;
          public String getUserName(){
              return property.getUser();
          }
          public String getPassword(){
              return property.getPass();
          }
      }
  </code></pre>
  
  <p>And this is how I call them</p>
  
  <pre><code>GetCreds creds = new GetCreds();
  String username = creds.getUserName();
  String pass =  creds.getPassword();
  </code></pre>
  
  <p>I named the resource jndiCreds and have the names UserName and UserPass with the values containing respective data.</p>
  
  <p>Here is the view from the GlassFish GUI:</p>
  
  <p><a href="https://i.stack.imgur.com/i8KpZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/i8KpZ.png" alt="Enter image description here"></a></p>
  
  <p>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.</p>
  
  <p>Help would be appreciated; I am very stuck.</p>
  
  <p>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:</p>
  
  <pre><code>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;
      }
  </code></pre>
  
  <p>And this is how I grab the information:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>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.</p>