Results 1 to 5 of 5
  1. #1
    DragonGhola is offline Member
    Join Date
    Jun 2011
    Location
    Louisville, KY
    Posts
    6
    Rep Power
    0

    Default BufferedReader throwing strange error

    Hi, I'm a new member but a fairly advanced Java user. I'm working on a project right now that requires me to read in and extract information from a text file provided by the user. Every time I try to use the BufferedReader to do line by line reading, it throws Error: null randomly somewhere in the output and will not read anything in. I've tried switching up so many things, I have no idea what's wrong. Here's the relevant code:

    Java Code:
    private ArrayList<PageFingerPrint> DatabaseReader(String dbName) {
            ArrayList<PageFingerPrint> pArray = new ArrayList<PageFingerPrint>();
            String eolMarker = ";";
            String sepMarker = ",";
            try {
                // Open the file that is the first
                // command line parameter
                FileInputStream fstream = new FileInputStream(dbName);
                // Get the object of DataInputStream
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String strLine;
                PageFingerPrint p;
                strLine = br.readLine();
                //Read File Line By Line
                while (strLine != null && !strLine.equals("")) {
                    // Add the content to pArray
                    if (strLine.substring(0, 1).equals("D") || strLine.substring(0, 1).equals("F")) {
                        p = new PageFingerPrint();
                        p.setActualType(strLine.substring(0,4));
                        System.out.println(p.toString());
                        for (int a = 5; a < (strLine.length()-5); a++) {
                            System.out.println("A = " + a);
                            System.out.println("StrLineLength " + (strLine.length()-5));
                            System.out.println(a < (strLine.length()-5));
                            System.out.println("Entered Loop");
                            if (!strLine.substring(a, a + 1).equals(eolMarker) && !strLine.substring(a, a + 1).equals(sepMarker)) {
                               System.out.println("First if success");
                               if (strLine.substring(a + 4, a + 5).equals(eolMarker) || strLine.substring(a + 4, a + 5).equals(sepMarker)) {
                                   String s = p.toString();
                                   System.out.println(s);
                                   p.addtoAlt(strLine.substring(a, a + 4));
                                   
                                }
                               System.out.println("Outside second if");
                            }
                            System.out.println("Outside first if");
                        }
                        pArray.add(p);
                        System.out.println("Line Added");
                    }
                    strLine = br.readLine();
                }
                //Close the input stream
                in.close();
                
            } catch (Exception e) {//Catch exception if any
                System.err.println("Error: " + e.getMessage());
            }
            return pArray;
        }


    The code should be outputting "Second if success", etc, but doesn't. I can attach the .txt file if necessary, but I don't think it's an issue with that.
    Thanks for any help you can provide :)
    Last edited by DragonGhola; 06-16-2011 at 03:22 AM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    a) Please post the full error message...in other words when catching an exception call e.printStackTrace() to see the full stack trace (line numbers, etc...) which contains a wealth of information above and beyond the exception message b) Please flank your code with the code tags so the code is properly formatted and readable

  3. #3
    DragonGhola is offline Member
    Join Date
    Jun 2011
    Location
    Louisville, KY
    Posts
    6
    Rep Power
    0

    Default

    Thanks for the help :) Here's the result of the stackTrace -
    Error: null
    java.lang.NullPointerException
    at drsppgui.PageFingerPrint.toString(PageFingerPrint. java:41)
    at drsppgui.DRSPPSystem.DatabaseReader(DRSPPSystem.ja va:137)
    at drsppgui.DRSPPSystem.runExample(DRSPPSystem.java:5 1)
    at drsppgui.DRSPPSystem.main(DRSPPSystem.java:295)
    at drsppgui.FileInputScreen.jButton1ActionPerformed(F ileInputScreen.java:179)
    at drsppgui.FileInputScreen.access$100(FileInputScree n.java:18)
    at drsppgui.FileInputScreen$2.actionPerformed(FileInp utScreen.java:69)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6289)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:605 4)
    at java.awt.Container.processEvent(Container.java:204 1)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4652)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2099)
    at java.awt.Component.dispatchEvent(Component.java:44 82)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:44 82)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:644)
    at java.awt.EventQueue.access$000(EventQueue.java:85)
    at java.awt.EventQueue$1.run(EventQueue.java:603)
    at java.awt.EventQueue$1.run(EventQueue.java:601)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:617)
    at java.awt.EventQueue$2.run(EventQueue.java:615)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 614)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    And here is the relevant part of the PageFingerPrint Class -
    Java Code:
     @Override
        public String toString()
        {
            StringBuffer sb = new StringBuffer();
            sb.append("Acutal Type - " + actualType);
            sb.append("Produced Types - ");
            for(int x = 0; x < altTypes.size(); x++)
            {
                sb.append(altTypes.get(x) + ", ");
            }
            return sb.toString();
        }
    Thanks again for any help :)

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Add some println statements in there to check which one of the variables you are accessing within that method is null. Without an SSCCE, a comment identifying the line the exception is thrown on, or the code of the class its hard to point you further (although I'm guessing altTypes is null as string addition of a null object would not throw an exception)
    Last edited by doWhile; 06-16-2011 at 03:29 AM.

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

    Default

    Java Code:
                FileInputStream fstream = new FileInputStream(dbName);
                // Get the object of DataInputStream
                DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
    Why so many layers for reading a file?
    Why not simply a FileReader wrapped in a BufferedReader?

Similar Threads

  1. BufferedReader & PrintWriter error
    By robertmacedonia in forum Networking
    Replies: 0
    Last Post: 09-15-2010, 11:57 PM
  2. Replies: 1
    Last Post: 05-06-2010, 10:06 PM
  3. BufferedReader error?
    By Umogrim in forum New To Java
    Replies: 2
    Last Post: 04-27-2010, 07:58 PM
  4. BufferedReader error cannot resolve symbol
    By SwEeTAcTioN in forum New To Java
    Replies: 12
    Last Post: 10-22-2009, 05:22 AM
  5. Which statement is throwing a runtime error....
    By money123 in forum New To Java
    Replies: 1
    Last Post: 07-30-2007, 12:55 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
  •