| | |
why null pointer exception?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2008
Posts: 33
Reputation:
Solved Threads: 0
It keeps giving me this exception when it seems like I am adding a choice to my choices vector, and I don't know why.
Here is that snippet of code. If you need more I can provide it.
Thanks ahead of time.
Here is that snippet of code. If you need more I can provide it.
Thanks ahead of time.
java Syntax (Toggle Plain Text)
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Vector; public class multipleChoice extends question { public Vector<String> choices; public multipleChoice() { System.out.println("Please enter the text of your question: "); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { String textIn = br.readLine(); text = textIn; System.out.println("Please enter the answer of your question: "); BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in)); try { String AnsIn = br.readLine(); answer = AnsIn; BufferedReader inStream = new BufferedReader ( new InputStreamReader(System.in) ); System.out.println("Enter the number of choices: "); String inLine = inStream.readLine(); int numChoices = Integer.parseInt(inLine); for (int j = 0; j < numChoices - 1; j++) { System.out.println("Please enter choice number " + (j + 1) + ": "); BufferedReader br3 = new BufferedReader(new InputStreamReader(System.in)); try { String choice= br.readLine(); choices.add(choice); } catch (IOException e) { e.printStackTrace(); } } } catch (IOException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } }
It's because You didn't create an instance of Your 'choices' variable. You need to allocate a memory by adding a line (in constructor i.e.):
Oh, and I think You don't have to catch 3 times still one type of exception one after another. Remove 2 of them leaving 1 big try, so it would be like:
Too many brackets too find out, when one starts and where another ends, so I commented only two of them.
BTW: My first post on this forum. Hello! :7
Java Syntax (Toggle Plain Text)
choices = new Vector<String>();
Oh, and I think You don't have to catch 3 times still one type of exception one after another. Remove 2 of them leaving 1 big try, so it would be like:
Java Syntax (Toggle Plain Text)
try { // <- here starts try String textIn = br.readLine(); text = textIn; System.out.println("Please enter the answer of your question: "); BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in)); String AnsIn = br.readLine(); answer = AnsIn; BufferedReader inStream = new BufferedReader ( new InputStreamReader(System.in) ); System.out.println("Enter the number of choices: "); String inLine = inStream.readLine(); int numChoices = Integer.parseInt(inLine); for (int j = 0; j < numChoices - 1; j++) { System.out.println("Please enter choice number " + (j + 1) + ": "); BufferedReader br3 = new BufferedReader(new InputStreamReader(System.in)); String choice= br.readLine(); choices.add(choice); } } } } // <- here ends try catch (IOException e) { e.printStackTrace(); }
Too many brackets too find out, when one starts and where another ends, so I commented only two of them.
BTW: My first post on this forum. Hello! :7
Last edited by Zibo; May 1st, 2009 at 8:33 pm.
![]() |
Similar Threads
- Getting a null pointer exception when saving image to file. (Java)
- Null Pointer Exception problem (Java)
- Null Pointer Exception....... (Java)
- Null pointer exception while getting other applet in the same page (Java)
- null pointer exception --- urgent (Java)
- Java Null Pointer Exception (Java)
- help with sort using Calendar class getting null pointer exception (Java)
Other Threads in the Java Forum
| Thread Tools | Search this Thread |
Tag cloud for Java
addball android api apple applet application apps arguments array arrays automation binary bluetooth businessintelligence card chat class classes client code collision component crashcourse database draw eclipse ee error event exception file fractal free game gis givemetehcodez graphics gui helpwithhomework html ide image input integer integration j2me java javadoc javafx javaprojects jmf jni jpanel julia jvm linux list loop machine map method methods migrate mobile netbeans newbie nls number object oracle physics print problem program programming project radio recursion scanner screen security server service set size sms socket software sort sql string swing test textfield threads time tree trolltech utility windows





