Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Array of object always null?

java class array null

Best Answer gonerogue, 16 September 2014 - 09:43 AM

for (int i = 0; i < m; ++i)

{

            scan.nextLine();

            walls[i] = new doorWindow();
            walls[i].width = scan.nextInt();
            walls[i].length = scan.nextInt();
}

Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1 xXAlphaXx

xXAlphaXx

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 109 posts

Posted 16 September 2014 - 09:28 AM

It has been a while since I have written anything in Java, and I am struggling to figure out why my array of objects are all instanciating to null. From what I have found, that is the default value, however when ever I try to assign a value to a specific element in the array, I get a null pointer exception, like the element isn't even there. I created a single instance of this object and I could access it fine, but I couldn't access anything inside an array it seems.

 

Here is the relevant code:

doorWindow a = new doorWindow();
doorWindow[] walls = new doorWindow[m];

        for (int i = 0; i < m; ++i)
        {
            scan.nextLine();
            walls[i].width = scan.nextInt();
            walls[i].length = scan.nextInt();
        }
public class doorWindow
{
    public int length = 0;
    public int width = 0;

    public int calculateArea()
    {
        return length * width;
    }
}

Edited by xXAlphaXx, 16 September 2014 - 09:29 AM.


#2 gonerogue

gonerogue

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 197 posts

Posted 16 September 2014 - 09:43 AM   Best Answer

for (int i = 0; i < m; ++i)

{

            scan.nextLine();

            walls[i] = new doorWindow();
            walls[i].width = scan.nextInt();
            walls[i].length = scan.nextInt();
}



#3 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts

Posted 16 September 2014 - 01:36 PM

doorWindow a = new doorWindow();
doorWindow[] walls = new doorWindow[m];

What is 'm'?  I believe that variable would need to be initialized, if you are trying to set the size or your array, otherwise it's going to end up being null.

 

And as GoneRogue said, you will also need:

 walls[i] = new doorWindow();

Edited by CommittedC0der, 16 September 2014 - 01:37 PM.

There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#4 xXAlphaXx

xXAlphaXx

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 109 posts

Posted 16 September 2014 - 01:43 PM

doorWindow a = new doorWindow();
doorWindow[] walls = new doorWindow[m];
What is 'm'? I believe that variable would need to be initialized, if you are trying to set the size or your array, otherwise it's going to end up being null.

And as GoneRogue said, you will also need:
 walls[i] = new doorWindow();

Yep, m is indeed initialized. GoneRouge nailed it, I needed to redeclare each element in the array.

Thank you both!

Edited by xXAlphaXx, 16 September 2014 - 01:50 PM.





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download