I just finished this project, thought I had it done, but on line 1, it's telling me, "incorrect Package". What am I missing here?

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

public class JavaNotepad extends JFrame implements ActionListener, KeyListener
{
    boolean txtChanged = false;
    String fname = "";
    JMenuBar mbar;
    JMenu mnuFile, mnuEdit, mnuHelp;
    JMenuItem fileNew, fileOpen, fileSave, fileExit;
    JMenuItem editCut, editCopy, editPaste, editSelectAll, editDel;

    ImageIcon iconNew, iconOpen, iconSave;
    ImageIcon iconCut, iconCopy, iconPaste;
    JButton bttnNew, bttnOpen, bttnSave;
    JButton bttnCut, bttnCopy, bttnPaste;

    JTextArea txtPad;
    Container c;

    JavaNotepad()
    {
        initComponents();
        setTitle("Java Notepad");
        setSize(500,550);
        setVisible(true);

        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WinHandler());
    }

    //reference content pane
    void initComponents()
    {
        c = getContentPane();
        //set layout manager
        c.setLayout(new BorderLayout());

        initMenu();

        //create textarea
        txtPad = new JTextArea();
        Font f = new Font("Serif", Font.PLAIN, 12);
        txtPad.setFont(f);
        txtPad.addKeyListener(this);

        //add txtPad in scrollpane
        JScrollPane jscroll = new JScrollPane(txtPad);
        //add scrollpane in window
        c.add(jscroll, BorderLayout.CENTER);

    }

    void initMenu()
    {
        //create menubar
        mbar = new JMenuBar();

        //create menus
        mnuFile = new JMenu("File");
        mnuEdit = new JMenu("Edit");

        //create menuitems
        fileNew = new JMenuItem("New");
        fileOpen= new JMenuItem("Open");
        fileSave= new JMenuItem("Save");
        fileExit = new JMenuItem("Exit");

        editCut = new JMenuItem("Cut");
        editCopy= new JMenuItem("Copy");
        editPaste = new JMenuItem("Paste");
        editSelectAll = new JMenuItem("Select All");

        //add file menuitems
        mnuFile.add(fileNew);
        mnuFile.add(fileOpen);
        mnuFile.add(fileSave);
        mnuFile.add(fileExit);
        //add edit menuitems
        mnuEdit.add(editCut);
        mnuEdit.add(editCopy);
        mnuEdit.add(editPaste);
        mnuEdit.addSeparator();
        mnuEdit.add(editSelectAll);

        //attach menus to menubar
        mbar.add(mnuFile);
        mbar.add(mnuEdit);
        mbar.add(mnuHelp);
        //attach menubar to window
        setJMenuBar(mbar);

        //attach actionlistener to file menuitems
        fileNew.addActionListener(this);
        fileOpen.addActionListener(this);
        fileSave.addActionListener(this);
        fileExit.addActionListener(this);
        //attach actionlistener to edit menuitems
        editCut.addActionListener(this);
        editCopy.addActionListener(this);
        editPaste.addActionListener(this);
        editSelectAll.addActionListener(this);
    }

    //establish menu actions
    public void actionPerformed(ActionEvent e)
    {
        Object src = e.getSource();
        if(src.equals(fileNew))
        {
            newFile();
        }
        else if(src.equals(fileOpen))
        {
            openFile();
        }
        else if(src.equals(fileSave))
        {
            saveFile();
        }
        else if(src.equals(fileExit))
        {
            exitFile();
        }
        else if(src.equals(editCut))
        {
            txtPad.cut();
        }
        else if(src.equals(editCopy))
        {
            txtPad.copy();
        }
        else if(src.equals(editPaste))
        {
            txtPad.paste();
        }
        else if(src.equals(editSelectAll))
        {
            txtPad.selectAll();
        }
    }

    //new file process
    void newFile()
    {
        if(txtChanged == true)
        {
            int res;
            res = JOptionPane.showConfirmDialog
            (
                this, //parent
                "Do you wish to save changes?",
                "File New",
                JOptionPane.YES_NO_CANCEL_OPTION
            );
            if(res == JOptionPane.YES_OPTION)
            {
                saveFile();
            }
            else if(res == JOptionPane.CANCEL_OPTION)
            {
                return;
            }
        }

        fname = "";
        txtChanged = false;
        txtPad.setText("");
    }

    //save process
    void saveFile()
    {
        if(fname.equals(""))
        {
            JFileChooser jfc = new JFileChooser();
            int res;
            res = jfc.showSaveDialog(this);
                if(res == jfc.APPROVE_OPTION)
                {
                    fname = jfc.getSelectedFile().getAbsolutePath();
                }
                else
                {
                    return;
                }
        }

        try
        {
            FileWriter fw = new FileWriter(fname);
            fw.write(txtPad.getText());
            fw.flush();
            fw.close();
            txtChanged = false;
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog
            (
                this,
                ex.getMessage(),
                "Error Saving",
                JOptionPane.ERROR_MESSAGE
            );
        }

    }

    //open process
    void openFile()
    {
        int res;
        JFileChooser jfc = new JFileChooser("c:/");
        res = jfc.showOpenDialog(this);//parent window
        if(res == jfc.APPROVE_OPTION)
        {
            fname = jfc.getSelectedFile().getAbsolutePath();

            try
            {
                FileReader fr = new FileReader(fname);
                BufferedReader br = new BufferedReader(fr);

                //clear existing content of text area
                txtPad.setText("");
                String s;
            while( (s = br.readLine()) != null )
            {
                txtPad.append(s);
                txtPad.append("\n");
            }
            br.close();
            fr.close();
            }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog
        (
        this,
        ex.getMessage(),
        "Error Opening",
        JOptionPane.ERROR_MESSAGE //icon
        );
        }

        }
    }

    //exit process
    void exitFile()
    {
    if(txtChanged == true)
    {
        int res;
        res = JOptionPane.showConfirmDialog
        (
            this,
            "Do you wish to save changes?",
            "File Exit",
            JOptionPane.YES_NO_CANCEL_OPTION
        );
        if(res == JOptionPane.YES_OPTION)
        {
            saveFile();
        }
        else if(res == JOptionPane.CANCEL_OPTION)
        {
            return;
        }
    }
    dispose();
    }

    public void keyPressed(KeyEvent e)
    {

    }

    public void keyReleased(KeyEvent e)
    {

    }

    public void keyTyped(KeyEvent e)
    {
        txtChanged = true;
    }

    class WinHandler extends WindowAdapter
    {
        public void windowClosing(WindowEvent e)
        {
            exitFile();
        }
    }
    
    public static void main(String args[])
    {
        JavaNotepad mp = new JavaNotepad();
    }
}

Recommended Answers

All 4 Replies

What is the exact error message? Is it a compilation error? Or a runtime exception you are encountering?

Take a look at line 91:

mbar.add(mnuHelp);

mnuHelp has NOT been initialized, so you will get a Null Pointer Exception. To rectify this, initialize mnuHelp where you initialize the other two menus:

mnuFile = new JMenu("File");
mnuEdit = new JMenu("Edit");
mnuHelp = new JMenu("Help");

A null pointer exception means that you are trying to access/call a method on an object that is set to, or has a value of NULL. In this case, mnuHelp.

This will help :)

Yes, thank you! I meant to comment that out so I wouldn't forget to add it after I ensured that the rest of it was working correctly.

If you're still having trouble with the original problem, it would be because you don't specify the package. If you are using an IDE such as Eclipse or NetBeans, each source file exists in a specific directory, or package. (This is true even if not using netbeans or eclipse)
Example:

// Source file exists in the package:
package apps.ntpd;

Basically it's asking for the location of the file in the java directory system. (I'm VERY sorry if this didn't make sense.)

Also, if using Netbeans or Eclipse, the directory tree shows you which package your source file is in.

Hope this helps! =)

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.