Results 1 to 3 of 3
  1. #1
    jeremy28 is offline Member
    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Default I get the error Exception in thread "main" java.lang.NullPointerException?

    Hi all;

    I'm new to java.

    I have a program that is intented to get a text file (in here,ScanTest.txt) containing some names and values such as:

    name=jeremy
    #This is a comment
    account=1234
    balance=500
    ....

    The line starting with "#" is considered as a comment.

    Then, I have a method called "getString" so that if I give a name to it, it should return corresponding value as following:

    name---->jeremy
    balance---->500

    To implement this program, I've used "hashtable" class. Here is my code:

    package io;

    import java.io.*;
    import java.util.*;

    public class HashTableDemo {
    static String fileName;
    static Hashtable hash;
    static Enumeration names;

    public HashTableDemo() {
    fileName = "D:\\Documents and Settings\\Bahareh\\Desktop\\ScanText.txt";
    hash = new Hashtable();
    }
    public static String getString(String st)
    {
    return (String)hash.get(st);
    }

    public static void main(String args[]) throws Exception{
    // Create a hash map
    //Hashtable hash = new Hashtable();
    try{
    String st;
    InputStream in = new FileInputStream(fileName);
    Scanner sc = new Scanner(in).useDelimiter("=");
    while (sc.hasNext()) {
    hash.put(sc.next(), sc.next());
    }
    // Show all hashes in hash table.
    names = hash.keys();
    while (names.hasMoreElements()) {
    str = (String) names.nextElement();
    System.out.println(str + ": "+ /*hash.get(str)*/getString(str));
    }
    System.out.println();
    }
    catch(IOException e)
    {
    System.out.print(e.getMessage());
    }
    }
    }
    When I run it, I get the error:
    ------------------------------------------------------
    Exception in thread "main" java.lang.NullPointerException
    at java.io.FileInputStream.<init>(FileInputStream.jav a:134)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:97)
    at io.HashTableDemo.main(HashTableDemo.java:29)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 2 seconds)
    ------------------------------------------------------

    What's the problem?

    Besides, I want to ignore the lines starting with "#" sign and not to add them to the hash table.

    How should I seperate these lines?
    How does java recognize that first character of a line is "#" sign?

    Please help...

    TIA

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,564
    Rep Power
    23

    Default Re: I get the error Exception in thread "main" java.lang.NullPointerException?

    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default Re: I get the error Exception in thread "main" java.lang.NullPointerException?

    Is this an assignment? If not, just use Properties (Java Platform SE 6).
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Replies: 4
    Last Post: 12-22-2012, 06:40 AM
  2. Replies: 5
    Last Post: 12-21-2011, 06:44 PM
  3. Replies: 10
    Last Post: 07-20-2011, 02:11 PM
  4. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  5. Replies: 1
    Last Post: 07-16-2009, 10:35 AM

Posting Permissions

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