split pblm running program

hello


am using this statement:

String[] f= in.readLine.split(" ");
where in is a BufferedReader

using it to split fields that are seperated by a space. while running it gives me warnings and it does nt give me the right answer, but the programs runs with out errors...

what do i do??????????????????????????????????????

the error i got is:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at NewJFrame.jButton1ActionPerformed(NewJFrame.java:672)
at NewJFrame.access$000(NewJFrame.java:17)
at NewJFrame$1.actionPerformed(NewJFrame.java:86)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6038)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
at java.awt.Component.processEvent(Component.java:5803)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4410)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2429)
at java.awt.Component.dispatchEvent(Component.java:4240)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)


10x

Comments

  • : hello
    :
    :
    : am using this statement:
    :
    : String[] f= in.readLine.split(" ");
    : where in is a BufferedReader
    :
    : using it to split fields that are seperated by a space. while running it gives me warnings and it does nt give me the right answer, but the programs runs with out errors...
    :
    : what do i do??????????????????????????????????????
    :
    : the error i got is:
    : Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    : at NewJFrame.jButton1ActionPerformed(NewJFrame.java:672)
    : at NewJFrame.access$000(NewJFrame.java:17)
    : at NewJFrame$1.actionPerformed(NewJFrame.java:86)
    : at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    : at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    : at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    : at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    : at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    : at java.awt.Component.processMouseEvent(Component.java:6038)
    : at javax.swing.JComponent.processMouseEvent(JComponent.java:3260)
    : at java.awt.Component.processEvent(Component.java:5803)
    : at java.awt.Container.processEvent(Container.java:2058)
    : at java.awt.Component.dispatchEventImpl(Component.java:4410)
    : at java.awt.Container.dispatchEventImpl(Container.java:2116)
    : at java.awt.Component.dispatchEvent(Component.java:4240)
    : at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
    : at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
    : at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
    : at java.awt.Container.dispatchEventImpl(Container.java:2102)
    : at java.awt.Window.dispatchEventImpl(Window.java:2429)
    : at java.awt.Component.dispatchEvent(Component.java:4240)
    : at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    : at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
    : at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
    : at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
    : at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
    : at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
    : at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
    :
    :
    : 10x
    :
    :
    I think you forgot the () after readLine. Now the compiler thinks readLine is a field in the BufferedReader, and since that field doesn't exist it throws a Null-pointer exception (first exception).
  • acually i did not forget it! this is the code:

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
    //find student and print corressponding line

    String file="students.txt";

    String sid=jTextField1.getText();
    String S;
    String info=" ";

    if (sid.equals(""))
    {
    JOptionPane.showMessageDialog(this,"Enter your ID above!");
    return;
    }
    try{
    BufferedReader in = new BufferedReader ( new FileReader(file));

    while ((S=in.readLine())!=null)
    {
    String[] fields= in.readLine().split("");
    if ((fields[0]).equals(sid))
    info+=S +"
    ";
    }
    in.close();
    } catch (IOException e) {
    JOptionPane.showMessageDialog(this,"Error Locating File!");
    }
    jTextArea1.setText(info);// TODO add your handling code here:
    }//GEN-LAST:event_jButton3ActionPerformed


    maybe i should have put the String[] fields= in.readLine().split(""); somewhere outside the loop is nt it????
  • : acually i did not forget it! this is the code:
    :
    : private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
    : //find student and print corressponding line
    :
    : String file="students.txt";
    :
    : String sid=jTextField1.getText();
    : String S;
    : String info=" ";
    :
    : if (sid.equals(""))
    : {
    : JOptionPane.showMessageDialog(this,"Enter your ID above!");
    : return;
    : }
    : try{
    : BufferedReader in = new BufferedReader ( new FileReader(file));
    :
    [red]
    : while ((S=in.readLine())!=null)
    : {
    : String[] fields= in.readLine().split("");
    [/red]
    : if ((fields[0]).equals(sid))
    : info+=S +"
    ";
    : }
    : in.close();
    : } catch (IOException e) {
    : JOptionPane.showMessageDialog(this,"Error Locating File!");
    : }
    : jTextArea1.setText(info);// TODO add your handling code here:
    : }//GEN-LAST:event_jButton3ActionPerformed
    :
    :
    : maybe i should have put the String[] fields= in.readLine().split(""); somewhere outside the loop is nt it????
    :
    The red part is the problem. First you read a line and assign it to S. Then you read the next line and split it. If your file has an uneven number of lines, the last line is read and checked if it is not equal to null. Then the last+1 line is read to perform the split.
    Solution: only use S inside the loop, since that is the line you just read.
  • U mean that I should say:

    String[] fields= S.split(""); in side the loop????

    another thing: i didnt understand in ur previous reply why should we know the length of the fields of each record if i have to concatinate two fields???

    thankss a lot

  • : U mean that I should say:
    :
    : String[] fields= S.split(""); in side the loop????
    :
    : another thing: i didnt understand in ur previous reply why should we know the length of the fields of each record if i have to concatinate two fields???
    :
    : thankss a lot
    :
    :
    You don't if you're only writing the fields, but if you're reading them you need some way to differentiate between the individual fields. My previous reply described two methods: fixed-length or delimited fields. You can choose either one of them, depending on your preferences or some protocol or format description.
  • am sorry man taking ur time but i guess u now know well what am working on

    plz...i want to add students to a file called enrollmen.txt:

    //enrolling a Student.
    String S;
    String file;

    String S1="
    "+jTextField1.getText().trim();
    String S2="
    "+jTextField2.getText().trim();

    String L=jTextField1.getText().trim()+" "+jTextField2.getText().trim()+"
    ";

    if (S1.equals("
    ") && S2.equals("
    "))
    {
    JOptionPane.showMessageDialog(this,"Enter CRN and your ID");
    return;
    }

    else if (S2.equals("
    "))
    {
    JOptionPane.showMessageDialog(this,"Insert CRN!");
    return;
    }
    else if (S1.equals("
    "))
    {
    JOptionPane.showMessageDialog(this,"Insert you ID!");
    return;
    }

    file="enrollments.txt";

    try{
    BufferedReader in = new BufferedReader ( new FileReader(file));
    while ((S=in.readLine())!=null)
    {
    if (S.equals(L))
    {
    JOptionPane.showMessageDialog(this,"This Student Already Registered This Course: Cannot Add twice!");
    return;
    }
    }
    in.close();
    BufferedWriter out = new BufferedWriter ( new FileWriter(file,true));
    out.append(L);
    out.close();

    }

    catch (IOException e) {
    JOptionPane.showMessageDialog(this,"Error locating file. Please make sure the file is in the right directory!");
    }


    this is my code

    z pblem is that every time i enroll a student the file will append every record in the same line!!! it is not reding he "
    " thing! is trim() causing the pblem?? or what to do??

    another question:

    i want to delete the record from the file...if i delete it a null will occur...how to resolve the issue and make user able to delete and replace? and how to make user be able to modify a record?

    plzzz answer me these three questions and i'll be very thankful

    thanks a lot n srry taking ur time)

  • : am sorry man taking ur time but i guess u now know well what am working on
    :
    : plz...i want to add students to a file called enrollmen.txt:
    :
    : //enrolling a Student.
    : String S;
    : String file;
    :
    : String S1="
    "+jTextField1.getText().trim();
    : String S2="
    "+jTextField2.getText().trim();
    :
    : String L=jTextField1.getText().trim()+" "+jTextField2.getText().trim()+"
    ";
    :
    : if (S1.equals("
    ") && S2.equals("
    "))
    : {
    : JOptionPane.showMessageDialog(this,"Enter CRN and your ID");
    : return;
    : }
    :
    : else if (S2.equals("
    "))
    : {
    : JOptionPane.showMessageDialog(this,"Insert CRN!");
    : return;
    : }
    : else if (S1.equals("
    "))
    : {
    : JOptionPane.showMessageDialog(this,"Insert you ID!");
    : return;
    : }
    :
    : file="enrollments.txt";
    :
    : try{
    : BufferedReader in = new BufferedReader ( new FileReader(file));
    : while ((S=in.readLine())!=null)
    : {
    : if (S.equals(L))
    : {
    : JOptionPane.showMessageDialog(this,"This Student Already Registered This Course: Cannot Add twice!");
    : return;
    : }
    : }
    : in.close();
    : BufferedWriter out = new BufferedWriter ( new FileWriter(file,true));
    : out.append(L);
    : out.close();
    :
    : }
    :
    : catch (IOException e) {
    : JOptionPane.showMessageDialog(this,"Error locating file. Please make sure the file is in the right directory!");
    : }
    :
    :
    : this is my code
    :
    : z pblem is that every time i enroll a student the file will append every record in the same line!!! it is not reding he "
    " thing! is trim() causing the pblem?? or what to do??
    :
    : another question:
    :
    : i want to delete the record from the file...if i delete it a null will occur...how to resolve the issue and make user able to delete and replace? and how to make user be able to modify a record?
    :
    : plzzz answer me these three questions and i'll be very thankful
    :
    : thanks a lot n srry taking ur time)
    :
    :
    Depending on the OS you're running line endings are as follows:
    - Windows

    - Linux

    - Mac

    A better way of handling line endings is to use BufferedReader.ReadLine() and BufferedWriter.WriteLine() methods.
    I myself wouldn't use a file as primary storage for the list. I would use an ArrayList for that. That way you can simply write the entire list into the file. Deleting a single line from a file often requires copying the file except for the line to delete.
    If you want to truly be able to delete records from a file, without copying the file, you need to change the structure into a fixed record length structure and change the file into a binary file. That way you can delete a record by either moving the records after it forward and truncating the file.
    This also holds true for modifying a record in a file. Note that text files are simply line-ending-delimited binary files. This makes it quite difficult to modify individual lines since they might end up longer than the original and the line-ending might be deleted.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories