1 2 Previous Next 20 Replies Latest reply on May 10, 2002 3:49 AM by juha

    NullPointerException

    angelawhelan

      hi all,
      I am getting a NullPointerException whose stack trace looks like :
      org.jboss.jmx.client.RMIClientConnectorImpl.getAttribute(RMIClientConnectorImpl.java:416)
      at CustomMonitor.MBeanMonitor.update(MBeanMonitor.java:254)
      at COM.freshtech.SiteScope.AtomicMonitor.monitorUpdate(AtomicMonitor.java)
      at COM.freshtech.SiteScope.AtomicMonitor.run(AtomicMonitor.java)
      at java.lang.Thread.run(Thread.java:484

      when I execute the following code in an MBeanServer client application:
      lConnector.getAttribute(name, "NumDocsArchived")
      (lConnector is of type JMXConnector)

      Does anyone have any ideas what the problem is ?

      The strange thing is that this code works fine when I run it within JBuilder4, but when I run it outside of that in an environment called 'SiteScope', that's when I get the NullPointerException.

      Any help would be very much appreciated.
      Thanks,
      A.

        • 1. Re: NullPointerException

          Here's the source.
          http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/jboss/jboss/src/main/org/jboss/jmx/client/Attic/RMIClientConnectorImpl.java?annotate=1.6.4.1

          It could be a problem with your initial context?
          See lines 95 and 416

          Regards,
          Adrian

          • 2. Re: NullPointerException
            angelawhelan

            hi Adrian,
            I am getting a NullPointerException no matter what method I call from RMIClientConnectorImpl.

            I've checked the initial context and the test scenario which works, and the test scenario which doesn't work, use the exact same initial context...

            ... an intriguing problem, I wonder is the problem that the second test environment where the NullPointerException is being thrown is not capable of connecting via RMI to the MBeanServer, could this be possible ? I don't understand how this could be possible though because all other interaction with the MBean server works fine.

            I notice in the source code of the RMIClientConnectorImpl class that if an exception occurs in, for example, the invoke method, null is returned in the try/catch statment. This certainly is not helping my debugging efforts :(

            Any ideas ?
            Thanks, Angela

            • 3. Re: NullPointerException

              Perhaps you can try a small test program that does
              the jndi lookup? See what you get?

              Regards,
              Adrian

              • 4. Re: NullPointerException
                angelawhelan

                hi Adrian,
                I am not sure exactly what code you are referring to when you say JNDI lookup ? The piece of code which queries the JNDI server ?

                Anyway, I just guessed what you are referring to, and here's the piece of code which I thought was relevant :
                ---------------------------------------------------------
                // Now let's list the available JMX Connectors
                InitialContext lContext = null;
                try {
                lContext = new InitialContext();
                }
                catch( Exception e ) {
                e.printStackTrace();
                }
                myReading= new Integer(5);
                lContext.addToEnvironment("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
                lContext.addToEnvironment("java.naming.provider.url","sun02:1099");
                lContext.addToEnvironment("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");

                Hashtable lProperties = lContext.getEnvironment();
                ----------------------------------------------------------

                This code works fine in both JBuilder4 and in the SiteScope environment.

                Please let me know if we are talking about the same piece of code and what other comments/suggestions you may have.

                Thanks very much,
                A.

                • 5. Re: NullPointerException

                  Try using the context to
                  Object obj = IContext.lookup("jmx:sun02:rmi");
                  System.out.println(obj);
                  System.out.println(obj.getClass());

                  I'm guessing this where your problem is?

                  Regards,
                  Adrian

                  • 6. Re: NullPointerException
                    angelawhelan

                    hi Adrian,
                    I used that code, and it all worked fine, so it doesn't appear to be a JNDI problem.

                    Let me insert the code that I am using, so that you can see exactly what works and what doesn't work in my tests :

                    --------------------------------------------------------
                    // First create a MBeanServer and let it start
                    final MBeanServer lLocalServer = MBeanServerFactory.createMBeanServer();
                    myReading= new Integer(1);

                    // Then register the connector factory
                    final ObjectInstance lFactoryInstance = lLocalServer.createMBean(
                    "org.jboss.jmx.client.ConnectorFactoryService",
                    new ObjectName( lLocalServer.getDefaultDomain(),
                    "name", "ConnectorFactory" ));

                    System.out.println("Found Factory: "+lFactoryInstance.getObjectName());
                    lLocalServer.invoke(lFactoryInstance.getObjectName(),
                    "init",
                    new Object[] {},
                    new String[] {});

                    lLocalServer.invoke(lFactoryInstance.getObjectName(),
                    "start",
                    new Object[] {},
                    new String[] {});


                    getUserInput("\n" +
                    "2. Lookup for all available connectors with the JNDI defined by jndi.properties\n" +
                    "=> hit any key to proceed");

                    // Now let's list the available JMX Connectors
                    InitialContext lContext = null;
                    try {
                    lContext = new InitialContext();
                    }
                    catch( Exception e ) {
                    e.printStackTrace();
                    }

                    lContext.addToEnvironment("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
                    lContext.addToEnvironment("java.naming.provider.url","jnp://sun02:1099");
                    lContext.addToEnvironment("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");

                    Hashtable lProperties = lContext.getEnvironment();
                    ConnectorFactoryImpl.JBossConnectorTester lTester
                    = new ConnectorFactoryImpl.JBossConnectorTester();

                    Iterator lConnectors = (Iterator) lLocalServer.invoke(
                    lFactoryInstance.getObjectName(),
                    "getConnectors",
                    new Object[] {lProperties,lTester},
                    new String[] {
                    lProperties.getClass().getName(),
                    //lTester.getClass().getName()
                    ConnectorFactoryImpl.IConnectorTester.class.getName()
                    }
                    );

                    int lCount = 0;
                    StringBuffer lMessage = new StringBuffer();
                    lMessage.append( "List of all available connectors on your net\n" );
                    lMessage.append( "=========================================\n" );
                    Vector lTemp = new Vector();

                    while( lConnectors.hasNext() ) {
                    ConnectorFactoryImpl.ConnectorName lName = (ConnectorFactoryImpl.ConnectorName) lConnectors.next();
                    lTemp.addElement( lName );
                    lMessage.append( " - " + ( lCount++ ) + ". connector is: " + lName + "\n" );
                    }

                    lMessage.append( "\n" );
                    lMessage.append( "3. Select your connector by entering its number\n" );
                    lMessage.append( "=> hit any key to proceed" );
                    int lChoice = getUserInput( lMessage.toString() );
                    Iterator i = lTemp.iterator();
                    lCount = 0;

                    while( i.hasNext() ) {
                    if( ( lCount++ ) == lChoice ) {
                    break;
                    }
                    }
                    final ConnectorFactoryImpl.ConnectorName lConnectorName = (ConnectorFactoryImpl.ConnectorName) i.next();
                    lMessage.setLength( 0 );
                    lMessage.append(
                    "\n" +
                    "You selected connector: " + lConnectorName + "\n\n"
                    );
                    getUserInput(
                    "5. Connect to the given connector\n" +
                    "=> hit any key to proceed"
                    );


                    // Take the first server and its first protoccol and create a
                    // connection to the remote MBeanServer
                    JMXConnector lConnector = (JMXConnector) lLocalServer.invoke(
                    lFactoryInstance.getObjectName(),
                    "createConnection",
                    new Object[] {lConnectorName},
                    new String[] {lConnectorName.getClass().getName()}
                    );


                    ObjectName name = new ObjectName("Example:name=ConceptTest");

                    myReading = (Integer)(lConnector.invoke(name,
                    "getNumDocsArchived",
                    new Object[] {},
                    new String[] {}));
                    -------------------------------------------------------

                    The point where the exception occurs is at the very last line :
                    lConnector.invoke(name,
                    "getNumDocsArchived",
                    new Object[] {},
                    new String[] {})

                    This code is more-or-less a copy of the code in the TestClient.java test program from the JBoss CVS repository.

                    Any ideas on what the problem might be ?

                    Thanks so much for your help :)
                    A.

                    • 7. Re: NullPointerException

                      > lConnector.invoke(name,
                      > "getNumDocsArchived",
                      > new Object[] {},
                      > new String[] {})

                      Does this work?

                      lConnector.getAttribute(name, "NumDocsArchived")

                      Regards,
                      Adrian

                      • 8. Re: NullPointerException
                        angelawhelan

                        hi Adrian,
                        Nope :( That doesn't work either. At least, it works fine in JBuilder4 but not in SiteScope environment.

                        I reckon that because the code works ok in JBuilder4 and not in the SiteScope environment, that JBuilder4 is doing something that SiteScope is not, the difficult question is what ?

                        Any ideas ?

                        Thanks very much again,
                        A.

                        • 9. Re: NullPointerException
                          angelawhelan

                          I just thought I would include the stack trace from the exception I get when I run the previous piece of code, here it is :

                          java.lang.NullPointerException
                          at org.jboss.jmx.client.RMIClientConnectorImpl.getAttribute(RMIClientConnectorImpl.java:416)
                          at CustomMonitor.MBeanMonitor.update(MBeanMonitor.java:245)
                          at COM.freshtech.SiteScope.AtomicMonitor.monitorUpdate(AtomicMonitor.java)
                          at COM.freshtech.SiteScope.AtomicMonitor.run(AtomicMonitor.java)
                          at java.lang.Thread.run(Thread.java:484)

                          • 10. Re: NullPointerException

                            Have you compared the client jars with what JBuilder
                            is using?

                            Also, you have the source. You could try patching
                            the code to do some better error reporting?

                            Regards,
                            Adrian

                            • 11. Re: NullPointerException
                              angelawhelan

                              hi,
                              It seems that the NullPointerException is occuring the the class : org.jboss.jmx.client.RMIClientConnectorImpl, the member variable : mRemoteConnector, is null.

                              The scenario is that I am using network monitoring software, which allows you to write custom monitors ( Java classes ) from that software. My custom monitor is connecting to the MBean server and querying an MBean.

                              The code which I use for the custom monitor runs fine when run from JBuilder4 on my local Windows NT machine and it also works fine when I run it, via the command line, on the same machine that the JBoss server is running on, albeit in a different JVM.

                              Maybe it's a problem of the monitoring software I'm using, that it doesn't support connecting via RMI.

                              Any ideas, suggestions ?
                              Thanks,
                              A.

                              • 12. Re: NullPointerException

                                That goes back to my original suggestion.

                                The mRemoteConnector will be null when the lookup
                                fails, probably because the jndi properties are not
                                set correctly or jnp-client.jar is not in the classpath.

                                Regards,
                                Adrian

                                • 13. Re: NullPointerException
                                  angelawhelan

                                  The following code works :

                                  Object obj = lContext.lookup("jmx:sun02:rmi");
                                  System.out.println(obj);
                                  System.out.println(obj.getClass());

                                  but the following code doesn't :

                                  System.out.println(lConnector.getAttribute(name, "NumDocsArchived"));

                                  It seems JNDI is set up properly from the first piece of code, I don't have any other ideas why mRemoteConnector is null...

                                  Any ideas ? or have I mis-understood you Adrian ?
                                  A.

                                  • 14. Re: NullPointerException
                                    angelawhelan

                                    I wonder is it something to do with my MBean code ?

                                    As I am new to JBoss, the examples are very simple :)

                                    Here it is :

                                    import java.util.Random;

                                    public class Test implements TestMBean{

                                    public int getNumDocsArchived(){
                                    int randomNumber=0;
                                    randomNumber = new Random().nextInt();

                                    if (randomNumber >=0) return randomNumber;
                                    else return randomNumber*(-1);

                                    }

                                    public void setNumber(){

                                    }
                                    }


                                    public interface TestMBean{

                                    // management operations
                                    public int getNumDocsArchived();
                                    public void setNumber();
                                    }

                                    1 2 Previous Next