| | |
nullpointerexception error
Thread Solved |
•
•
Join Date: Nov 2007
Posts: 227
Reputation:
Solved Threads: 0
Member is class consisted of member informaiton such as firstname, lastname, phone etc.
would anyone please point out what this function gave me NullPointerException runtime error? I think I initialized everything.
thanks
would anyone please point out what this function gave me NullPointerException runtime error? I think I initialized everything.
thanks
Java Syntax (Toggle Plain Text)
public static void read(Member box[], int count) { Scanner reader = null; try { reader = new Scanner(new FileReader("database.txt")); int id; String type, fn, ln, ph; Double fee; while(reader.hasNextLine()) { id= reader.nextInt(); type= reader.next(); fn= reader.next(); ln= reader.next(); ph= reader.next(); fee= reader.nextDouble(); System.out.println(count); box[count]= new Member(id, type, fn, ln, ph, fee); box[count].output(); //count++; } } catch(FileNotFoundException e) { System.out.println("File not Found!"); } catch(IOException e) { System.out.println("Error reading"); } catch(NoSuchElementException e) { System.out.println("done reading"); } }
I expect that argument count is related to Member array box and is holding the actual size of the array ( correct me if I'm wrong)
Then you try to read file and store retrieved data in the box array.
You try to insert first set of data into array position of count.
However as count hold the array size the variable already exceeds array boundaries as Array.size() returns number of elements (lets say 20), however last element position is actually equal to count-1 ( that will be 19 as arrays starts from zero unlike vector for example). This kick off your NullPointerException.
If your method is supposed to read a file and return retrieved data you would be better of with method that will return the array then try to locally fill in and then re-use it.
Unfortunately you may not always know amount of data to be read so you should use something more "dynamic" like ArrayList or Vector. I re-do you read method little so now instead of void you get some return therefore to call it do as follows
and here is update read method
PS: Don't forget to close your Scanner
Then you try to read file and store retrieved data in the box array.
You try to insert first set of data into array position of count.
However as count hold the array size the variable already exceeds array boundaries as Array.size() returns number of elements (lets say 20), however last element position is actually equal to count-1 ( that will be 19 as arrays starts from zero unlike vector for example). This kick off your NullPointerException.
If your method is supposed to read a file and return retrieved data you would be better of with method that will return the array then try to locally fill in and then re-use it.
Unfortunately you may not always know amount of data to be read so you should use something more "dynamic" like ArrayList or Vector. I re-do you read method little so now instead of void you get some return therefore to call it do as follows
Java Syntax (Toggle Plain Text)
ArrayList<Member> boxReturn = read();
Java Syntax (Toggle Plain Text)
public static ArrayList<Memeber> read() { ArrayList<Member> box = new ArrayList<Member>(); Scanner reader = null; try { reader = new Scanner(new FileReader("database.txt")); int id; String type, fn, ln, ph; Double fee; while(reader.hasNextLine()) { id= reader.nextInt(); type= reader.next(); fn= reader.next(); ln= reader.next(); ph= reader.next(); fee= reader.nextDouble(); System.out.println(count); box.add( new Member(id, type, fn, ln, ph, fee)); //count++; } } catch(FileNotFoundException e) { System.out.println("File not Found!"); } catch(IOException e) { System.out.println("Error reading"); } catch(NoSuchElementException e) { System.out.println("done reading"); } finally { reader.close(); } return box; }
PS: Don't forget to close your Scanner
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Nov 2007
Posts: 227
Reputation:
Solved Threads: 0
peter_budo,
eventhou i never learned array list, it make sense to me using it. However, if i do know how many member are there? or if i want to assume there are 300 in the array and let it fill up (300 is a number that is way more than the total members) then could you suggest a way fixing my original code? i am just trying to see what went wrong and the solution of the nullpointerexception error. thanks
eventhou i never learned array list, it make sense to me using it. However, if i do know how many member are there? or if i want to assume there are 300 in the array and let it fill up (300 is a number that is way more than the total members) then could you suggest a way fixing my original code? i am just trying to see what went wrong and the solution of the nullpointerexception error. thanks
Any exception in Java comes with a stack trace. Try to read the entire exception trace and see on which line things are going wrong. If you are using an IDE like Eclipse, use the debugging feature to inspect the values at run time. If you are using a normal text editor, look into the command line debugger JDB. BTW, have you allocated memory to the Member array box?
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- Exception in thread "main" java.lang.NoClassDefFoundError: Invaders Error (Java)
- Multi class multi form j2me app getting NullPointerException (Java)
- Error: 500 on Apache SOAP Admin page (RSS, Web Services and SOAP)
- Summing Issue from Array (Java)
- Problem with database connection (JSP)
- error messages (Java)
- J2ME error - Unable to create MIDlet null (Java)
- NullPointerException: (Java)
- GUI Window - Exit Button? (Java)
- Need some help with a Java lab... (Java)
Other Threads in the Java Forum
- Previous Thread: Java random Unique numbers HELP
- Next Thread: solve.java
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle physics plazmic print problem program project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree trolltech unlimited utility webservices windows






