java.lang.NullPointerException in matching a keyword in a web page

V

Victoria

I want to find out the no of occurence of a keyword(Traditional
Chinese) in a web page (Traditional Chinese).
Now, I can get the web page content and display it.
And, I can get keyword from a test.txt file and display it.
However,I cannot count this keyword occurence in a web page and there
is a null pointer exeption .

How to solve this problem?
Below is the program with java.lang.NullPointerException.


import java.io.*;
import java.net.URL;

public class Check
{
// count the occurence of a keyword in a str (web site content)
public static int Count(String str, String keyword){
int count=0;

int index=str.indexOf(keyword);

while(index!=-1)
{
count++;
index=str.indexOf(keyword,index+1);

}
return count;
}



public static void main(String [] args) throws IOException
{
System.out.println("Please wait...");

// Retrieve first line (keyword) only in test.txt
File f_keyword = new File ( "test.txt" );
FileInputStream FIO = new FileInputStream (f_keyword );
BufferedReader inK = new BufferedReader(new InputStreamReader(
FIO ));

int kLine = 0;
String kline = null ;

while( (kLine < 2) && ((kline = inK.readLine()) != null))
{
kLine++;
System.out.println( kline ); // print the keyword
}



// Retrieve first 10 lines only in the web site
BufferedReader in = new BufferedReader(new InputStreamReader(
new
URL("http://www.yahoo.com.hk/" ).openStream() ));


int iLine = 0;
String line;
String total = null;


while( (iLine < 100) && ((line = in.readLine()) != null))
{
iLine++;
//System.out.println( line );
total += line;
}



System.out.println( total );

// count the occurence of the keyword
int result = Count(total,kline);
System.out.println( "Result : " + result );

}
}


Victoria
 
C

Chris Smith

Victoria said:
I want to find out the no of occurence of a keyword(Traditional
Chinese) in a web page (Traditional Chinese).
Now, I can get the web page content and display it.
And, I can get keyword from a test.txt file and display it.
However,I cannot count this keyword occurence in a web page and there
is a null pointer exeption .

How to solve this problem?
Below is the program with java.lang.NullPointerException.

The stack trace for the NullPointerException would help. It would also
help if you point out the specific lines pointed to by the stack trace,
since most newsreaders don't have convenient line-counting features.

In the mean time, someone may have the patience to read through your
code and painstakingly locate the error on their own, after the compiler
already told *you* exactly where it is and you failed to pass on that
information. That someone isn't me.

(Incidentally, I did take a short look, and the one thing I noticed is
that it's absolutely hideous to use "kline" and "kLine" as separate
identifiers in the same piece of code. That one pretty much convinced
me to stop looking.)

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
A

Andrew Thompson

| I want to find out the no of occurence of a keyword(Traditional
| Chinese) in a web page (Traditional Chinese).
| Now, I can get the web page content and display it.
| And, I can get keyword from a test.txt file and display it.
| However,I cannot count this keyword occurence in a web page and
there
| is a null pointer exeption .
|
| How to solve this problem?
| Below is the program with java.lang.NullPointerException.

The basic problem was that you were
reading your keyword (kline) _until_ it was
null. It could never be anything else.

I stored the results of the successive reads
in a separate variable, that solves the problem..
vis..
_________________
import java.io.*;
import java.net.URL;

public class Check
{
// count the occurence of a keyword in a str (web site
content)
public static int Count(String str, String keyword){
// System.out.println("C.c str: " + str);
System.out.println("C.c keyword: " + keyword);
int count=0;

int index=str.indexOf(keyword);

while(index!=-1)
{
count++;
index=str.indexOf(keyword,index+1);

}
return count;
}



public static void main(String [] args) throws IOException
{
System.out.println("Please wait...");

// Retrieve first line (keyword) only in test.txt
File f_keyword = new File ( "test.txt" );
FileInputStream FIO = new FileInputStream (f_keyword );
BufferedReader inK = new BufferedReader(new
InputStreamReader(
FIO ));

int kLine = 0;
String kline = new String(""), keyword = new String("");

while( (kLine < 2) && ((kline = inK.readLine()) != null))
{
kLine++;
keyword += kline;
System.out.println( "C.m kline: " + kline ); // print the
keyword
System.out.println( "C.m keyword: " + keyword ); // print
the keyword
}



// Retrieve first 10 lines only in the web site
BufferedReader in = new BufferedReader(new
InputStreamReader(
new
URL("http://www.yahoo.com.hk/" ).openStream() ));


int iLine = 0;
String line;
String total = new String("");


while( (iLine < 100) && ((line = in.readLine()) != null))
{
iLine++;
//System.out.println( line );
total += line;
}


// System.out.println( "C.m total: " + total );
System.out.println( "C.m kline: " + kline );
System.out.println( "C.m keyword: " + keyword );

// count the occurence of the keyword
int result = Count(total,keyword);
System.out.println( "C.m Result : " + result );

}
}
____________
 
A

Andrew Thompson

| Victoria wrote:
....
| (Incidentally, I did take a short look, and the one thing I
noticed is
| that it's absolutely hideous to use "kline" and "kLine" as
separate
| identifiers in the same piece of code. That one pretty much
convinced
| me to stop looking.)

I persevered, but it would have been a lot
quicker and easier if you (Victoria) had
followed the naming conventions.

'Count' - a method, should be 'count'

'Check' is a bad name for a class, it
is a verb, whereas class names should
be nouns.

'kline' and 'kLine' - Calvin? Longer and more
meaningful names would certainly have helped.

etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top