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)

Recommended Answers

All 4 Replies

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

e.g.

inputArr = new String[8];

@ 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 ?

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

merci, the link was really handy!.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.