943,772 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1661
  • Java RSS
May 1st, 2009
0

why null pointer exception?

Expand Post »
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.

java Syntax (Toggle Plain Text)
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Vector;
  5.  
  6. public class multipleChoice extends question
  7. {
  8. public Vector<String> choices;
  9.  
  10. public multipleChoice()
  11. {
  12. System.out.println("Please enter the text of your question: ");
  13. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  14. try {
  15. String textIn = br.readLine();
  16. text = textIn;
  17. System.out.println("Please enter the answer of your question: ");
  18. BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
  19. try {
  20. String AnsIn = br.readLine();
  21. answer = AnsIn;
  22. BufferedReader inStream = new BufferedReader (
  23. new InputStreamReader(System.in)
  24. );
  25. System.out.println("Enter the number of choices: ");
  26. String inLine = inStream.readLine();
  27. int numChoices = Integer.parseInt(inLine);
  28. for (int j = 0; j < numChoices - 1; j++) {
  29. System.out.println("Please enter choice number " + (j + 1) + ": ");
  30. BufferedReader br3 = new BufferedReader(new InputStreamReader(System.in));
  31. try {
  32. String choice= br.readLine();
  33. choices.add(choice);
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. } catch (IOException e) {
  39. e.printStackTrace();
  40. }
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
gotm is offline Offline
33 posts
since May 2008
May 1st, 2009
0

Re: why null pointer exception?

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.):

Java Syntax (Toggle Plain Text)
  1. 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)
  1. try
  2. { // <- here starts try
  3. String textIn = br.readLine();
  4. text = textIn;
  5. System.out.println("Please enter the answer of your question: ");
  6. BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
  7.  
  8. String AnsIn = br.readLine();
  9. answer = AnsIn;
  10. BufferedReader inStream = new BufferedReader (
  11. new InputStreamReader(System.in)
  12. );
  13. System.out.println("Enter the number of choices: ");
  14. String inLine = inStream.readLine();
  15. int numChoices = Integer.parseInt(inLine);
  16. for (int j = 0; j < numChoices - 1; j++) {
  17. System.out.println("Please enter choice number " + (j + 1) + ": ");
  18. BufferedReader br3 = new BufferedReader(new InputStreamReader(System.in));
  19.  
  20. String choice= br.readLine();
  21. choices.add(choice);
  22. }
  23. }
  24. }
  25.  
  26. } // <- here ends try
  27. catch (IOException e) {
  28. e.printStackTrace();
  29. }

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.
Reputation Points: 12
Solved Threads: 3
Light Poster
Zibo is offline Offline
41 posts
since May 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC