2015-08-27 46 views
-3

你好這是我創造,我有這個錯誤我的第一個Java應用程序我有一種感覺,這是一個語法錯誤,但我不能找到它,即使我在C++錯誤與我的Java的記事本

/********************************** 

    Notepad Created By Anmol Brar 
    Free To Be Copied & Modified 
    www.github.com/anmol-brar 
    www.twitter.com/circlinq_anmol 

**********************************/ 


package com.anmol.notepad; 

import java.awt.Font; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.InputEvent; 
import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.FileWriter; 

import javax.swing.JColorChooser; 
import javax.swing.JFileChooser; 
import javax.swing.JFrame; 
import javax.swing.JMenu; 
import javax.swing.JMenuBar; 
import javax.swing.JMenuItem; 
import javax.swing.JOptionPane; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
import javax.swing.KeyStroke; 
import javax.swing.ScrollPaneConstants; 

public class Notepad extends JFrame implements ActionListener { 

    // DEFINING MENUBAR 
    JMenuBar mb; 

    JMenu filemenu, editmenu, formatmenu, viewmenu, helpmenu; 

    JMenuItem newitem, openitem, saveitem, saveasitem, pagesetupitem, printitem, exititem, undoitem, cutitem, copyitem, 
      pasteitem, deleteitem, finditem, findnextitem, replaceitem, gotoitem, selectallitem, timeanddateitem, 
      wordwrapitem, fontitem, statusbaritem, viewhelpitem, aboutnotepaditem; 

    // CATEGORIZING MENUBAR SHORTCUT NAMES 
    JTextArea ta; 
    JScrollPane jsp; 
    JFileChooser jfc; 
    JColorChooser jc; 

    // DEFINING FONT 
    Font f = new Font("calbri", 4, 30); 

    // CONSTRUCTOR 
    public Notepad() { 
     jfc = new JFileChooser(); 
     jc = new JColorChooser(); 
     ta = new JTextArea(); 
     ta.setFont(f); 

     // MAKING SCROLLPANES 
     int vsp = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS; 
     int hsp = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS; 

     jsp = new JScrollPane(ta, vsp, hsp); 
     add(jsp); 

     // DEFINING MENUBAR/MAKING 
     mb = new JMenuBar(); 
     setJMenuBar(mb); 

     filemenu = new JMenu("File"); 
     filemenu = new JMenu("Edit"); 
     filemenu = new JMenu("Format"); 
     filemenu = new JMenu("View"); 
     filemenu = new JMenu("Help"); 

     // CREATING ITEMS 
     mb.add(filemenu); 
     mb.add(editmenu); 
     mb.add(formatmenu); 
     mb.add(viewmenu); 
     mb.add(helpmenu); 

     newitem = new JMenuItem("New"); 
     newitem.setAccelerator(KeyStroke.getKeyStroke('N', 1)); 
     openitem = new JMenuItem("Open"); 
     saveitem = new JMenuItem("Save"); 
     saveasitem = new JMenuItem("Save This File As...."); 
     pagesetupitem = new JMenuItem("Page Setup"); 
     printitem = new JMenuItem("Print This Document(Ctrl + P)"); 
     exititem = new JMenuItem("Exit This Document/Program :("); 
     exititem.setAccelerator(KeyStroke.getKeyStroke('X', InputEvent.ALT_DOWN_MASK)); 
     undoitem = new JMenuItem("Undo"); 
     cutitem = new JMenuItem("Cut"); 
     cutitem.setAccelerator(KeyStroke.getKeyStroke('X', InputEvent.CTRL_DOWN_MASK)); 
     copyitem = new JMenuItem("Copy Text"); 
     pasteitem = new JMenuItem("Paste Text"); 
     deleteitem = new JMenuItem("Delete (Del)"); 
     finditem = new JMenuItem("Find Text/Word (Ctrl + F)"); 
     findnextitem = new JMenuItem("Find NextItem(F3)"); 
     replaceitem = new JMenuItem("Replace A Piece Of Text (Ctrl + H)"); 
     gotoitem = new JMenuItem("Goto... (Ctrl + G)"); 
     selectallitem = new JMenuItem("Select All (Ctrl + A)"); 
     timeanddateitem = new JMenuItem("Time & Date (F5)"); 
     wordwrapitem = new JMenuItem("WordWrap Item..."); 
     fontitem = new JMenuItem("Color "); 
     statusbaritem = new JMenuItem("Status Bar"); 
     viewhelpitem = new JMenuItem("View Help"); 
     aboutnotepaditem = new JMenuItem("About Notepad"); 
     filemenu.add(newitem); 
     filemenu.add(openitem); 
     filemenu.add(saveitem); 
     filemenu.add(saveasitem); 
     filemenu.addSeparator(); 
     filemenu.add(pagesetupitem); 
     filemenu.add(printitem); 
     filemenu.addSeparator(); 
     filemenu.add(exititem); 
     editmenu.add(undoitem); 
     filemenu.addSeparator(); 
     editmenu.add(cutitem); 
     editmenu.add(copyitem); 
     editmenu.add(pasteitem); 
     editmenu.add(deleteitem); 
     editmenu.addSeparator(); 
     editmenu.add(finditem); 
     editmenu.add(findnextitem); 
     editmenu.add(replaceitem); 
     editmenu.add(gotoitem); 
     editmenu.addSeparator(); 
     editmenu.add(selectallitem); 
     editmenu.add(timeanddateitem); 
     formatmenu.add(wordwrapitem); 
     formatmenu.add(fontitem); 
     viewmenu.add(statusbaritem); 
     helpmenu.add(viewhelpitem); 
     helpmenu.add(aboutnotepaditem); 

     // SETTING THE TITLE/MAKING THE WINDOW 
     setTitle("Untitled -- SwiftPad"); 
     setVisible(true); 
     setExtendedState(MAXIMIZED_BOTH); 

     newitem.addActionListener(this); 
     openitem.addActionListener(this); 
     saveitem.addActionListener(this); 
     saveasitem.addActionListener(this); 
     exititem.addActionListener(this); 
     copyitem.addActionListener(this); 
     cutitem.addActionListener(this); 
     pasteitem.addActionListener(this); 
     fontitem.addActionListener(this); 
    } 

    public void actionPerformed(ActionEvent ae) { 
     if (ae.getSource() == exititem) { 
      System.exit(0); 
     } 
     if (ae.getSource() == newitem) { 
      ta.setText(""); 
     } 
     if (ae.getSource() == copyitem) { 
      ta.copy(); 
     } 
     if (ae.getSource() == pasteitem) { 
      ta.paste(); 
     } 
     if (ae.getSource() == cutitem) { 
      ta.cut(); 
     } 
     if (ae.getSource() == openitem) { 
      String s = ta.getText(); 
      if (!(s.equalsIgnoreCase(""))) { 
       int opt = JOptionPane.showConfirmDialog(this, " Unsaved Documents! Do you want to save them? y/n? :)"); 
       if (opt == JOptionPane.YES_OPTION) { 
        jfc.showOpenDialog(this); 
        try { 
         FileWriter fw = new FileWriter(jfc.getSelectedFile()); 
         String data = ta.getText(); 
         fw.write(data + "\n"); 
         fw.close(); 
        } catch (Exception e) { 
         JOptionPane.showMessageDialog(this, "Error in Reading :(", "Error", JOptionPane.ERROR_MESSAGE); 
        } 
       } 
      } else { 
       jfc.showOpenDialog(this); 
       try { 
        FileReader fr = new FileReader(jfc.getSelectedFile()); 
        BufferedReader br = new BufferedReader(fr); 
        String data = ""; 
        ta.setText(""); 
        while ((data = br.readLine()) != null) { 
         ta.append(data + "\n"); 
        } 
        fr.close(); 
       } catch (Exception e) { 
        JOptionPane.showMessageDialog(this, "Error In Opening :(", "Error", JOptionPane.ERROR_MESSAGE); 

       } 
      } 
     } 
     if(ae.getSource() == saveitem) { 
      jfc.showSaveDialog(this); 
      try { 
       FileWriter fw = new FileWriter(jfc.getSelectedFile()); 
       String data = ta.getText(); 
       fw.write(data + "\n"); 
       fw.close(); 
      }catch (Exception e) { 

       JOptionPane.showMessageDialog(this, "Error In Saving :("); 

      } 
     } 

    } 

    // RUNNING CLASS 
    public static void main(String[] args) { 
     new Notepad(); 
    } 

} 
已編程

這是我得到的錯誤,我用java製作了遊戲,但是得到了它一次,但從來沒有,請幫助! :)

Exception in thread "main" java.lang.NullPointerException 
    at java.awt.Container.addImpl(Unknown Source) 
    at java.awt.Container.add(Unknown Source) 
    at javax.swing.JMenuBar.add(Unknown Source) 
    at com.anmol.notepad.Notepad.<init>(Notepad.java:79) 
    at com.anmol.notepad.Notepad.main(Notepad.java:221) 

謝謝,Anmol!

+3

可能重複[什麼是空指針異常,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how -do-i-fix-it) –

+0

請在代碼示例中標記第79行。 –

+0

@Simz我看了一下這個帖子之前,我做了這個我沒有得到它 –

回答

0

您使用的是類的字段沒有正確初始化它們:

filemenu = new JMenu("File"); 
    filemenu = new JMenu("Edit"); 
    filemenu = new JMenu("Format"); 
    filemenu = new JMenu("View"); 
    filemenu = new JMenu("Help"); 

    // CREATING ITEMS 
    mb.add(filemenu); 
    mb.add(editmenu); 
    mb.add(formatmenu); 
    mb.add(viewmenu); 
    mb.add(helpmenu); 

你只是初始化filemenu,但你實際使用的所有菜單領域。它應該是這樣的:

filemenu = new JMenu("File"); 
    editmenu = new JMenu("Edit"); 
    formatmenu = new JMenu("Format"); 
    viewmenu = new JMenu("View"); 
    helpmenu = new JMenu("Help"); 

    // CREATING ITEMS 
    mb.add(filemenu); 
    mb.add(editmenu); 
    mb.add(formatmenu); 
    mb.add(viewmenu); 
    mb.add(helpmenu); 

mb.add()方法拋出一個NullPointerException因爲傳遞的參數是null作爲現場未初始化。

+0

看起來它可能是複製粘貼錯誤。 – gla3dr

+0

謝謝你的男人!你解決了我的問題,所以基本上我必須告訴Java這些是對的? –

+0

@AnmolBrar確實,您需要將實例分配給字段。 –