why null pointer exception?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2008
Posts: 33
Reputation: gotm is an unknown quantity at this point 
Solved Threads: 0
gotm gotm is offline Offline
Light Poster

why null pointer exception?

 
0
  #1
May 1st, 2009
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 34
Reputation: Zibo is an unknown quantity at this point 
Solved Threads: 3
Zibo's Avatar
Zibo Zibo is offline Offline
Light Poster

Re: why null pointer exception?

 
0
  #2
May 1st, 2009
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.):

  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:

  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC