| | |
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
android api apple applet application arc arguments array arrays automation binary bluetooth c++ chat class classes client code codesnippet component csv database doctype draw ebook eclipse error event exception fractal freeze game givemetehcodez graphics gui html ide image input integer intellij iphone j2me java java.xls javaprojects jmf jni jpanel julia linux list loop loops mac map method methods mobile netbeans newbie number online oracle page parameter plazmic print problem program programming project recursion reporting rotatetext scanner screen sell server set size sms socket sort sourcelabs sql string superclass swing system template test testautomation threads time title tree tutorial-sample windows working





