Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

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

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

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

    Welcome to the forum! Please read the announcement topic at the top of most sub forums to learn how to properly post code ans ask questions.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

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

    Error occurs on line 29 of your code. Look at that line and work what is null. Trying printing out stuff on the line above. When you know what is null then work out why it is null. Then fix it!
    Improving the world one idiot at a time!

Similar Threads

  1. [SOLVED] error in my code(Exception in thread "main" java.lang.NullPointerException)
    By barhom in forum What's Wrong With My Code?
    Replies: 8
    Last Post: April 27th, 2013, 06:42 AM
  2. Replies: 1
    Last Post: April 7th, 2013, 03:40 PM
  3. Replies: 1
    Last Post: February 27th, 2013, 06:48 AM
  4. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  5. [SOLVED] Error Message: Exception in thread "main" java.lang.NullPointerException etc.
    By coffecupcake in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2011, 09:34 AM