Results 1 to 3 of 3
Thread: NullPointerException issue
- 09-03-2008, 04:26 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 8
- Rep Power
- 0
NullPointerException issue
I am hoping someone can help me out here. I am trying to run an example provided by an instructor and I am not able to get it running properly. The input is console based, should be able to enter 1, 2, or 3 but when I enter any number other than 1, I am getting the following error message:
Exception in thread "main" java.lang.NullPointerException
at WordDef.main(WordDef.java:47).
Here is the text of the example:
//Program Title: Word Definition Check Program
import java.util.*;
import javax.swing.JOptionPane;
import java.io.*;
public class WordDef
{
public static void main(String[] args) throws IOException
{
Scanner input = new Scanner (System.in);
int i = 0;
String[] word = new String[3];
String[] def = new String[3];
File myJavaCode = new File("C:\\java\\defs.txt");
BufferedReader fileInput = new BufferedReader(new FileReader(myJavaCode));
String s = fileInput.readLine();
//First method to parse input line
StringTokenizer sT = new StringTokenizer(s,"-");
word[i] = new String(sT.nextToken().trim().toLowerCase());
def[i] = new String(sT.nextToken().trim());
i++;
//Second method to parse input line
//String [] sA = fileInput.readLine().split("-");
//word[i] = new String(sA[0].trim().toLowerCase());
//def[i] = new String(sA[1].trim());
i++;
s = fileInput.readLine();
//Third method to parse input line
//word[i] = new String(s.substring(0,s.indexOf('-')).trim().toLowerCase());
//def[i] = new String(s.substring(s.indexOf('-')).trim());
System.out.print("Enter a number from 1 to " + word.length + " ");
i = (Integer.parseInt(input.nextLine()) - 1);
System.out.printf("%s%d%s%s ", "Enter a ", word[i].length(), " letter word meaning ", def[i]);
//Get user response and convert to lower case
String inWord = input.nextLine().trim().toLowerCase();
//Manipulate input String using String methods to assess correctness of response
inWord = inWord.replaceAll(word[i], "6");
inWord = inWord.replaceAll("[^6]", "@");
inWord = inWord.replaceFirst("[@]", "Incorrect!");
inWord = inWord.replaceAll("[@]", " ");
inWord = inWord.replaceAll("[6]", "Correct!");
//Show results
JOptionPane.showMessageDialog(null, "And the verdict is...\n\n" + inWord); //, JOptionPane.TOP_ALIGNMENT, JOptionPane.LEFT_ALIGNMENT);
System.exit(0);
}
}
/* Program reads word definitions from a file and queries user regarding its contents.
**Assumptions:
1) Only three definitions are to be read from file
2) Program can be used as a shell to create more robust app at a later date */
- 09-03-2008, 05:19 PM #2
What code is at line 47 in WordDef?at WordDef.main(WordDef.java:47).
Is there a object reference (pointer) there that can be null?
- 09-03-2008, 05:21 PM #3
Member
- Join Date
- Sep 2008
- Posts
- 8
- Rep Power
- 0
Similar Threads
-
NullPointerException - createImage()
By Bojevnik in forum AWT / SwingReplies: 8Last Post: 06-29-2008, 09:20 PM -
NullPointerException
By adeeb in forum AWT / SwingReplies: 3Last Post: 06-11-2008, 08:42 AM -
NullPointerException
By mensa in forum Java 2DReplies: 5Last Post: 05-03-2008, 11:19 PM -
NullPointerException
By ravian in forum New To JavaReplies: 2Last Post: 12-07-2007, 04:20 PM -
NullPointerException
By Feng in forum New To JavaReplies: 5Last Post: 11-24-2007, 07:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks