0

Can somebody enlighten me why do i get an exception ?

code is over here :

import java.io.InputStreamReader;
import java.util.*;

public class ObtainKey 
{

	public static void KeyAnalyzer(String input)
	{
		int idx = 0;
		int tokenCount;
		String inputArr[] = null;
		
		StringTokenizer st = new StringTokenizer(input);
		tokenCount = st.countTokens();
		for (idx=0; idx<tokenCount; idx++)
		{
			inputArr[idx] = st.nextToken();
		}

i get the exception @ inputArr[idx] = st.nextToken();


error message :

Exception in thread "main" java.lang.NullPointerException
	at ObtainKey.KeyAnalyzer(ObtainKey.java:21)
	at main.main(main.java:13)
2
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by hatux
0

inputArr is null, you must create the array before you use it.

e.g.

inputArr = new String[8];
0

@ di2daer

Sure, but i don't know it's length.
Depending on the input;
if user provides 3 words then it's so
If 4 so
and goes on.

Any ideas ?

0

countTokens gives the number of words.

tokenCount = st.countTokens();
inputArr = new String[tokenCount];

Or is this not the issue in your case?
Apart from that you can use a

Vector<String> v = new Vector<String>();

and then add each token

v.add(st.nextToken);

This link is quite useful:
http://www.devdaily.com/java/edu/pj/pj010006

Edited by di2daer: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.