11 Replies Latest reply on Dec 20, 2007 6:33 AM by pmuir

    Trouble W/ NullPointerException

    nathandennis

      Having trouble with a NPE. I'm sure this is something simple
      jboss4.2.1
      seam 2.0 +

      @Stateful
      @Name("uploadAction")
      @Scope(ScopeType.SESSION)
      public class FileUploadAction implements FileUploadLocal{
      
       @PersistenceContext
       private EntityManager em;
      
       @Out(scope=ScopeType.CONVERSATION, required=false)
       FtpRemote send;
      
      
       private String medianame;
       private Long size;
       .
       .
       .
      
       public void UploadFile() throws NullPointerException {
      
      
       if(!medianame.equals("")){
       try{
      // create empty file with specified name and path
       File os = new File("/var/temp/filewritetemp/"+getMediaName());
       FileOutputStream oStream =new FileOutputStream(os);
       oStream.write(getIs());
      
      
      
      
       File file = new File("/var/temp/filewritetemp/" + getMediaName());
       size = file.length();
       dob = new Date();
       fis = new FileInputStream("/var/temp/filewritetemp/" + getMediaName());
       try{
       ftpFile(fis);
       }catch (Exception e){
       e.printStackTrace();
       }
       .
       .
       .
       }
      
      
       public void ftpFile(FileInputStream fis){
       send = new FtpRemote();
       send.findServer(fis);
       }
      ...


      which calls

      @Stateless
      @Name("ftpRemote")
      public class FtpRemote implements FtpRemoteLocal {
      
       private String login;
       .
       .
       .
      
       @In
       EntityManager em;
      
       @In(required=false)
       @Out(required=false)
       ReturnServer host;
      
       public void findServer(FileInputStream fis){
       host = new ReturnServer();
      
       ....


      and it breaks just before the query
      
      @Stateless
      public class ReturnServer
      {
       @In
       EntityManager em;
      
       private String server;
       private ArrayList<Server> serverlist;
       .
       .
       .
      
      
       @In(create=true)
       private ServerList result;
      
       public String findServer(){
       queryServer();
       requestServer();
       return server;
       }
      
       @SuppressWarnings("unchecked")
       public void queryServer() {
       System.out.println("made it to -- line before QUERY");
       List<Server> resultList = em.createQuery("select s from " +
       "Server s where s.status = 'online' order by s.usedspace desc")
       .setMaxResults(5).setFirstResult(0).getResultList();
       System.out.println("made it to -- line after QUERY");
       serverlist = new ArrayList(resultList.subList(0, resultList.size()));
      
       }
      



      I've convinced myself that this is a fundamental misunderstanding EJB3 annotations. I'm just not sure how to fix it.
      [q22:35:16,598 INFO [STDOUT] made it to -- line before QUERY
      22:35:16,598 ERROR [STDERR] java.lang.NullPointerException
      22:35:16,599 ERROR [STDERR] at com.dcg.action.ReturnServer.queryServer(ReturnServer.java:66)
      22:35:16,599 ERROR [STDERR] at com.dcg.action.ReturnServer.findServer(ReturnServer.java:41)
      


      any direction would be greatly appreciated.

        • 1. Re: Trouble W/ NullPointerException
          nathandennis

          bump

          • 2. Re: Trouble W/ NullPointerException
            andygibson

            Your return server class doesn't have a @Name annotation which means it is not a seam component. Try adding a name to it unless you are defining it in components.xml.

            How is it even getting to this code since without the name, the component cannot be injected.

            Beyond that, I would try checking each piece of the query operation to see which item is null (the entitymanager, or the query).

            Cheers,

            Andy


            • 3. Re: Trouble W/ NullPointerException
              damianharvey

              Well not knowing your components.xml, I'd hazard a guess that maybe you should check that your entityManager is null? Put in a log line checking something like 'em==null' (or run it through a debugger).

              And why not use

              @In
              EntityManager entityManager;
              rather than PersistenceContext? You define your entityManager in your <persistence:managed-persistence-context> in your components.xml.

              Cheers,

              Damian.

              • 4. Re: Trouble W/ NullPointerException
                nathandennis

                thanks for the responses. unfortunately im still struggling. i tried both of your recommendations with no luck. here is the entity manager as declared in my components.xml

                 <persistence:managed-persistence-context name="entityManager"
                 auto-create="true"
                 persistence-unit-jndi-name="java:/pictureEntityManagerFactory"/>
                


                i didnt find much with debug either. apparently im not doing something right.

                2007-12-16 21:26:11,361 DEBUG [org.jboss.seam.Component] instantiating Seam component: org.jboss.seam.web.parameters
                2007-12-16 21:26:11,361 DEBUG [org.jboss.seam.Component] initializing new instance of: org.jboss.seam.web.parameters
                2007-12-16 21:26:11,361 DEBUG [org.jboss.seam.Component] done initializing: org.jboss.seam.web.parameters
                2007-12-16 21:26:11,370 ERROR [STDERR] java.lang.NullPointerException
                2007-12-16 21:26:11,371 ERROR [STDERR] at com.dcg.action.ReturnServer.queryServer(ReturnServer.java:76)
                2007-12-16 21:26:11,371 ERROR [STDERR] at com.dcg.action.ReturnServer.findServer(ReturnServer.java:51)
                2007-12-16 21:26:11,371 ERROR [STDERR] at com.dcg.action.FtpRemote.findServer(FtpRemote.java:66)
                2007-12-16 21:26:11,372 ERROR [STDERR] at com.dcg.action.FileUploadAction.ftpFile(FileUploadAction.java:120)
                2007-12-16 21:26:11,372 ERROR [STDERR] at com.dcg.action.FileUploadAction.UploadFile(FileUploadAction.java:82)
                2007-12-16 21:26:11,372 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                2007-12-16 21:26:11,372 ERROR [STDERR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                2007-12-16 21:26:11,372 ERROR [STDERR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                2007-12-16 21:26:11,372 ERROR [STDERR] at java.lang.reflect.Method.invoke(Method.java:585)
                2007-12-16 21:26:11,372 ERROR [STDERR] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:112)
                2007-12-16 21:26:11,372 ERROR [STDERR] at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
                2007-12-16 21:26:11,372 ERROR [STDERR] at org.jboss.seam.intercept.EJBInvocationContext.proceed(EJBInvocationContext.java:44)
                2007-12-16 21:26:11,372 ERROR [STDERR] at org.jboss.seam.intercept.SeamInvocationContext.proceed(SeamInvocationContex
                


                could this have something to do with the way i called these classes
                using "new" ????

                • 5. Re: Trouble W/ NullPointerException
                  saeediqbal1

                  Please try using the setParameter(s) instead. see if the query works then. It probably doesnt see the params at all.

                  • 6. Re: Trouble W/ NullPointerException
                    nathandennis

                    changed it to

                    List resultList = entityManager.createQuery("select s from " +
                     "Server s where lower (s.status) = :param order by s.usedspace desc")
                     .setParameter("param", "online")
                     .getResultList();
                    


                    same error.

                    i did find my entityManager is null for some reason. once again, i think this is a misconception of ebj3 that i must have.
                    i have even tried switching these to stateful beans and still null entitiyManager.
                    i should point out that this is a seam-gen base i am working off of. i have used entityManager.persist, create, remove etc. else where with no problem. it only happens when i have it in an object that i have called using "new"

                    • 7. Re: Trouble W/ NullPointerException
                      saeediqbal1

                      Try using this instead of @In with em

                      @PersistenceContext
                      EntityManager em;

                      • 8. Re: Trouble W/ NullPointerException
                        nathandennis

                        i actually already tried that. but the entity manager is still null. im starting to wonder if it is a bug. this version is cvs 11/15/07. here is an updated code snippet.

                        @Stateful
                        @Name("uploadAction")
                        @Scope(ScopeType.SESSION)
                        public class FileUploadAction implements FileUploadLocal, Serializable{
                        
                         @PersistenceContext
                         private EntityManager entityManager;
                        
                         @Logger
                         private Log log;
                        
                         FtpRemote send;
                        
                         @Out(required=false)
                         private Media media;
                        
                         private String medianame;
                         private Long size;
                         private String userid;
                         private String comment;
                         private String server;
                         private String url;
                         private String filetype;
                         private Date dob;
                         private Long mark;
                         private FileInputStream fis;
                         private byte[] is;
                        
                         public void UploadFile() throws NullPointerException {
                        
                        
                         if(!medianame.equals("")){
                         try{
                        // create empty file with specified name and path
                         File os = new File("/var/temp/filewritetemp/"+getMediaName());
                         FileOutputStream oStream =new FileOutputStream(os);
                         oStream.write(getIs());
                        
                        
                        
                        
                         File file = new File("/var/temp/filewritetemp/" + getMediaName());
                         size = file.length();
                         dob = new Date();
                         fis = new FileInputStream("/var/temp/filewritetemp/" + getMediaName());
                         try{
                         ftpFile(fis);
                        
                         Media media = new Media();
                         media.setServer(server);
                         media.setUrl(url);
                         media.setDob(dob);
                         media.setFiletype(filetype);
                         media.setMark(mark);
                         media.setMedianame(medianame);
                         media.setSize(size);
                         media.setUserid(userid);
                         entityManager.persist(media);
                         }catch (Exception e){
                         e.printStackTrace();
                         }
                         file.delete();
                        // Media media = ;
                        /* media.setServer(server);
                         media.setUrl(url);
                         media.setDob(dob);
                         media.setFiletype(filetype);
                         media.setMark(mark);
                         media.setMedianame(medianame);
                         media.setSize(size);
                         media.setUserid(userid);
                         entityManager.persist(media);
                        */ destroy();
                        
                         } catch(Exception e){
                         e.printStackTrace();
                        
                         }
                         }
                         }
                         public void ftpFile(FileInputStream fis){
                         send = new FtpRemote(); //action starts
                         send.findServer(fis);
                         }
                        ...
                        

                        now to process the file..

                        
                        @Stateful
                        @Name("ftpRemote")
                        public class FtpRemote implements FtpRemoteLocal, Serializable {
                         /**
                         */
                         private String login;
                         private String password;
                         private String directory;
                         private String path;
                         private String filename;
                         private String server;
                         private Long serverid;
                         private Long usedspace;
                         private int reply;
                         private int replywrite;
                         private byte[] filestream;
                         private boolean successflag;
                        
                         @PersistenceContext
                         private EntityManager entityManager;
                        
                         @In(required=false)
                         @Out(required=false)
                         ReturnServer host;
                        
                         @In(required=false)
                         @Out(required=false)
                         ReturnServer host2;
                        
                        
                         public void findServer(FileInputStream fis){
                         host = new ReturnServer(); //return an available server from DB second action
                         server = host.findServer();
                         serverid = host.getServerId();
                         login = host.getLogin();
                         password = host.getPasswd();
                         usedspace = host.getUsedSpace();
                         successflag = ftpConnect(fis);
                         host.destroy();
                         for(boolean flag=successflag; flag==true;){
                         host2 = new ReturnServer();
                         server = host2.findServer();
                         serverid = host2.getServerId();
                         login = host2.getLogin();
                         password = host2.getPasswd();
                         usedspace = host2.getUsedSpace();
                         host2.destroy();
                         flag = ftpConnect(fis);
                         }
                         }
                        

                        the query to return the server is where it is breaking... EM is null for some reason.

                        @Stateful
                        @Name("returnServer")
                        public class ReturnServer implements ReturnServerLocal, Serializable
                        {
                         @PersistenceContext
                         private EntityManager entityManager;
                        
                         private String server;
                         private ArrayList<Server> serverlist;
                        
                         private String login;
                         private String passwd;
                         private Long usedspace;
                         private Long serverid;
                         private int serverindex;
                         private int noavail;
                        
                         @Logger private Log log;
                        
                         @In(create=true)
                         private List<Server> result;
                        
                         @Create
                         public String findServer(){
                         queryServer();
                         requestServer();
                         return server;
                         }
                        
                         public void requestServer() {
                         int i = 0;
                         noavail = serverlist.size();
                         if(serverlist.isEmpty()){
                         System.out.println("null query");
                         } else {
                         for (i = 0; i == serverlist.size() + 1; i++) {
                         if(serverlist.get(i).getUsedspace()
                         < serverlist.get(i).getTotalspace()) {
                         server = serverlist.get(i).getServername();
                         login = serverlist.get(i).getLogin();
                         passwd = serverlist.get(i).getPasswd();
                         serverid = serverlist.get(i).getId();
                         }}
                         }
                         }
                        
                         @SuppressWarnings("unchecked")
                         public void queryServer() {
                         System.out.println("made it to RemoteServer -- before QUERY");
                         if(entityManager==null){
                         System.out.println("em is null");
                         }
                         if(entityManager != null){
                         System.out.println("em not null");
                         }
                        //
                         try{
                         log.info("i am here");
                        
                         List resultList = entityManager.createQuery("select s from " +
                         "Server s where lower (s.status) = :param order by s.usedspace desc")
                         .setParameter("param", "online")
                         .getResultList();
                         serverlist = new ArrayList(resultList.subList(0, resultList.size()));
                         }catch (Exception e){
                         e.printStackTrace();
                         }
                        
                        
                         }
                        ...


                        i local interfaces for all. i thought there might have been something i was missing. i cant really find anything similar in the examples and google is really letting me down too.


                        • 9. Re: Trouble W/ NullPointerException
                          pmuir

                          This is not a valid way to instantiate a Seam component

                          host = new ReturnServer();


                          You need something like

                          Component.getInstance("host");


                          • 10. Re: Trouble W/ NullPointerException
                            nathandennis

                            pete,, i apologize. i know you have to be tired of telling people that. i found the rest of what i needed in some of your other post.

                            ReturnServerLocal host = (ReturnServerLocal) Component.getInstance("returnServer");
                            

                            not only did instantiating the object correctly fix my NPE,,, if fixed the log not working mystery... which when i think about it now,, makes really good sense that it didnt work without being properly wrapped.

                            i have to say after four days of beating around the bush trying to figure out something so simple,, that was the sweetest infinite loop i have hung my app server with in a while.

                            post your donate button and i'll buy you a beer.

                            • 11. Re: Trouble W/ NullPointerException
                              pmuir

                              Answering questions where people post all the information necessary to answer the question without prompting (which you did) in a concise form is a joy :) In short, if you take effort over your post you are more likely to get a good response (as this thread showed).

                              Beer is always good!