Hello everyone,I have little problem over here with my work. It gives null pointer about JTextArea in ActionListener. Could anyone help me,please? Thanks in advance
Here's code:

import java.awt.event.*;
import java.io.*;

import javax.swing.*;

/**
 * @author Nikola
 * 
 */

public class FileListener implements ActionListener {

    private JMenuItem exit;
    private JMenuItem new1, open, save, saveas, wordcount, fileviewer;
    private JTextArea area;
    private JTextField field;
    private JFileChooser fileChoose;

    public FileListener(JMenuItem exit, JMenuItem wordcount, JTextArea area,
            JTextField field, JMenuItem open, JMenuItem save, JMenuItem saveas) {
        this.exit = exit;
        this.wordcount = wordcount;
        this.area = area;
        this.field = field;
        this.open = open;
        this.save = save;
        this.saveas = saveas;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == exit) {
            System.exit(0);
        }

        else if (e.getSource() == wordcount) {
            if (area.getText().equals("")) {
                JOptionPane.showMessageDialog(null, "Text file is empty",
                        "Message", JOptionPane.ERROR_MESSAGE);
            }

            else {

                String in = area.getText();

                String[] words = in.split(" "); // separate string around spaces
                int count = words.length; // count number of wotrds

                JOptionPane.showMessageDialog(null, "The number of words is:  "
                        + count, "WordCount", JOptionPane.INFORMATION_MESSAGE);
            }

        }

        else if (e.getSource() == new1) {
            area.setText("");
            // field.setEditable(true);
            field.setText("");
        }

        else if (e.getSource() == open) {

            int result = fileChoose.showOpenDialog(area);
            File file = fileChoose.getSelectedFile();
            if (file != null && result == JFileChooser.APPROVE_OPTION) {
                try {
                    // open file
                    BufferedReader in = new BufferedReader(new FileReader(file));
                    StringBuffer sb = new StringBuffer(); // holds the incoming
                                                            // data

                    // read all data from file
                    String line = null;
                    while ((line = in.readLine()) != null) {
                        sb.append(line + "\n");
                    }

                    // done getting data, place it in the text area
                    area.setText(sb.toString());
                } catch (IOException ioe) {
                    // unexpected problem as file was found
                    System.out.println(ioe.getMessage());
                    ioe.printStackTrace();
                }
            } // end if

        }

        else if (e.getSource() == save) {
            String fileName = field.getText();
            File newFile = new File(fileName);

            String whatIsIn = area.getText();

            try {

                if (newFile.exists()) {
                    FileWriter fos = new FileWriter(newFile);
                    fos.write(whatIsIn);
                    fos.close();

                }

            } catch (IOException ioe) {
                ioe.printStackTrace();
            }

        }

        else if (e.getSource() == saveas) {

            int result = fileChoose.showSaveDialog(area);
            File file = fileChoose.getSelectedFile();
            if (file != null && result == JFileChooser.APPROVE_OPTION)
                try {
                    BufferedWriter out = new BufferedWriter(
                            new FileWriter(file));
                    out.write(area.getText());
                    out.close();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }

            String setNameFile = file.getName();
            field.setText(setNameFile);

        }

    }

}




  import javax.swing.*;

   import java.awt.*;
import java.awt.event.*;


public class FileViewer extends JFrame
{
     private JTextField field;
    private JTextArea area;
     private JLabel name;
     private JMenu file,tools,help;
     private JButton setfont;
     private JMenuItem new1,open,save,saveas,exit,wordcount,fileviewer;
     private JComboBox font,size;
     private JMenuBar bar;
     private JPanel jp1,jp2,jp3,jp4,jp5,jp6;
    // private JFileChooser jf;

    public FileViewer()
    {
        setTitle("FileViewer");
        setLocation(250,200);
        setSize(640,505);   
        setResizable(false);
        setLayout(new BorderLayout());

        bar=new JMenuBar();
        setJMenuBar(bar);

        file=new JMenu("File");
        bar.add(file);

        tools=new JMenu("Tools");
        bar.add(tools);

        help=new JMenu("Help");
        bar.add(help);

        new1= new JMenuItem("New");
        new1.setMnemonic('N');
        file.add(new1);

        open=new JMenuItem("Open");
        open.setMnemonic('O');
        file.add(open);

        save=new JMenuItem("Save");
        save.setMnemonic('S');
        file.add(save);

        saveas=new JMenuItem("Save as");
        saveas.setMnemonic('S');
        file.add(saveas);

        exit=new JMenuItem("Exit");
        exit.setMnemonic('E');
        file.add(exit);

        wordcount=new JMenuItem("Word Count");
        wordcount.setMnemonic('W');
        tools.add(wordcount);

        fileviewer=new JMenuItem("About");
        fileviewer.setMnemonic('A');
        help.add(fileviewer);

        jp1=new JPanel();
        add(jp1,"West");

        area=new JTextArea(25,40);
        area.setLineWrap(true);
        area.setWrapStyleWord(true);
        jp1.add(area);

        //Panel for East
        jp2=new JPanel();
        add(jp2,"East");
        jp2.setLayout(new BorderLayout(174,174));

        String[] strfont= {"Regular","Italic","Bold","Bold Italic"};
        font=new JComboBox(strfont);
        Object obj1 = font.getSelectedItem();
        jp2.add(font,"North");

        String[] strsize= {"10","12","15"};
        size=new JComboBox(strsize);
        Object obj2 = size.getSelectedItem();

        jp2.add(size,"Center");

        setfont=new JButton("Set Font");
        setfont.setMnemonic('S');
        jp2.add(setfont,"South");

        //South
        jp3=new JPanel();
        add(jp3,"South");

        name=new JLabel("File Name: ");
        jp3.add(name);

        field=new JTextField(10);
        field.setEditable(false);
        jp3.add(field);

        // help listener
        HelpListener help = new HelpListener(fileviewer);
        fileviewer.addActionListener(help); // add to help listener

        // file listener

        FileListener listening = new FileListener(exit, wordcount,area,field,open,save,saveas);
        exit.addActionListener(listening);
        wordcount.addActionListener(listening);
        new1.addActionListener(listening);
        open.addActionListener(listening);
        save.addActionListener(listening);
        saveas.addActionListener(listening);
        //area.addActionListener(listening);
        // TODO  setfont.addActionListener(listening);
        //jf.addActionListener(listening);



/*        Listener8 listening= new Listener8(field, area, setfont, new1, open, save, saveas, exit, wordcount, fileviewer,font,size, jf);

        field.addActionListener(listening);



        fileviewer.addActionListener(listening);
        font.addActionListener(listening);
        //size.addActionListener(listening);
    //  jf.addActionListener(listening);
        */

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);

    }

    public static void main(String[]args)
    {
        FileViewer int8=new FileViewer();
    }

}

and here is the ERROR that shows :

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at FileListener.actionPerformed(FileListener.java:113)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)

at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)

at javax.swing.AbstractButton.doClick(AbstractButton.java:389)

at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:809)

at com.apple.laf.AquaMenuItemUI.doClick(AquaMenuItemUI.java:137)

at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:850)

at java.awt.Component.processMouseEvent(Component.java:6375)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)

at java.awt.Component.processEvent(Component.java:6140)

at java.awt.Container.processEvent(Container.java:2083)

at java.awt.Component.dispatchEventImpl(Component.java:4737)

at java.awt.Container.dispatchEventImpl(Container.java:2141)

at java.awt.Component.dispatchEvent(Component.java:4565)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4619)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4280)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4210)

at java.awt.Container.dispatchEventImpl(Container.java:2127)

at java.awt.Window.dispatchEventImpl(Window.java:2482)

at java.awt.Component.dispatchEvent(Component.java:4565)

at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:684)

at java.awt.EventQueue.access$000(EventQueue.java:85)

at java.awt.EventQueue$1.run(EventQueue.java:643)

at java.awt.EventQueue$1.run(EventQueue.java:641)

at java.security.AccessController.doPrivileged(Native Method)

at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)

at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)

at java.awt.EventQueue$2.run(EventQueue.java:657)

at java.awt.EventQueue$2.run(EventQueue.java:655)

at java.security.AccessController.doPrivileged(Native Method)

at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:654)

at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)

at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Recommended Answers

All 4 Replies

It gives null pointer

Look at line 113 and find the variable with the null value. Then backtrack in the code to see why that variable does not have a valid value.

Yes,I know there's problem in that line but I can not see the problem and I do not understand why is it?

A JFileChooser's showSaveDialog method accepts a Component reference.This must refer to the parent JFrame over which the Dialog is created.

I do not understand why is it?

There is a variable on that line with a null value causing the NPE.
If you can not see what variable has the null value, add a println() statement just before that line that prints out the values of all the variables used on that line so you can see which variable has the null value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.