Results 1 to 3 of 3
- 08-02-2007, 04:01 AM #1
Member
- Join Date
- Jul 2007
- Posts
- 18
- Rep Power
- 0
I really need help on this final project
I want to be able to read strings from a file, and search if a word im looking for is in there. if it is, a message pane will pop up saying yes. if not, then it will say no. Right now, i can't seem to get the two methods to connect. THeres always an error.
Code:
import java.io.*;
import java.util.*;
import java.awt.Color;
import javax.swing.*;
public class AccessFile {
public String phrase;
public void displayFile(String f) throws IOException {
String phrase = f;
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
f = br.readLine();
while (f != null) {
System.out.println(f);
f = br.readLine();
}
br.close();
fr.close();
}
public void search(){
UIManager.put("OptionPane.background", Color.white);
UIManager.put("Panel.background", Color.white);
String word = JOptionPane.showInputDialog (null, "Please enter the word you are looking for", "Find", JOptionPane.OK_CANCEL_OPTION);
StringTokenizer st = new StringTokenizer(phrase);// Created a new object, that will allow me to tokenize String.
/** Basically, boolean starts out as false. if the StringTokenizer st has more token, it will keep
* reading each word. If one of the words in the string equal to the word the user entered, then
* boolean will turn true.
*/
boolean compare = false;
while (st.hasMoreTokens())
{
if (st.nextToken().equals (word))
compare = true;
}
/** If boolean was true, then it will show up the message screen saying word found. If boolean isn't true, then
* a screen will pop up saying you sure you entered it correctly.
*/
if (compare){
JOptionPane.showMessageDialog (null, "Word Found.", "Word is found!", JOptionPane.PLAIN_MESSAGE);
System.out.println((word));
}
else{
JOptionPane.showMessageDialog (null, "You sure you entered it correctly?", "Word is not found!", JOptionPane.PLAIN_MESSAGE);
}
}
}
Error:
NullPointerException:
at java.util.StringTokenizer.<init>(Unknown Source)
at java.util.StringTokenizer.<init>(Unknown Source)
at AccessFile.search(AccessFile.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
at java.lang.reflect.Method.invoke(Unknown Source)
>
PLz help. i really need it. Some tips, editing of the code, etc.Last edited by jason27131; 08-02-2007 at 04:48 AM.
- 08-02-2007, 04:55 AM #2
Senior Member
- Join Date
- Jul 2007
- Posts
- 130
- Rep Power
- 0
This seems like the extension of the String Tokenizer problem u write in another thread
I'll try analyzin ur code for now, ill inform u if i get some progress, good luck with ur project
- 08-03-2007, 04:47 AM #3
Senior Member
- Join Date
- Jul 2007
- Posts
- 130
- Rep Power
- 0
I don't know if u recognize, but u didn't put any link between the 2 method actually, what were u trying to do :confused:
From my point of view it seems that u were using te search method to search inside the file and find a matching words
The problem is in the search method, u declare the string tokenizer with the input parameter of phrase, in which phrase itself havent been initialized at all, check ur code, it's bit hard reading the code without the indents and tabs
This error
refer to the instantiation of the StringTokenizer after all, it could not find variable phrase, which leads to nullJava Code:at java.util.StringTokenizer.<init>(Unknown Source) at java.util.StringTokenizer.<init>(Unknown Source) at AccessFile.search(AccessFile.java:30)
Similar Threads
-
Project Help
By XxHEXSORxX in forum AWT / SwingReplies: 4Last Post: 01-28-2009, 10:01 AM -
Could anyone help with project?
By billdara in forum New To JavaReplies: 1Last Post: 03-12-2008, 05:05 PM -
need help, weird question kinda.
By carlos123 in forum New To JavaReplies: 6Last Post: 01-22-2008, 03:19 AM -
First Project Need Big Help
By earl in forum New To JavaReplies: 1Last Post: 01-18-2008, 06:12 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks