Vswe's Summer Courses | You're not logged in. Sign up or log in | Server time: | Donate
One can now see other persons' badge hunts. Simply head over to their profiles to access them.

Go back to course page

Coding and a cup of Java

An introduction course to programming

Null Pointer Exception in Array

  • 2013-08-29 16:24:16
    I'm currently doing the assignment for this course and are having issue with this line of code:

    int[][] basket = new int[3][];

    This line works fine but if i try to edit any values in the array like so:

    basket[0][0] = 0;

    it throws up a Null Pointer Exception. I've looked up what this means and think that it is treating the array as a null object. Can anyone tell me what I'm doing wrong? Thanks.
  • 2013-08-29 16:50:05
    Do you really want to create a two-dimensional array?
    If you do then you need two initialize the second array, by giving it a size too. Like this for example:

    int[][] = new int[3][2];

    Otherwise you can't reach a value inside the second array,
    because it is still null.
  • 2013-08-29 16:50:30
    It's because you only set the first array with the:
    int[][] basket = new int[3][];

    This is how that looks:
    int[][] basket = {null, null, null};

    The first array in the basket have 3 spots and all of the arrays of those are null as they are not set;
    You can either set them by doing:
    int[][] basket = new int[3][anyNumber];

    you can set each one by they own like:
    basket[0] = new int[anyNumber];
  • 2013-08-29 17:01:26
    Ok Thanks, I can see why that didn't work now.
    The reason I wanted to leave the second [] blank was because I wanted the array to be able to expand as the user added more stuff to it and not be limited at a fixed size. Would it just be better if I set it to a large number like 100 and hope that the user didn't add too much stuff or is there a workaround?
  • 2013-08-29 17:08:20
    Scratch that I think I've worked it out.
  • 2013-08-29 18:06:26
    If you don't know how much you want to store, it is possible to set the array size to something high so that it is unlikely, that it fills up, but that is wasteful, it would be better to use a type like ArrayList, which expands itself when it runs out of space.
  • 2013-08-29 19:51:36
    The first assignment does not require you to expand the array. Also, the ArrayLists are part of the second course. I would recommend to leave them for now.
  • 2013-08-30 11:49:46
    Thanks everyone for the help, I've managed to work out a way that just uses a one dimensional array of fixed size that works better than the method I was using before.

    Thanks for running these courses Vswe! I don't have much free time so often skip parts of the videos but the overview pictures and examples are enough for me by themselves so thanks for them!
  • 2013-10-31 06:13:04
    Here's a trick that's used by some of the collection classes: Create your array and when it is getting full copy it to a new array with greater capacity. The only problem with that is if the arrays are massive, otherwise it's a Good Idea.
  • Log in or sign up to reply to this thread