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.

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

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:

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

This article has been dead for over six months. Start a new discussion instead.