Hi, its the first time I have posted here. I am just starting with JAVA and really need some help with a small prgram I have writtem I was messing about so the progrm will probably look a bit dtrange and pointless but the whole point was really just to create an Array, populate it with data and search succesfully on one of the variables. The problem is at the very bottom, I just cant figure out where i am going wrong. Here is the code
The text file 32 Glasgow 12 George Stephenson 44 Edinburgh 15 Robert McMeachan 21 London 20 John Smith 22 Rochdale 21 Richard Evans 23 Lisbon 23 Frank White 24 Lisboa 250 Gerry McNee 25 Azoia 33 Randall Hopkins 28 Cascais 45 Telly Savallas 56 Estoril 50 Randy Hopkins ============================================================================ And finally the error message! java.lang.NullPointerException at Person2.main(Person2.java:59) Exception in thread "main" ============================================================================ I know I am doing something wrong but cant figure exactly what, any help would be greatly appreciated! Thanks in advance.
[ edited to format code -ds ] [ October 31, 2003: Message edited by: Dirk Schreckmann ]
Please use *code* tags. Place a comment on the line which is breaking. Don't include lines of code which are commented out, and ... I think someone else is posting questions about this exact same assignment. Check the other posts.
Please ignore post, I have no idea what I am talking about.
William, believe it or not, it's not the same thing that I have been pestering everyone with Funny thing that we got assignments looking so alike
Joined: Oct 28, 2003
Hi, thanks for the reply, can I just say to begin with, if this was a homewrok assignment I would be honest enough to say so, beleive it or not I actually want to learn how to do this. Sorry about the code tags didnt realise about that, so heres the code
Here is the error exactly as it appears, java.lang.ArrayIndexOutOfBoundsException at personList.getPersonAt(personList.java:77) at Person2.main(Person2.java:54) Exception in thread "main" If i can get a handle on how to this I should be able to work with Arrays a bit better than just now. And apologies if I have messed up again, I appreciate the patience of your good selves with programming novice! Cheers. [ edited to fix formatting -ds ] [ October 31, 2003: Message edited by: Dirk Schreckmann ]
Couple of things. You can get around this error by checking for MAX in that method, and or catching that exception. But I would first recommand making a method to dump all of your data. Make it a private method and only use it for debugging purposes. And just add a bunch of print statements everywhere - the poor mans debugger. Printing out the number of the index which is killing that method would be a good one. I am assuming you know why the programming is dieing. Ok, in case you don't. You are asking for an element in the array which doesn't exist. You would get this error if for example you have an array with 10 elements and you want to do something with element number 11.
Joined: Oct 28, 2003
damn messed up the last bit of code here its again
Not sure why you are re-posting you code. My comments are just general comments which will help you find the problem. So look at my last post. What is the difference between getPosition() and getPersonAt(). It can be rather confusing to have two different methods doing the same thing.