Array probs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • reon
    New Member
    • Feb 2007
    • 80

    Array probs

    Did anybody please explain me wat is the problem here...
    it is showing a null pointer exception...
    Code:
    class array
    {
    String var;
    public static void main(String args[])
    	{
    	array obj[];
    	obj = new array[1];
    	obj[0].var="1";
    	System.out.println(obj[0].var);
    	}
    }
  • shana07
    Contributor
    • Jan 2007
    • 280

    #2
    Originally posted by reon
    Did anybody please explain me wat is the problem here...
    it is showing a null pointer exception...
    Code:
    class array
    {
    String var;
    public static void main(String args[])
    	{
    	array obj[];
    	obj = new array[1];
    	obj[0].var="1";
    	System.out.println(obj[0].var);
    	}
    }
    friend, why don't you try such this:
    Code:
    class Array
    {
    	public static void main(String args[])
    	{
    		String[] obj;
    		
    		obj = new String[1];
    		
    	    	obj[0] = "Hello;
    	               System.out.println(obj[0]);	
    		
    	}
    }

    Comment

    • reon
      New Member
      • Feb 2007
      • 80

      #3
      My dear friend
      Try to understand wat i mean , i am calling that variable named ' var ' from the array class...
      Think before you post....

      Originally posted by shana07
      friend, why don't you try such this:
      Code:
      class Array
      {
      	public static void main(String args[])
      	{
      		String[] obj;
      		
      		obj = new String[1];
      		
      	    	obj[0] = "Hello;
      	               System.out.println(obj[0]);	
      		
      	}
      }

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by reon
        Did anybody please explain me wat is the problem here...
        it is showing a null pointer exception...
        Code:
        class array
        {
        String var;
        public static void main(String args[])
        	{
        	array obj[];
        	obj = new array[1];
        	obj[0].var="1";
        	System.out.println(obj[0].var);
        	}
        }
        When you instantiate an array of type T, as in:
        Code:
        T[] array= new T[42];
        ... you have created an array of (in this example) 42 references to objects of
        type T. They (the references) don't point to anything yet; they are all null.
        If you want your references to point to objects of type T you have to make them
        do that explicitly as in:
        Code:
        for (int i= 0; i < array.length; i++)
           array[i]= new T(); // point to a new T object
        kind regards,

        Jos
        Last edited by JosAH; Apr 6 '07, 07:41 AM. Reason: corrected a typo

        Comment

        • reon
          New Member
          • Feb 2007
          • 80

          #5
          i didnt get you
          would you please make it clear,,,

          Originally posted by JosAH
          When you instantiate an array of type T, as in:
          Code:
          T[] array= new T[42];
          ... you have created an array of (in this example) 42 references to objects of
          type T. They (the references) don't point to anything yet; they are all null.
          If you want your references to point to objects of type T you have to make them
          do that explicitly as in:
          Code:
          for (int i= 0; i < array.length; i++)
             array[i]= new T(); // point to a new T object
          kind regards,

          Jos

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by reon
            i didnt get you
            would you please make it clear,,,
            What particular part of my reply didn't you understand?

            kind regards,

            Jos

            Comment

            • reon
              New Member
              • Feb 2007
              • 80

              #7
              first of all "When you instantiate an array of type T, as in:"
              i didnt understand that .. and how can i slove my prob....

              Code:
              class array
              {
              String var;
              public static void main(String args[])
              	{
              	array obj[];
              	obj = new array[3];
              	obj[0].var="A";
              	System.out.println(obj[0].var);
              	}
              }
              Originally posted by JosAH
              What particular part of my reply didn't you understand?

              kind regards,

              Jos

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by reon
                first of all "When you instantiate an array of type T, as in:"
                i didnt understand that .. and how can i slove my prob....
                T is a type and I want an array of that type, so I create (instantiate) one like this:
                Code:
                T[] array= new T[42];
                Now I have an array with 42 references to a type T. These references don't point
                to anything yet; you have to do that yourself. In your code you just created the
                references (you 'new'd the array) but you didn't make any reference in that
                array point to something. You should create an object to point to, as in:
                Code:
                array[0]= new T(); // element 0 of the array now points to a T object
                kind regards,

                Jos

                Comment

                • reon
                  New Member
                  • Feb 2007
                  • 80

                  #9
                  Thanx to all of them who helped me..
                  I got solution...

                  But would anybody please explain me that line i commented...


                  Code:
                  class array
                  {
                  String var;
                  public static void main(String args[])
                  	{
                  	array obj[];
                  	obj = new array[1];
                  	obj[0]=new array(); // i didnt understand this line...please tell me in details...
                  	obj[0].var="A";
                  	System.out.println(obj[0].var);
                  	}
                  }

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by reon
                    Thanx to all of them who helped me..
                    I got solution...

                    But would anybody please explain me that line i commented...


                    Code:
                    class array
                    {
                    String var;
                    public static void main(String args[])
                    	{
                    	array obj[];
                    	obj = new array[1];
                    	obj[0]=new array(); // i didnt understand this line...please tell me in details...
                    	obj[0].var="A";
                    	System.out.println(obj[0].var);
                    	}
                    }
                    That's the line where an object actually is created (by using the 'new' operator).
                    The reference in the first array slot points to this object now. btw, naming a
                    user defined type 'array' is very confusing, that's the reason I used the more
                    abstract type 'T' in my previous replies.

                    kind regards,

                    Jos

                    Comment

                    • reon
                      New Member
                      • Feb 2007
                      • 80

                      #11
                      Thanx ..
                      Is that T abstract type is more easy to understand would you please complete the code for me i didnt understand that(T abstract type) ....
                      if you demonstrate it with that particular code it might be able to quickly understandable. .


                      Originally posted by JosAH
                      That's the line where an object actually is created (by using the 'new' operator).
                      The reference in the first array slot points to this object now. btw, naming a
                      user defined type 'array' is very confusing, that's the reason I used the more
                      abstract type 'T' in my previous replies.

                      kind regards,

                      Jos

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by reon
                        Thanx ..
                        Is that T abstract type is more easy to understand would you please complete the code for me i didnt understand that(T abstract type) ....
                        if you demonstrate it with that particular code it might be able to quickly understandable. .
                        Ok, a small code snippet can't do much harm:
                        Code:
                        T[] a; // an array; each element has type T
                        a= new T[42]; // make an array with 42 T references
                        a[0]= new T(); // the first element points to a type T object now
                        kind regards,

                        Jos

                        Comment

                        Working...