
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();
}

Siten0308 - Jun 20 2019 01:43 PM
johnnylo - Apr 23 2019 07:49 AM
PJohnson - Apr 18 2019 03:55 AM
xarzu - Apr 05 2019 09:17 AM
xarzu - Apr 04 2019 11:47 AM
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();
}
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.
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();
}
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.
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();
Edited by xXAlphaXx, 16 September 2014 - 01:50 PM.