Posted: Submitted by Sammy P 06 on 11 March 2007 - 2:25pm. |
|
![]()
Joined: 2006-11-04
Posts: 52 |
I know this is not technically mathematics but it is from an MA module! Is anyone able to tell me why both of the following pieces of code compile properly but only the first one runs (using a normal LineSegment object) whereas the second (using an array) gives a runtime NullPointerException ?: --- LineSegment s = new LineSegment(); This correctly calls my printSegment method on the LineSegment constructor. --- LineSegment [] s = new LineSegment[1]; This causes a NullPointerException error. Edit: changed input format to show the code properly. |
Posted: 12 March 2008 - 7:52pm |
|
![]()
Joined: 2007-06-03
Posts: 3 |
Look at this code: When you define an integer (eg int i), you are not creating an integer: all you are doing is creating a pointer that can point to an integer. The piece of code (i = 1) gets some memory (think its 2 bytes for an integer but could be wrong) and stores the value of one into it, If you had some code like:
there is no memory for i, it is just a pointer pointing nowhere (technically null) this code might well compile but would throw a null pointer exception, since the pointer is pointing at null. To create space for an object in the memory, you use the 'new' keyword. However, in java, when you initialise an array with, say, LineSegment [] s = new LineSegment[1];
you are only initialising an array of pointers. For example:-
LineSegment[] arrayOfLines;
this a pointer to an array of lines
arrayOfLines = new LineSegment[4]
this initialises the array, but all this does is create an array of pointers. Noew we need to initialise every element in the array.
Hope this was helpful Rich xx NOTE you do not need to initialise every element in the array, just dont reference one that you haven't |
Posted: 12 March 2008 - 8:07pm |
|
![]()
Joined: 2006-10-09
Posts: 318 |
Couldn't have said it any better myself! Ah well, back to marking... :( Was hoping for procrastination on the forums... :( Jamie |
Posted: 12 March 2008 - 8:09pm |
|
![]()
Joined: 2007-10-03
Posts: 245 |
Well the answer is a just a tiny bit late but that's helpful anyway. |
Posted: 12 March 2008 - 8:27pm |
|
![]()
Joined: 2006-10-05
Posts: 534 |
It's not late the deadline for MA117 project 2 is this Sunday. Thanks to not realising this I finished last week. |
Posted: 12 March 2008 - 8:29pm |
|
![]()
Joined: 2007-10-03
Posts: 245 |
Well the question was asked one year ago, and the explanation corresponds just to that question, so in that sense it is late. |
Posted: 12 March 2008 - 8:43pm |
|
![]()
Joined: 2006-10-05
Posts: 534 |
Wait this has gotta be a glitch? |
Posted: 12 March 2008 - 8:51pm |
|
![]()
Joined: 2007-10-03
Posts: 245 |
No ? Sammy is a second year that took this module in the first year, so I don't see why it would be a glitch. Just seems strange that someone dug up that old thread and answered (especially nearly exactly one year after :D). |
Posted: 12 March 2008 - 9:08pm |
|
![]()
Joined: 2006-10-05
Posts: 534 |
I'm a second year! I think it's just too weird too have a Java question unanswered for a year. This isn't the first time Jamie has tried to procrastinate. |
Posted: 12 March 2008 - 9:22pm |
|
![]()
Joined: 2006-10-09
Posts: 318 |
Bloody hell... Didn't notice the date, assumed it was asked yesterday rather than 1y 1d ago... Wowzers... Jamie |
Posted: 12 March 2008 - 9:23pm |
|
![]()
Joined: 2006-10-01
Posts: 370 |
That's stunning...! Hadn't spotted that. In general, we're pretty good at answering that kind of stuff quite fast. SpooOOooky... |
Posted: 12 March 2008 - 9:30pm |
|
![]()
Joined: 2006-10-10
Posts: 391 |
Our forum is haunted?! |
Posted: 12 March 2008 - 10:14pm |
|
![]()
Joined: 2006-11-02
Posts: 811 |
I don't think it's a glitch, it's genuinely a year old thread. The person who answered it must have done a search for Java stuff on the forum and found this thinking it was posted yesterday (or presumably something like this). Plus, most people taking this module are first years. |
Posted: 12 March 2008 - 10:45pm |
|
![]()
Joined: 2006-10-01
Posts: 370 |
Either way, Mr. Dreery, we salute you! |
Posted: 13 March 2008 - 12:00am |
|
![]()
Joined: 2007-06-03
Posts: 3 |
haha found it while doing a search for MA117 stuff on google. |
Posted: 13 March 2008 - 4:07am |
|
![]()
Joined: 2006-11-02
Posts: 811 |
Definetly. Whole threads are disappearing. :/ |
Posted: 13 March 2008 - 4:13am |
|
![]()
Joined: 2007-10-03
Posts: 245 |
Yeah, it's strange...
Edit : for anyone that is interested, the url was http://warwickmaths.org/forum/maths-banter/if-kronecker-was-right-i-am-d... , it gives a different error than just trying a random nonexistent thread, so I think the thread might still exist... |
Posted: 16 March 2008 - 10:57pm |
|
![]()
Joined: 2007-10-04
Posts: 186 |
Apparently that thread should manifest in an admin in-tray of some variety. I can't find one of those anywhere however. |
Posted: 16 March 2008 - 11:28pm |
|
![]()
Joined: 2006-08-31
Posts: 676 |
Someone unpublished it. |
Posted: 17 March 2008 - 12:29am |
|
![]()
Joined: 2006-10-18
Posts: 13 |
I remember reading this thread a while back, it was just the first post at the time and I was about to answer it myself - until I realised I was 6 months late. It's a pity I didn't read the forums this time last year or I'd have answered it straight away. |