This discussion is archived
11 Replies Latest reply: Aug 27, 2012 5:22 AM by linkin RSS

null pointer exception

951645 Newbie
Currently Being Moderated
hi everybody

i want to make a durable subscriber jms aplication and i have make a test program to see if all is fine
but when i publish the event i get a nullpointerexception i don' t understand why ?

public class TestAgendaDurable extends junit.framework.TestCase{


private SynchronousQueue<RendezVous> queue1,queue2,queue3;

public void test_souscripteur_durable(){
Souscription s1=null,s2=null,s3=null;
PriseDeRDV priseDeRDV=null;
queue1 = new SynchronousQueue<RendezVous>();
try{

priseDeRDV = new PriseDeRDV("agenda_tests");
RendezVous r1 = new RendezVous(new Date(40000L),"test");
if (priseDeRDV != null && r1 != null)
{
priseDeRDV.publier(r1); <========================= here i get a nullpointerexception
}

// System.out.println("envoi !!!" + r1);

// s1 = new SouscriptionDurable("agenda_tests","durable3"){
// public void onMessage(Message message){
// try{
// RendezVous rdv = (RendezVous)((ObjectMessage)message).getObject();
// System.out.println("recu !!!" + rdv);
// queue1.put(rdv);
// }catch(Exception e) {
          // }
// }
// };

// RendezVous r = queue1.poll(2L, TimeUnit.SECONDS);
// assertNotNull(" le rendezVous n'est pas reçu, en moins de 2 sec ?",r);
// System.out.println("recu !!!" + r);
// assertTrue(" le rendezVous n'est pas le bon ?",r.equals(r1));
}catch(Exception e){
fail("exception inattendue : " + e.getClass().getName()); <=========== the line which provoke the exception i can' t remove
}finally{
try{s1.close();}catch(Exception e){}
try{priseDeRDV.close();}catch(Exception e){}

Edited by: migacti on 28 juil. 2012 08:22
  • 1. Re: null pointer exception
    EJP Guru
    Currently Being Moderated
    With that catch block you can't possibly tell which line of code threw the exception. You must be guessing. Print the stacktrace.
  • 2. Re: null pointer exception
    951645 Newbie
    Currently Being Moderated
    hi thanks for your first response

    putting e.printStackTrace() like this

    ==>fail("exception inattendue : " + e.getClass().getName() + e.printStackTrace());
    have send me back a void error

    ==>e.printStackTrace();
    fail("exception inattendue : " + e.getClass().getName());

    has done nothing

    anyway i have investigated the line that's seems to me provoke the exception is astonishment:

    priseDeRDV.publier(r1);

    Edited by: migacti on 28 juil. 2012 08:32
  • 3. Re: null pointer exception
    DrClap Expert
    Currently Being Moderated
    migacti wrote:
    ==>fail("exception inattendue : " + e.getClass().getName() + e.printStackTrace());
    have send me back a void error
    Of course it does. The printStackTrace method doesn't return anything so naturally you can't use it in an expression. Just call the method, don't try to use it in an expression.
  • 4. Re: null pointer exception
    951645 Newbie
    Currently Being Moderated
    hello
    if i make this code the test succeed there is no error

    fail("exception inattendue : ");
    }catch(Exception e){
    e.printStackTrace();

    }finally{

    but if i put the expression fail in the process of the exception like this

    }catch(Exception e){
    fail("exception inattendue : " + e.getClass().getName());
    }finally{

    it doesn' work
    i want to know what's the good programming practice
    i wonder if there is no exception in the first case why the fail expression is called.
    i am very curious about the handling of exceptions for my programming experience even this practices are complex
  • 5. Re: null pointer exception
    EJP Guru
    Currently Being Moderated
    fail() is called in the first case because there is no exception, so the code always gets to it. But I'm having a very hard time believing any of this. I strongly suspect you aren't running the code you think you're running in at least someof these cases.
  • 6. Re: null pointer exception
    951645 Newbie
    Currently Being Moderated
    do not take car of this line wrong operation

    Edited by: migacti on 29 juil. 2012 10:47

    Edited by: migacti on 29 juil. 2012 10:52
  • 7. Re: null pointer exception
    EJP Guru
    Currently Being Moderated
    I've just answered that. If you don't understand, say so, don't just repeat the question. It annoys people. People who are trying to help you. The issue here is that you are calling fail() in a success case, at the end of the try block. You can only get there if there was no exception. So, you shouldn't call fail() from that place. You programmed it wrong.
  • 8. Re: null pointer exception
    951645 Newbie
    Currently Being Moderated
    My conclusion is that the methode fail()
    force the failure of the exception so the test must be realize without this line.
    i' am not the owner of this code, i just work on it.
    i think it's a bad programming practice, i will continue my work and still look for information about this subject.

    thanks for your help

    i would prefer this:
    }catch(Exception e){
    e.printStackTrace();

    }finally{

    i don' t know why the owner have put a fail expression in the catch block
    the nullexception is thrown whereas neither the publier and the information published are not null.
    (see the code below i have verify each object if they are null before using them).
  • 9. Re: null pointer exception
    EJP Guru
    Currently Being Moderated
    I agree, but as this is clearly a JUnit test there is no reason to catch the exception at all. JUnit will catch it and report it as a failure.
  • 10. Re: null pointer exception
    951645 Newbie
    Currently Being Moderated
    hi good morning,

    i have finally a trace of what happens on the server:

    javax.naming.NoInitialContextException: Cannot instantiate class: org.exolab.jms.jndi.InitialContextFactory [Root exception is java.lang.ClassNotFoundException: org.exolab.jms.jndi.InitialContextFactory]
         at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:657)
         at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
         at javax.naming.InitialContext.init(InitialContext.java:223)
         at javax.naming.InitialContext.<init>(InitialContext.java:197)
         at question2.PriseDeRDV.<init>(PriseDeRDV.java:29)
         at question3.TestAgendaDurable.test_souscripteur_durable(TestAgendaDurable.java:22)
         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
         at java.lang.reflect.Method.invoke(Method.java:585)
         at junit.framework.TestCase.runTest(TestCase.java:154)
         at junit.framework.TestCase.runBare(TestCase.java:127)
         at junit.framework.TestResult$1.protect(TestResult.java:106)
         at junit.framework.TestResult.runProtected(TestResult.java:124)
         at junit.framework.TestResult.run(TestResult.java:109)
         at junit.framework.TestCase.run(TestCase.java:118)
         at junit.framework.TestSuite.runTest(TestSuite.java:208)
         at junit.framework.TestSuite.run(TestSuite.java:203)
         at bluej.runtime.RemoteTestRunner.doRun(RemoteTestRunner.java:69)
         at bluej.runtime.ExecServer.runTestMethod(ExecServer.java:641)
         at bluej.runtime.ExecServer.access$700(ExecServer.java:58)
         at bluej.runtime.ExecServer$3.run(ExecServer.java:869)
    Caused by: java.lang.ClassNotFoundException: org.exolab.jms.jndi.InitialContextFactory
         at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
         at java.security.AccessController.doPrivileged(Native Method)
         at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
         at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
         at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
         at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
         at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
         at java.lang.Class.forName0(Native Method)
         at java.lang.Class.forName(Class.java:242)
         at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:42)
         at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:654)
         ... 21 more
    java.lang.NullPointerException
         at question2.PriseDeRDV.publier(PriseDeRDV.java:60)
         at question3.TestAgendaDurable.test_souscripteur_durable(TestAgendaDurable.java:26)
         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
         at java.lang.reflect.Method.invoke(Method.java:585)
         at junit.framework.TestCase.runTest(TestCase.java:154)
         at junit.framework.TestCase.runBare(TestCase.java:127)
         at junit.framework.TestResult$1.protect(TestResult.java:106)
         at junit.framework.TestResult.runProtected(TestResult.java:124)
         at junit.framework.TestResult.run(TestResult.java:109)
         at junit.framework.TestCase.run(TestCase.java:118)
         at junit.framework.TestSuite.runTest(TestSuite.java:208)
         at junit.framework.TestSuite.run(TestSuite.java:203)
         at bluej.runtime.RemoteTestRunner.doRun(RemoteTestRunner.java:69)
         at bluej.runtime.ExecServer.runTestMethod(ExecServer.java:641)
         at bluej.runtime.ExecServer.access$700(ExecServer.java:58)
         at bluej.runtime.ExecServer$3.run(ExecServer.java:869)
  • 11. Re: null pointer exception
    linkin Newbie
    Currently Being Moderated
    migacti wrote:
    hi good morning,

    i have finally a trace of what happens on the server:

    javax.naming.NoInitialContextException: Cannot instantiate class: org.exolab.jms.jndi.InitialContextFactory [Root exception is java.lang.ClassNotFoundException: Your application is unable to load the Connection factory class "org.exolab.jms.jndi.InitialContextFactory". Put the proper jar in classpath.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points