Results 1 to 7 of 7
  1. #1
    mobosecomin is offline Member
    Join Date
    Oct 2010
    Posts
    23
    Rep Power
    0

    Question Null Pointer exception (Again !!)

    Hi everyone I have a null pointer exception. I'm still not exactly sure how these work but this one is only on a string comparison so I'm really confused. Here is the code would appreciate being pointed in the right direction.

    Java Code:
    
    package GarageSystem;
    
    import java.io.*;
    
    public class GarageUpdate {
    
    
        public static void main (String [ ] args ) {
    
            // note file names coded into program these can also be passed in as parameters
            // Initialise variables
    
            // Opening Master File Data
            String omReg = " ";
            String omName = " ";
            String omAddress = " ";
            char omType = ' ';
            double omBalance = 0.0;
    
            // Closing Master File Data
            // used as an output buffer
            String cmReg = " ";
            String cmName = " ";
            String cmAddress = " ";
            char cmType = ' ';
            double cmBalance = 0.0;
    
            // Transaction Reg and Balance
            String transReg = " ";
            double transValue = 0.0D;
    
            try{                            // Try block
                // Set up Opening Master File for input
                FileReader inputFile1 = new FileReader( "om.txt" );
                BufferedReader br1 = new BufferedReader( inputFile1);
                StreamTokenizer om = new StreamTokenizer( br1);
    
                // Set up Closing Master File for output
                FileWriter outputFile = new FileWriter( "closingMaster.txt" );
                BufferedWriter cm = new BufferedWriter( outputFile);
    
                // Set up Transaction File for input
                FileReader inputFile2 = new FileReader( "trans.txt" );
                BufferedReader br2 = new BufferedReader( inputFile2);
                StreamTokenizer tr = new StreamTokenizer( br2);
    
                // Read  the first Opening Master record
                om.nextToken();
                omReg =  om.sval;
                om.nextToken ();
                omName = om.sval;
                om.nextToken ();
                omAddress = om.sval;
                om.nextToken ();
                omType = (char)om.nval;
                om.nextToken ();
                omBalance = om.nval;
    
                // Copy to output buffer
                cmReg = omReg;
                cmName = omName;
                cmAddress = omAddress;
                cmType =  omType;
                cmBalance = omBalance;
    
                // Loop thru and process all records in the Transaction File
                while (tr.nextToken () != StreamTokenizer.TT_EOF){
    
                    // Read a record from the Transaction File
                    tr.nextToken();
                    transReg =  tr.sval;
                    tr.nextToken();
                    transValue = tr.nval;
    
                    // If the (transReg != omReg) the current customer
                    // has no transactions today
                    // Write the output buffer to the Closing Master File
                    // and read the next record from the Opening Master File
                 
                    //***************************************************************************************
                    //***************************************************************************************
                    // Eclipse says the exception is in line below
                    //***************************************************************************************
                    //***************************************************************************************
    
                  while (! transReg.equals(omReg)){
                        cm.write( cmReg + "\t" + cmName + "\t" + cmAddress + "\t" + cmType + "\t" + cmBalance);
                        cm.newLine();
    
                        // Read a record from the Opening Master File
                        om.nextToken();
                        omReg =  om.sval;
                        om.nextToken ();
                        omName = om.sval;
                        om.nextToken ();
                        omAddress = om.sval;
                        om.nextToken ();
                        omType = (char)om.nval;
                        om.nextToken ();
                        omBalance = om.nval;
    
                        // Copy to output buffer
                        cmReg = omReg;
                        cmName = omName;
                        cmAddress = omAddress;
                        cmType =  omType;
                        cmBalance = omBalance;
                    }
    
                    // Customer has had a transaction today
                    // Update the customers balance
                    cmBalance = cmBalance + transValue;
                    //  System.out.println(cmBalance);
                    cm.write( cmReg + "\t" + cmName + "\t" + cmAddress + "\t" + cmType + "\t" + cmBalance);
                    cm.newLine();
    
                }                                   // End of Transaction File
    
                // Write last customer to have a transaction today
                // to the Closing Master File
                // cm.write( cmReg + "\t" + cmName + "\t" + cmAddress + "\t" + cmType + "\t" + cmBalance);
                // cm.newLine();
    
                // Process any records left in the Opening Master File
                while (om.nextToken () != StreamTokenizer.TT_EOF){
    
                    // Read a record from the Opening Master File
                    om.nextToken();
                    omReg =  om.sval;
                    om.nextToken ();
                    omName = om.sval;
                    om.nextToken ();
                    omAddress = om.sval;
                    om.nextToken ();
                    omType = (char)om.nval;
                    om.nextToken ();
                    omBalance = om.nval;
    
                    // Copy to output buffer
                    cmReg = omReg;
                    cmName = omName;
                    cmAddress = omAddress;
                    cmType =  omType;
                    cmBalance = omBalance;
    
                    // Write output buffer to the Closing Master File
                    cm.write( cmReg + "\t" + cmName + "\t" + cmAddress + "\t" + cmType + "\t" + cmBalance);
                    cm.newLine();
                }
    
                // Close all files
                br1.close();
                cm.close();
                br2.close();
            }                                       // End of try
    
            // Catch any exceptions thrown during processing
            catch (IOException ioe){                // Catch block
                System.err.println( ioe);
                return;
            }                                       // End of catch
        }                                           // End of main()
    }
    Last edited by mobosecomin; 03-29-2011 at 04:22 PM.

  2. #2
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    What is on your om.txt?Can you post a sample?
    I have just compile your code and there is no exception.Its okay.

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    The stacktrace that you got for the N.P.E contains an exact line number in your code of where the problem happened. At that line, one of the variables being dereferenced is null.

  4. #4
    mobosecomin is offline Member
    Join Date
    Oct 2010
    Posts
    23
    Rep Power
    0

    Default

    om.txt just contains

    00-tn-100 Ken Urlingford p 100
    01-kk-200 Nic tipp c 200

    and trans.txt contains

    00-tn-100 200

    I input the data directly into the file. I'm also not sure about the end of file marker and how it works. If I enter the data manually dose the end of file still work.? Thamks for looking Ujjal

  5. #5
    mobosecomin is offline Member
    Join Date
    Oct 2010
    Posts
    23
    Rep Power
    0

    Default

    oh ok now i see its still null after i run the nextToken.

    Java Code:
                om.nextToken();
                omReg =  om.sval;
                System.out.println(omReg);
    returns null

    I'm not sure why though

  6. #6
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    I got this error:
    Exception in thread "main" java.lang.NullPointerException
    at GarageUpdate.main(GarageUpdate.java:84)
    Line 84 is
    while (!transReg.equals(omReg)){
    this returns null.That means transReg or omReg or both are null.Check it.

    And initialization should be changed.like
    Java Code:
            String omReg = "";
            String omName ="";
            String omAddress= "";
    Regards
    Ujjal

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by UJJAL DHAR View Post
    I got this error:

    Line 84 is

    this returns null.That means transReg or omReg or both are null.Check it.

    And initialization should be changed.like
    Java Code:
            String omReg = "";
            String omName ="";
            String omAddress= "";
    Regards
    Ujjal
    That would mean transReg was null.
    If omReg was null then the exception would be thrown from inside the equals() method...and would therefore be a bug in the equals method.

Similar Threads

  1. Null pointer exception
    By samuel.roshni in forum Java Servlet
    Replies: 14
    Last Post: 01-22-2011, 03:25 PM
  2. null pointer exception
    By anthonym2121 in forum New To Java
    Replies: 7
    Last Post: 04-06-2009, 04:25 AM
  3. Null Pointer Exception
    By Jacinth in forum New To Java
    Replies: 4
    Last Post: 01-22-2009, 02:47 PM
  4. null pointer exception
    By cityguy503@yahoo.com in forum New To Java
    Replies: 4
    Last Post: 08-22-2008, 08:22 PM
  5. getting a null pointer exception
    By Rjava in forum XML
    Replies: 4
    Last Post: 07-16-2008, 06:56 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
  •