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 6 of 6

Thread: Error Message: Exception in thread "main" java.lang.NullPointerException etc.

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Error Message: Exception in thread "main" java.lang.NullPointerException etc.

    Hello, I'm quite obviously new the the forum but joined in hopes of figuring out various problems with my coding. I don't quite understand the error message I'm given (I'm very very new to Java programming) so bear with me, and thank you in advance for any help you are willing to offer.

    I get this output:
    Exception in thread "main" java.lang.NullPointerException
    	at WhileTokenizer.retreiveData(WhileTokenizer.java:55)
    	at WhileTokenizer.main(WhileTokenizer.java:33)

    Here is my complete code:
    import java.io.*;
     import java.util.StringTokenizer;
     
    public class WhileTokenizer {
     
            // File input variables
                private static FileInputStream inputFile;
                private static InputStreamReader inputReader;
                private static BufferedReader reader;
            // Tokenizer variable
                private static StringTokenizer token;
            // Variables to define data
                private static String line, firstName, lastName;
                private static double hourlyWage, overWage, totalPay, totalT,
                                      MonReg, MonPay, MonOver,
                                      TuesReg, TuesOver, TuesPay,
                                      WedsReg, WedsOver, WedsPay,
                                      ThursReg, ThursPay, ThursOver,
                                      FriReg, FriOver, FriPay;
     
    public static void main(String[] args) throws IOException
        {
        	//Methods
        	initiateFile();
        	retreiveData();
        	inputFile.close();
        }
     
    public static void initiateFile() throws IOException
        {
            //Import file to parse
        inputFile = new FileInputStream //Change this text file to the one on your computer!
            ("D:\\...data.txt"); \\I figured you didn't need to see my file location when looking for the error.. The txt file is three lines of data that needs to be parsed. (I will attach it so you can see for yourself).
        inputReader = new InputStreamReader(inputFile);
        reader = new BufferedReader(inputReader);
        }
     
    public static void retreiveData() throws IOException
        {
            //Data retreival
        line = reader.readLine();
            System.out.println("Gathered data line = " + line);
     
            while (line != null)
            {
                //Define variables
                    firstName = token.nextToken();
                    lastName = token.nextToken();
     
                    hourlyWage = Double.parseDouble(token.nextToken());
                    MonReg = Double.parseDouble(token.nextToken());
                    MonOver = Double.parseDouble(token.nextToken());
                    TuesReg = Double.parseDouble(token.nextToken());
                    TuesOver = Double.parseDouble(token.nextToken());
                    WedsReg = Double.parseDouble(token.nextToken());
                    WedsOver = Double.parseDouble(token.nextToken());
                    ThursReg = Double.parseDouble(token.nextToken());
                    ThursOver = Double.parseDouble(token.nextToken());
                    FriReg = Double.parseDouble(token.nextToken());
                    FriOver = Double.parseDouble(token.nextToken());
     
                    calculateTotal();
                    printResults();
     
                    line = reader.readLine();
            }
        }
     
    public static void calculateTotal() throws IOException
    {
        //Data processing
            overWage = (hourlyWage * 1.5);
     
            MonPay = (MonReg * hourlyWage);
            TuesPay = ((TuesReg * hourlyWage) + (overWage * TuesOver));
            WedsPay = ((WedsReg * hourlyWage) + (overWage * WedsOver));
            ThursPay = (ThursReg * hourlyWage);
            FriPay = ((FriReg * hourlyWage) + (overWage * FriOver));
     
            totalPay = (MonPay + TuesPay + WedsPay + ThursPay + FriPay);
            totalPay = Math.round(totalPay*100.000)/100.000;
    }
     
    public static void printResults() throws IOException
    {
        //Show Results
        System.out.println("Hello "+ firstName +" "+ lastName +".");
        System.out.println("You have made $" + totalPay + " this period.");
    }
     
    }

    Here is the content of the input text file.
    Kermit D.Frogge  17.25 5.25 0.0 8.0 1.5 8.0 2.25 7.75 0.0 8.0 2.0 
    Allyson Wonderland  10.18 6.75 0.0 8.0 1.5 8.0 2.25 8.0 3.0 6.5 0.0
    Rocky Balboa 14.95 8.0 3.0 8.0 2.25 8.0 2.0 8.0 3.25 8.0 1.25
    Juan Valdez 18.65 8.0 4.25 7.75 0.0 8.0 1.5 6.25 0.0 6.0 0.0
    Attached Files Attached Files
    Last edited by coffecupcake; November 14th, 2011 at 03:18 PM.


  2. #2
    Junior Member
    Join Date
    Sep 2011
    Location
    philadelphia
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Error Message: Exception in thread "main" java.lang.NullPointerException etc.

    you r missing one line like this

    token = new StringTokenizer(line);
    Acoolme is an Online Marketing Software Platform And Social Community

  3. The Following User Says Thank You to seohulu For This Useful Post:

    coffecupcake (November 15th, 2011)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Error Message: Exception in thread "main" java.lang.NullPointerException etc.

    Exception in thread "main" java.lang.NullPointerException
    at WhileTokenizer.retreiveData(WhileTokenizer.java:55 )
    At line 55 in WhileTokenizer one of the variables you are using has a null value. Look at that line of code and see if you can determine which variable has a null value. If you can not see which one, add a println statement just before line 55 and print out the values of all the variables used on line 55. Then you can see what variable has a null value. Now backtrack in your code and see why that variable does NOT have a valid value.

  5. The Following User Says Thank You to Norm For This Useful Post:

    coffecupcake (November 15th, 2011)

  6. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Error Message: Exception in thread "main" java.lang.NullPointerException etc.

    Thank you both! I was indeed missing my tokenizer. (I miss all the silly things...)

    Thanks again

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

    Default Re: Error Message: Exception in thread "main" java.lang.NullPointerException etc.

    StringTokenizer is not silly. Camelot is though!
    Improving the world one idiot at a time!

  8. #6
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Error Message: Exception in thread "main" java.lang.NullPointerException etc.

    Haha no, I mean I myself make silly errors

Similar Threads

  1. Need help with Exception in thread "main" java.lang.NullPointerException
    By JGeorge337 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 28th, 2011, 06:36 AM
  2. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  3. Exception in thread "main" java.lang.NullPointerException
    By manzili in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 9th, 2011, 12:02 PM
  4. Replies: 7
    Last Post: May 30th, 2011, 09:11 AM
  5. Replies: 6
    Last Post: November 12th, 2010, 04:40 AM

Tags for this Thread