It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

Can you please help me with a simple code?

Can you please help me with a simple code?

Postby lzakl on Thu Apr 19, 2007 3:54 pm

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!!
lzakl
 
Posts: 0
Joined: Thu Apr 19, 2007 3:47 pm

Ok

Postby ggomez on Fri Apr 20, 2007 5:54 am

stobj and sbobj are out of any method and are taking variables with a null value.

Code: Select all
private String input,first,word, display;
String rest = "";

public Words(String s)
{
input = s;
}
StringTokenizer stobj = new StringTokenizer(input);
StringBuffer sbobj = new StringBuffer(display);


good luck
ggomez
 
Posts: 45
Joined: Tue Apr 17, 2007 3:30 am
Location: !!!!!MéXiCo¡¡¡¡¡¡


Who is online

Users browsing this forum: No registered users and 6 guests