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

Thread: how to remove NullPointerException

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    10
    My Mood
    Inspired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Cool how to remove NullPointerException

    import java.awt.*;
    import java.awt.event.*;
    //import java.Color;
    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();			//Dimension 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();
    	}
     
     
     
    }


    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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to remove NullPointerException

    NullPointerException as

    at java.awt.Menu.add(Menu.java:262)
    at Project_notepad.<init><Project_notepad.java:65>
    There is a variable with a null value on line 65. Look at line 65 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
    If you can not tell which variable it is, add a println just before line 65 and print out the values of all the variables on that line.

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: how to remove NullPointerException

    Another reason you could be getting a null pointer that you did not list is "You are passing a null object into a function that is expecting a non null object." and this is what you are doing. Like Norm said, go look at line 65 and see what object is null and backtrack to see why it is null.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  4. The Following User Says Thank You to Chris.Brown.SPE For This Useful Post:

    saurabh_kabra (March 29th, 2013)

  5. #4
    Junior Member
    Join Date
    Mar 2013
    Posts
    10
    My Mood
    Inspired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: how to remove NullPointerException

    i got the error..i have not declared finds,that's why null is used there.
    thanks everyone for their response.

Similar Threads

  1. How can I remove a element from an array?
    By seaofFire in forum Java Theory & Questions
    Replies: 3
    Last Post: April 23rd, 2012, 03:42 PM
  2. Adding add/remove button to add/remove tab
    By JMtrasfiero in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 27th, 2012, 11:24 AM
  3. How to remove the last comma
    By fride360 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 29th, 2011, 07:20 AM
  4. not able to remove from ArrayList
    By harsha_c in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 3rd, 2011, 03:28 AM
  5. How to remove letters
    By noobish in forum Java Theory & Questions
    Replies: 13
    Last Post: October 3rd, 2009, 10:36 PM

Tags for this Thread