The Warwick Mathematics Society Website

User login

Upcoming events

  • No upcoming events available

There are 466 members of the Warwick Mathematics Society, of which 0 are new today!
We're 93% of the way toward our target of 500 members.
You can join up on the UWSU website.

MA117 Java problem

Post Icon 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();
--- s.printSegment();

This correctly calls my printSegment method on the LineSegment constructor.

--- LineSegment [] s = new LineSegment[1];
--- s[0].printSegment();

This causes a NullPointerException error.

Edit: changed input format to show the code properly.

Post Icon Posted: 12 March 2008 - 7:52pm

Joined: 2007-06-03
Posts: 3

Look at this code:
int i = 1;
or
int i; i = 1;

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:

int i;
i = i + 1;

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.

for(int i=0; i<4; i++) {;
arrayOfLines[i] = new LineSegment(x1, y1, x2, y2) //x1 ... y2 are numbers (doubles);
}
Now if you try to use a LineSegment method from one of the elements of the array, it will no longer throw a NullPointerException

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

Post Icon 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

Post Icon 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.

Post Icon 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.

Post Icon 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.
Then of course it is not unhelpful as I said as the assignment was given again this year and there is still time before the deadline.

Post Icon Posted: 12 March 2008 - 8:43pm

Joined: 2006-10-05
Posts: 534

Wait this has gotta be a glitch?

Post Icon 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).

Post Icon 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.

Post Icon 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

Post Icon 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...

Post Icon Posted: 12 March 2008 - 9:30pm

Joined: 2006-10-10
Posts: 391

Our forum is haunted?!

Post Icon 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.

Post Icon Posted: 12 March 2008 - 10:45pm

Joined: 2006-10-01
Posts: 370

Either way, Mr. Dreery, we salute you!

Post Icon Posted: 13 March 2008 - 12:00am

Joined: 2007-06-03
Posts: 3

haha

found it while doing a search for MA117 stuff on google.

Post Icon Posted: 13 March 2008 - 4:07am

Joined: 2006-11-02
Posts: 811

Our forum is haunted?!

Definetly. Whole threads are disappearing. :/

Post Icon Posted: 13 March 2008 - 4:13am

Joined: 2007-10-03
Posts: 245

Yeah, it's strange...

Access denied
You are not authorized to access this page.

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...

Post Icon 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.

Post Icon Posted: 16 March 2008 - 11:28pm

Joined: 2006-08-31
Posts: 676

Someone unpublished it.

Post Icon 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.