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

Thread: Display .txt file in java using eclipse

  1. #1
    Junior Member
    Join Date
    Apr 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Display .txt file in java using eclipse

    I am trying to display a particular text file in JTextArea but eclipse keeps showing me an error.

    this the code :

    package readFile;

    import java.awt.EventQueue;
    import java.io.*;
    import java.nio.file.Path;
    import java.util.StringTokenizer;

    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    import javax.swing.JButton;
    import javax.swing.JTextArea;
    import javax.swing.text.JTextComponent;

    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;

    public class frmReadFile {

    JFileChooser dialog = new JFileChooser();

    private JFrame frmFrmreadfile;
    private JTextField txtPath;

    /**
    * Launch the application.
    */
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    try {
    frmReadFile window = new frmReadFile();
    window.frmFrmreadfile.setVisible(true);
    } catch (Exception e2) {
    e2.printStackTrace();
    }
    }
    });
    }

    /**
    * Create the application.
    */
    public frmReadFile() {
    initialize();
    }

    /**
    * Initialize the contents of the frame.
    */
    private void initialize() {
    frmFrmreadfile = new JFrame();
    frmFrmreadfile.setTitle("frmReadFile");
    frmFrmreadfile.setBounds(100, 100, 450, 300);
    frmFrmreadfile.setDefaultCloseOperation(JFrame.EXI T_ON_CLOSE);
    frmFrmreadfile.getContentPane().setLayout(null);

    txtPath = new JTextField();
    txtPath.setBounds(10, 11, 272, 27);
    frmFrmreadfile.getContentPane().add(txtPath);
    txtPath.setColumns(10);

    JButton btnBrowse = new JButton("Browse");
    btnBrowse.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
    int pFile = dialog.showOpenDialog(null);
    if (pFile == JFileChooser.APPROVE_OPTION) {
    File file = dialog.getSelectedFile();
    try {
    txtPath.setText("" + file);
    } catch (Exception e) {
    System.out.println("Error : " + e.getMessage());
    }
    }
    }
    });
    btnBrowse.setBounds(319, 11, 89, 23);
    frmFrmreadfile.getContentPane().add(btnBrowse);

    JButton btnTampil = new JButton("Tampil Teks");
    btnTampil.addActionListener(new ActionListener() {
    private JTextComponent tmpTeks;

    public void actionPerformed(ActionEvent e) {
    tmpTeks.setText(null);
    String path = txtPath.getText();
    File file = new File(path);
    try {
    FileInputStream fstream = new FileInputStream(file);
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;
    while ((strLine = br.readLine()) != null) {
    StringTokenizer st = new StringTokenizer(strLine, ".");
    tmpTeks.setText(tmpTeks.getText() + st.nextToken() + "\n");
    }
    in.close();
    } catch (Exception e2) {
    System.out.println("Error : " + e2.getMessage());
    }
    }
    });
    btnTampil.setBounds(10, 49, 89, 23);
    frmFrmreadfile.getContentPane().add(btnTampil);

    JTextArea tmpTeks = new JTextArea();
    tmpTeks.setBounds(10, 81, 398, 168);
    frmFrmreadfile.getContentPane().add(tmpTeks);
    }

    }


    when i push buttom btnTampil teks not show, any error in my code????
    need help please..

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Display .txt file in java using eclipse

    eclipse keeps showing me an error.
    Please specific the errors
    Whatever you are, be a good one

  3. #3
    Junior Member
    Join Date
    Apr 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Display .txt file in java using eclipse

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at readFile.frmReadFile$3.actionPerformed(frmReadFile .java:85)
    at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)


    the text file not show in JTextArea

  4. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: Display .txt file in java using eclipse

    at readFile.frmReadFile$3.actionPerformed(frmReadFile .java:85)
    which line the error pointed to ?
    Whatever you are, be a good one

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Display .txt file in java using eclipse

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at readFile.frmReadFile$3.actionPerformed(frmReadFile .java:85)
    There was a null value when the statement at line 85 was executed. Look at line 85 and find where the null value was. Then back track in the code to see where the null value came from.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Code to open up a txt file and display only first five lines?
    By darkr166 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 6th, 2014, 10:00 PM
  2. display text for 1 sec than remove txt java
    By hwoarang69 in forum Java Theory & Questions
    Replies: 35
    Last Post: March 16th, 2013, 12:31 PM
  3. Replies: 0
    Last Post: November 13th, 2012, 07:02 AM
  4. How to make Eclipse work with txt file?
    By Prostak in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 4th, 2011, 05:43 PM
  5. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM