Hello! I`m new in this forum and I need a help!
I've created this code. When I compile, it doesnt show any error.
But when I run, it shows the folowing error:
Exception in thread "main" java.lang.NullPointerException
at java.util.StringTokenizer.<init>(StringTokenizer.java:182)
at java.util.StringTokenizer.<init>(StringTokenizer.java:219)
at Words.<init>(Words.java:14)
at RunWords.main(RunWords.java:11)
Well... thats the code.
Provider class:
import java.util.*;
public class Words
{
private int numbword = 1;
private int numbfirst = 1;
private String input,first,word, display;
String rest = "";
public Words(String s)
{
input = s;
}
StringTokenizer stobj = new StringTokenizer(input);
StringBuffer sbobj = new StringBuffer(display);
public void firstWork()
{
while(stobj.hasMoreTokens())
{
if(numbword==1)
{//if
word = stobj.nextToken();
sbobj.append(word);
}//if
else
{//else
rest = rest + "\n" + stobj.nextToken();
}//else
numbword++;
}//while
}
public void firstWord()
{
while(word.length()>numbfirst)
{
sbobj.insert((numbfirst*3) - 2, "\n");
}
}
public String displayFirst()
{
return display;
}
public String displayRest()
{
return rest;
}
}
Driver class:
import javax.swing.*;
import java.io.*;
public class RunWords
{
public static void main(String[] args) throws IOException
{
System.out.println("enter sentence...");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
Words wordobj = new Words(s);
wordobj.firstWork();
wordobj.firstWord();
System.out.println(wordobj.displayFirst());
System.out.println(wordobj.displayRest());
}
}
I would really apreciate if you could help me!!
thanks!!


