11 Replies Latest reply on Sep 29, 2009 11:23 PM by Val Sw

    Pls suggest how to inject list object in Home or any bean - NullPointerException

    Val Sw Expert
      Hi,

      I have a entity Item having attribute as itemName, etc... and ItemHome, ItemList.java

      I am trying to query list of itemName but getting "NullPointerException" : I am missing some basics.. :(

      Please suggest on the following :

      a) How can I get list of itemName in home object (itemHome.java) ?
      Tried injecting itemList but always get "NullPointerException.."

      b) I tried creating a separate bean ItemBean and used entityManager to query list of itemNames..
      @Name("itemsBean")
      public class ItemsBean {
           @In
           EntityManager entityManager;

           Item cc1 = (Item) entityManager.createQuery("select item from Item item");
           //Gives exception as
           java.lang.NullPointerException
                at com.session.ItemsBean.<init>(ItemsBean.java:xx)
      }
        • 1. Re: Pls suggest how to inject list object in Home or any bean - NullPointerException
          Francisco Jose Peredo Noguez Master

          Val Sw wrote on Sep 25, 2009 20:46:


               java.lang.NullPointerException
                    at com.session.ItemsBean.<init>(ItemsBean.java:xx)
          





          One of the nice things about java, is the stacktrace, the stacktrace is your friend, use the stacktrace, the stacktrace tells you the exact line where you are making a mistake, for example, in this NullPointerException, you can see that it says:


          at com.session.ItemsBean.<init>(ItemsBean.java:xx)
          





          That means, that in the file ItemsBean.java, at the line xx (if you had not replaced it with xx we could know which line it is) you are using a variable that is null.

          • 2. Re: Pls suggest how to inject list object in Home or any bean - NullPointerException
            Val Sw Expert
            Thanks for replying Francisco...

            I am doing some silly mistake.....:(
            This is the line causing exception.. stack trace and code below...
            "Item cc1 = (Item) entityManager.createQuery("select item from Item item");"

            I tried accessing itemList.java object in itemHome.java as well as in separate bean (ItemBean) but getting exception...

            I just need a list of itemNames from Item entity.... please suggest the right approach...
            Similar post : http://www.seamframework.org/Community/AccessSeamComponentIntoSeperateJavaClass

            StackTrace:
            -----------
            ItemsBean entered
            Checking
            Executing entity query
            java.lang.NullPointerException
                 at com.ItemsBean.<init>(ItemsBean.java:74)
                 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
                 at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
                 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
                 at java.lang.reflect.Constructor.newInstance(Unknown Source)
                 at java.lang.Class.newInstance0(Unknown Source)
                 at java.lang.Class.newInstance(Unknown Source)
                 at org.jboss.seam.Component.instantiateJavaBean(Component.java:1315)

            @Name("itemsBean")
            public ItemsBean() {     
                 @In
                 EntityManager entityManager;
                 .....
                 System.out.println("ItemsBean entered");
                 System.out.println("Checking ");          
                 try {               
                      System.out.println("Executing entity query");
                      //Item cc1 = (Item) entityManager.createQuery("select item from Item item").getResultList();
                      Item cc1 = (Item) entityManager.createQuery("select item from Item item");
                      System.out.println("Executed entity query");               
                 } catch (Exception nre) {
                      nre.printStackTrace();
            }
            • 3. Re: Pls suggest how to inject list object in Home or any bean - NullPointerException
              B Tomic Newbie

              Try


               Item cc1 = (Item) entityManager.createQuery("select item from Item item").getSingleResult();



              or


               List<Item> cc1 = entityManager.createQuery("select item from Item item").getResultList();



              depending on what you were trying to achieve. But, if you just want names, and not entities themselves, try


               List<String> = entityManager.createQuery("select item.name from Item item").getResultList();

              or something like that.





              • 4. Re: Pls suggest how to inject list object in Home or any bean - NullPointerException
                B Tomic Newbie

                Also, I don't know how you tried to access itemList (nor how is that component declared), but I'm guessing that you simply forgot to instantiate it before injecting it.
                If I'm right, just do


                @In(create=True) ItemList itemList;

                • 5. Re: Pls suggest how to inject list object in Home or any bean - NullPointerException
                  Val Sw Expert

                  Thanks for your help.


                  Here is scenario :
                  a) I need list of only itemNames from Item entity (which I will use in suggestionBox) either in corresponding ItemHome.java or new bean ItemBean.java
                  b) I already have default generated ItemList and ItemHome.java from seam-gen.


                  I tried the following and missing something basic :
                  1) Created ItemsBean and tried to get itemNames, but getting NullPointerException in both cases List Item and List String



                  @Name("itemsBean")
                  public class ItemsBean {     
                       @In
                       EntityManager entityManager;     
                       EntityHome<Item> itemHome;     
                       @In(create=true) ItemList itemList;
                  
                       public ItemsBean() {
                       try {               
                                 System.out.println("Executing entity query");
                                 List<Item> cc1 = entityManager.createQuery("select item from Item item").getResultList();//Gives NullPointerException as below 
                                 //OR
                                 List<String> cc1 = entityManager.createQuery("select item.itemName from Item item").getResultList();// Same //Gives NullPointerException as below 
                            } catch (Exception nre) {
                                 nre.printStackTrace();
                            }
                  }
                  Output :
                  -------
                  ItemsBean entered
                  Executing entity query
                  java.lang.NullPointerException
                       at com.session.ItemsBean.<init>(ItemsBean.java:76)
                       at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
                       at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
                       at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
                       ........
                       at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
                       at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
                       at java.lang.Thread.run(Unknown Source)
                  


                  2) Tried Injecting ItemList.java in ItemHome as well as ItemBean but itemList is null


                  @Name("itemHome")
                  public class ItemHome extends EntityHome<Item> {
                       @In(create=true) ItemList itemList;
                       .....
                  
                       //CHECK ITEMLIST
                        public ItemHome() {          
                            System.out.println("ItemHome entered");          
                            System.out.println("ItemHome check itemlist for null : "+itemList);//THIS IS NULL
                        }
                  }
                  
                  Output:
                  ItemHome entered
                  ItemHome check itemlist for null : null



                  • 6. Re: Pls suggest how to inject list object in Home or any bean - NullPointerException
                    Val Sw Expert
                    Please see the above post point 2 for injecting itemList in ItemHome or ItemBean..

                    Other point what is the way to use query use in ItemHome or ItemBean defined in components.xml

                    I have defined entity query in components.xml..
                    <framework:entity-query name="itemsList" ejbql="select i.itemName from Item i" />

                    How can I use this in ItemHome or ItemBean. I am able to display in xhtml using datatable...
                    • 7. Re: Pls suggest how to inject list object in Home or any bean - NullPointerException
                      Val Sw Expert

                      Please give your inputs...


                      Is it a problem as I am executing entity query in the constructor as above ?
                      How can I load list of Items during initialization of ItemHome or ItemBean..


                      Regards

                      • 8. Re: Pls suggest how to inject list object in Home or any bean - NullPointerException
                        Francisco Jose Peredo Noguez Master

                        You can not use anything injected with @In in the constructor of your component, and AFAIK you are calling entityManager.createQuery inside the constructor of the ItemHome component, that is why entityManager is null

                        • 9. Re: Pls suggest how to inject list object in Home or any bean - NullPointerException
                          Francisco Jose Peredo Noguez Master

                          If you want to do something when a component is created, do it inside a method with the @Create annotation, if you want do do something when a page is first visited, then link the visit to that page with the method by using the action parameter in the .page.xml file for your .xhtml page.

                          • 10. Re: Pls suggest how to inject list object in Home or any bean - NullPointerException
                            Val Sw Expert
                            Thanks for correcting me, I am trying to get itemNames directly from itemList (default generated by seam).
                            I am able to check the size of itemList in getItems(..) method.

                            Just need, small help... it is more like java issue..

                            Iterator <Item> iterator = getItems().iterator() calls
                            getItems(), which I want to use to check the itemNames in while loop below.

                            What will be the best way to get item names in "public ArrayList<Item> getItems()" function below which return items.

                            `
                            @Name("itemsBean")
                            public class ItemsBean {
                                   
                                    private ArrayList<Item> items = new ArrayList<Item>();     
                                    private ArrayList<String> itemsNames = new ArrayList<String>();
                                    private List<SelectItem> itemsOptions = new ArrayList<SelectItem>();
                                private String item = "";
                                   
                                private String currentNameFilterValue;   
                               
                                public List<Item> autocomplete(Object suggest) {
                               
                                    String pref = (String)suggest;
                                    ArrayList<Item> result = new ArrayList<Item>();
                                           
                                    Iterator<Item> iterator = getItems().iterator();
                                   
                                    while (iterator.hasNext()) {
                                        Item elem = ((Item) iterator.next());
                                           
                                            if ((elem.getItemName() != null && elem.getItemName().toLowerCase().indexOf(pref.toLowerCase()) == 0) || "".equals(pref))
                                        {
                                            result.add(elem);
                                        }
                                    }
                                    return result;
                                }

                                    public ArrayList<Item> getItems() {
                                            //This returns correct size of itemNames
                                            System.out.println("ItemBean.getItems(), 1 CHECK THIS ITEMLIST result size : " +itemList.getResultList().size());
                                            //Some how use itemNames from the above list and return, NOTE: Arraylist is of type Item
                                            return items;
                                    }      
                            }
                            `