This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NullPointerException" Watch "NullPointerException" New topic
Author

NullPointerException

Emile Ghazzawi
Greenhorn

Joined: Dec 30, 2002
Posts: 28
Hi,
I am creating a project as part of a University Assignment and all was well until i encountered a dreaded NullPointerException! I have tried all i can to work a way around it but just can't seem to suss it out. It has been causing me endless hours of frustration! If anyone could help me out then it would be HUGELY appreciated! I have included my code below and the error seems to be occuring on line 210 which reads:
if (stockCode[itemNum].equals(searchItem))
My Code
-------

[JAM -->Edited to use [CODE] and [/CODE] tags]
[ April 19, 2004: Message edited by: Joel McNary ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
It looks like your problem is in readData()
int count=0;
...
...
do
{
count++;//<---------------count is now 1, meaning stockCode[0] is null
stockCode[count] = input.readLine();
move the count++ to the end of the do/while block
Emile Ghazzawi
Greenhorn

Joined: Dec 30, 2002
Posts: 28
Hi again,
Solved it by changing the problamatic line 210 to
CODE] if (stockCode[itemNum]!=null && stockCode[itemNum].equalsIgnoreCase(searchItem))[ [/CODE]
Also implemented your idea, much more sensible that putting it before !
Thanks for your help, i sure i'll be back asking for more very shortly!
Thanks again,
Emile
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I can't speak for your teacher, but where I went to school we'd lose points for not indenting. It's hard to follow your logic otherwise.


Mike Gershman
SCJP 1.4, SCWCD in process
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

It's not that the code wasn't indented; it's that the [CODE] and [/CODE] tags were not used; thus, the indenting was not preserved. I edited the post to include them this time....


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
 
Don't get me started about those stupid light bulbs.
 
subject: NullPointerException
 
Similar Threads
help me
Exception in Thread-Inventory Program
Car Inventory Problems
Help with code
Currency Problem + General Advice