Can someone tell why my program is showing null pointer exception at line 33

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jinnejeevansai
    New Member
    • Nov 2014
    • 48

    Can someone tell why my program is showing null pointer exception at line 33

    Hii,I was trying to solve a problem but i don't get why my program is showing null pointer exception on the commented line.Can someone explain the reason.
    Code:
    import java.util.*;
    class cod1{
            public static void main(String args[]) throws java.lang.Exception
            {
                    int t,i,n,j,k,p,q,r;
                    int[] a =new int[100000];
                    Scanner s = new Scanner(System.in);
                    t=s.nextInt();
                    for(i=0;i<t;i++)
                    {
                            p=0;
                            n= s.nextInt();
                            ArrayList<Object>[] al = (ArrayList<Object>[])new ArrayList[n+1];
                            for(j=0;j<n;j++)
                            {
                                    a[j]=s.nextInt();
                            }
                            int c;
                            for(j=0;j<n;j++)
                            {
                                    c=0;
                                    for(k=0;k<p;k++)
                                    {
                                             if(al[k].size()-1>a[j])
                                            {
                                                    al[k].add(a[j]);
                                                    c++;
                                                    break;
                                            }
                                    }
                                    if(c==0)
                                    {
                                            al[p++].add(a[j]);//null pointer exception 
                                    }
                            }
                            for(j=0;j<p;j++)
                            {
                                    System.out.println(al[j].size()-1);
                            }
                    }
            }
    }
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    Because a[j] is null,
    and al[k].add(null) then throws this exception:
    it is not allowed to add a null to this ArrayList<Objec t>(). You specified in the generics that it accepts only Object.
    Always check for null before trying to add.
    Or use ArrayList() without generics, then you can store a null there.

    Comment

    • jinnejeevansai
      New Member
      • Nov 2014
      • 48

      #3
      but i gave the input to a[j]

      Comment

      • chaarmann
        Recognized Expert Contributor
        • Nov 2007
        • 785

        #4
        First of all, you should always first check with s.hasNextInt() before calling s.nextInt().

        So you are right, the value of a[j] cannot be null because it's a primitive type, an "int" (sorry, I overlooked that).
        Then the reason might be that your array list "al", your "ArrayList<Obje ct>()" can only store Objects and no primitive types.

        You can wrap a primitive type "int i" into an object "Integer" before storing it into "al" via "Object object = new Integer(i)". Maybe that helps.

        Or much better: declare your "al" as ArrayList<Integ er>(), then autoboxing will take over this job.

        Comment

        • jinnejeevansai
          New Member
          • Nov 2014
          • 48

          #5
          Same error occurs after replacing object with integer

          Comment

          • chaarmann
            Recognized Expert Contributor
            • Nov 2007
            • 785

            #6
            And what happens if you also wrap it as proposed? That means, do both?
            That means replace the line with the nullPointerExce ption through:
            Code:
            al[p++].add(new Integer(a[j]));
            Also change line 13 from ArrayList<Objec t>() to ArrayList<Integ er>(), that means:
            Code:
            ArrayList<Integer>[] al = (ArrayList<Integer>[])new ArrayList[n+1];

            By the way, shouldn't the arrayList size be "n" instead of "n+1"? I mean, if you are entering "1", then you create an array of size 2, with a[0] and a[1] as its elements ???

            Comment

            • jinnejeevansai
              New Member
              • Nov 2014
              • 48

              #7
              I got a doubt after you say,can you tell why the same error appears in the below code.

              Code:
              import java.util.*;
              class w1{
                      public static void main(String args[]) throws java.lang.Exception
                      {
                              int n=0;
                              ArrayList<Integer>[] al = (ArrayList<Integer>[])new ArrayList[n+1];
                              al[0].add(5);
                      //      al[1].add(6);
                              System.out.println(al[0].get(1));
                      //      System.out.println(al[1].get(0));
                      }
              }

              Comment

              • chaarmann
                Recognized Expert Contributor
                • Nov 2007
                • 785

                #8
                Sorry for the misinformation. I really should have run your code to see where the nullPointerExce ption occurs.
                The nullPointerExce ption occurs, because al[p] is null (and not the value given to the add-method what I assumed wrongly)
                You can see that if you add following line right before the error at line 33:
                Code:
                System.out.println("al length=" + al.length + " p=" + p + ((p < al.length) ? " al[p]=" + al[p] : " invalid index"));
                The output is:
                Code:
                al length=3 p=0 al[p]=null
                That means, you have defined an array of arrayLists.
                But you have not initialized its content, that means you have not put anything inside your array of arrayLists. There is everywhere "null" inside. You should put empty arrayLists inside to initialize its content (after line 13):
                Code:
                        for(int index = 0;index < al.length;index++)
                        {
                                al[index]=new ArrayList<Object>();
                        }
                Try it and see that your null pointer exception is away. (but it can have IndexOutOfBound s-Exception if "p" is equal or bigger than the given array size n+1)


                In my understanding that's a design error. You only wanted a single arrayList.
                if that assumption is correct, then you should use in line 13:
                Code:
                ArrayList<Object> al = new ArrayList<Object>();
                And then in line 33:
                Code:
                al.add(new Integer(a[j]));

                Comment

                Working...