dereferencing null pointer exception

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    dereferencing null pointer exception

    My variable cp is = 2 when I pass 1 and 2 values but I get a null pointer exception.

    Could you please explain?

    Code:
     
    for(int i=0; i<=resLeftL.size(); i++){
      //System.out.println(resLeftL.size());      
      for(int j=0; j<= resRightL.size(); j++)
      {  
        cP=resLeftL.get(i)*resRightL.get(j);
        System.out.println("cp="+cP); // prints 2
        elements.addElements(cP); 
        //dereferecing null
       }   
     }
     return elements;
     }
    Last edited by Frinavale; Oct 30 '13, 06:33 PM. Reason: moved the question above the code so that the reader has an understanding of the problem before looking at the code.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Did you check if elements is null at that point?

    Comment

    • oll3i
      Contributor
      • Mar 2007
      • 679

      #3
      Code:
      elements.addElement(resLeftL.get(i)*resRightL.get(j));
      <--is null at that point

      Comment

      • oll3i
        Contributor
        • Mar 2007
        • 679

        #4
        the error can be here
        Code:
         IBagResult  elements = null;
        and the BagResult is
        Code:
        public class BagResult implements IBagResult{
         Collection collection = new HashSet();
         @Override
         public Collection getElements()
         {return collection;} 
         @Override
         public void addElement(Integer i){
         collection.add(i);
         }
        }

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          Do you have something like
          Code:
          elements = new BagResult();
          anywhere? If not, add that before you try to access the elements object.

          Comment

          • oll3i
            Contributor
            • Mar 2007
            • 679

            #6
            Code:
            elements.addElement(resLeftL.get(i)*resRightL.get(j))
            returns

            thread "main" java.lang.Index OutOfBoundsExce ption: Index: 1, Size: 1
            at java.util.Array List.rangeCheck (ArrayList.java :635)
            at java.util.Array List.get(ArrayL ist.java:411)
            at s4409_s4409_pr2 .QExecUtils.car tesianProduct(Q ExecUtils.java: 60)
            at s4409_s4409_pr2 .S4409_s4409_pr 2.main(S4409_s4 409_pr2.java:38 )

            Comment

            • oll3i
              Contributor
              • Mar 2007
              • 679

              #7
              and in main that is the line
              Code:
              IBagResult commaRes = QExecUtils.cartesianProduct(resLeft,resRight

              Comment

              • Nepomuk
                Recognized Expert Specialist
                • Aug 2007
                • 3111

                #8
                As the loop is dependant on the size of resLeftL, I'd guess that resRightL is smaller than the left list. To be more precise, according to this exception it has one element and you're trying to access the second element.

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  The index problem is because of the loop conditions you have:
                  Code:
                  for(int i=0; i<=resLeftL.size(); i++)
                  {
                    for(int j=0; j<= resRightL.size(); j++)
                    {
                    }
                  }
                  Arrays etc. in Java start at index 0. This means that if you have something that is 10 units long the valid indices fall between 0 and 9 but in your loop you are going to 10 because of the <= check. It should just be <.



                  -Frinny

                  Comment

                  • Nepomuk
                    Recognized Expert Specialist
                    • Aug 2007
                    • 3111

                    #10
                    Good catch Frinny, I missed that one!

                    Comment

                    Working...