Results 1 to 7 of 7
  1. #1
    mobosecomin is offline Member
    Join Date
    Oct 2010
    Posts
    23
    Rep Power
    0

    Default nullPointerExceptioin

    I was just wondering about this what it means and how to avoid it.

    Java Code:
    public class Gallery {
    
        String galleryName;
        int gallerySize;
        int i = 0;
        GalleryArt galleryContents [] ;
       
        
        Gallery(){
        }
    
        
        public void insertGalleryContents(GalleryArt x ) {
            
            galleryContents [i] = x;
            i++;
      }
    this might be an obvious question but do i have to declare the size of the array before i use it ?
    i have the size i want to use declared when my gallery object is created but i cant just pop that variable in?
    would using an array list sort this ?

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The problem is you only declared a reference to the array, you forgot to create the object that the array reference is pointing to. Therefor, your reference is pointing to a null object, nullpointer exception is thrown.

  3. #3
    mobosecomin is offline Member
    Join Date
    Oct 2010
    Posts
    23
    Rep Power
    0

    Default

    ok thanks sunde. i actually didn't forget i just thought java could do some magic for me :) also i was wondering if you were making a class with arrays and you didn't want to define a size for the array an array list would be the way to go.?

    i couldn't use a class variable set up int the constructor e.g array ra [] = new array[class variable]
    don't think this make much sense haha but il ask now because it seems like something i might come across fairly often

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    It is impossible to create an array of undetermined size. If you want to do that then yes you need to use a List or some other Collection.

    Alternatively you can ask the user the array size and pass that value to the constructor of your class.
    Java Code:
    class Foo {
        int[] array;
    
        Foo(int size) {
            array = new array[size];
        }
    }

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The real nice part of using an arraylist is that it can easily change sizes. The array can't easily be increased in size, it's possible but not as simple as just using a list.

  6. #6
    mobosecomin is offline Member
    Join Date
    Oct 2010
    Posts
    23
    Rep Power
    0

    Default

    cheers guys exactly what i wanted to know appreciate it :)

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Your welcome, please mark your thread solved with the thread tools.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •