Results 1 to 3 of 3
  1. #1
    fritz1474 is offline Member
    Join Date
    Sep 2008
    Posts
    8
    Rep Power
    0

    Default 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 */

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    at WordDef.main(WordDef.java:47).
    What code is at line 47 in WordDef?
    Is there a object reference (pointer) there that can be null?

  3. #3
    fritz1474 is offline Member
    Join Date
    Sep 2008
    Posts
    8
    Rep Power
    0

    Default

    This is the line 47 from the code:

    System.out.printf("%s%d%s%s ", "Enter a ", word[i].length(), " letter word meaning ", def[i]);

Similar Threads

  1. NullPointerException - createImage()
    By Bojevnik in forum AWT / Swing
    Replies: 8
    Last Post: 06-29-2008, 09:20 PM
  2. NullPointerException
    By adeeb in forum AWT / Swing
    Replies: 3
    Last Post: 06-11-2008, 08:42 AM
  3. NullPointerException
    By mensa in forum Java 2D
    Replies: 5
    Last Post: 05-03-2008, 11:19 PM
  4. NullPointerException
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-07-2007, 04:20 PM
  5. NullPointerException
    By Feng in forum New To Java
    Replies: 5
    Last Post: 11-24-2007, 07:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •