Reputation: 75
import java.awt.*;
import java.awt.event.*;
public class Project_notepad
{
private Frame f; //basic frame
public MenuBar mb; //menubar declaration
private Menu File, Edit, Format,View, Findit;
private MenuItem new_file,new_window,open,save,save_as, close, cut, copy, paste, case_convert, indent,font,style,size,underline,italic,bold,finds, incremental_find,replace;
private CheckboxMenuItem status_bar,word_wrap,hide_menu_bar,full_screen;
public Project_notepad()
{
f=new Frame();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); class object for setting max screen size
f.setBounds(0,0,screenSize.width, screenSize.height);
mb=new MenuBar(); //menu bar declaration
File=new Menu("File");
Edit=new Menu("Edit");
Format=new Menu("Format");
View=new Menu("View");
Findit=new Menu("Find");
new_file=new MenuItem("new file"); //menu items declaration
new_window=new MenuItem("new window");
open=new MenuItem("open");
save=new MenuItem("save");
save_as=new MenuItem("save as");
close=new MenuItem("close");
cut=new MenuItem("cut");
copy=new MenuItem("copy");
paste=new MenuItem("paste");
case_convert=new MenuItem("case convert");
indent=new MenuItem("indent");
font=new MenuItem("font");
style=new MenuItem("style");
size=new MenuItem("size");
underline=new MenuItem("underline");
italic=new MenuItem("italic");
bold=new MenuItem("bold");
incremental_find=new MenuItem("incremental_find");
replace=new MenuItem("replace");
status_bar=new CheckboxMenuItem("status bar"); //chechkbox items declaration
word_wrap=new CheckboxMenuItem("word wrap");
hide_menu_bar=new CheckboxMenuItem("hide menu bar");
full_screen=new CheckboxMenuItem("full screen");
File.add(new_file); //adding items to menu bar
File.add(new_window);
File.add(open);
File.add(save);
File.add(save_as);
File.add(close);
Edit.add(copy);
Edit.add(paste);
Edit.add(case_convert);
Edit.add(indent);
Format.add(font);
Format.add(style);
Format.add(size);
Format.add(underline);
Format.add(italic);
Format.add(bold);
Findit.add(finds);
Findit.add(incremental_find);
Findit.add(replace);
View.add(status_bar);
View.add(word_wrap);
View.add(hide_menu_bar);
View.add(full_screen);
mb.add(File);
mb.add(Edit);
mb.add(Format);
mb.add(View);
mb.add(Findit);
f.setMenuBar(mb);
f.setVisible(true);
}
public static void main(String args[])
{
Project_notepad pr=new Project_notepad();
}
}
Actually,i am in the initial phase of developing a notepad for practice in java,but getting NullPointerException as
at java.awt.Menu.add(Menu.java:262)
at Project_notepad.<init><Project_notepad.java:65>
at Project_notepad.main<Project_notepad.java:92>
Here,project_notepad is the name of my file as well as my class. I have searched the reason of it as Attempting to invoke an instance method of a null object , Attempting to access or modify a particular field of a null object, Attempting to obtain the length of such null object as an array. But unable to link to any reason to error in my code. Require assistance. thanks in advance.
Upvotes: 1
Views: 228
Reputation: 97
You can also try the following tool if you want to do it for the entire code base: Find Bugs
Upvotes: 1
Reputation: 144
You haven't initialized the "finds" variable when you add it on line 65. Add this to your list of initializations above:
finds = new MenuItem("finds");
Also, get a better IDE. Most should tell if you haven't initialized something and you can catch it before you compile.
Upvotes: 2