Jump to content

Java Run-Error in MenuBar Program

- - - - -

  • Please log in to reply
12 replies to this topic

#1
dharmil007

dharmil007

    Newbie

  • Members
  • PipPip
  • 16 posts
i Have made this Program in Java.
The Programe runs Perfectly.
iT Gets Compiled too.
But whn running it using:
appletviewer <programme name>.java
it Gives an error:
NullPointerException

Program :-
//WAP to display a menu

/*<applet code = "Menus1" height = 250 width = 250></applet>*/

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

class Menus2 extends Frame implements ActionListener, ItemListener

{

	

	//Menu f,e,al;

	MenuBar mb = new MenuBar ();

	MenuItem i1,i2,i3,i4,i5,i6,i7,i8,i9,i13,i14,i15;

	CheckboxMenuItem i10,i11,i12;

	TextField t1,t2;

	String p = "You Selected";

	String msg = "";

	String a;

	Scrollbar HS,VS;

	public Menus2 (String title)

	{

		super (title);

		setLayout (new FlowLayout ());

		

		Menu f = new Menu ("File");

		Menu e = new Menu ("Edit");

		Menu al = new Menu ("Format");

		i1 = new MenuItem ("New");

		i3 = new MenuItem ("Open");

		i3 = new MenuItem ("Save");

		i4 = new MenuItem ("-");

		i5 = new MenuItem ("Quit");

		i6 = new MenuItem ("Cut");

		i7 = new MenuItem ("Copy");

		i8 = new MenuItem ("Paste");

		i9 = new MenuItem ("-");

		i10 = new CheckboxMenuItem ("Bold");

		i11 = new CheckboxMenuItem ("Italic");

		i12 = new CheckboxMenuItem ("Plain");

		i13 = new MenuItem ("-");

		i14 = new MenuItem ("VerticalScrollBar");

		i15 = new MenuItem ("HorizontalScrollBar");

		t1 = new TextField (35);

		t2 = new TextField (35);

		mb.add (f);

		f.add (i1);

		f.add (i2);

		f.add (i3);

		f.add (i4);

		f.add (i5);

		mb.add (e);

		e.add (i6);

		e.add (i7);

		e.add (i8);

		e.add (i9);

		e.add (al);

		al.add (i10);

		al.add (i11);

		al.add (i12);

		e.add (i13);

		e.add (i14);

		e.add (i15);

		add (t1);

		add (t2);

		i1.addActionListener (this);

		i2.addActionListener (this);

		i3.addActionListener (this);

		i5.addActionListener (this);

		i6.addActionListener (this);

		i7.addActionListener (this);

		i8.addActionListener (this);

		i14.addActionListener(this);

		i15.addActionListener (this);

		i10.addItemListener (this);

		i11.addItemListener (this);

		i12.addItemListener (this);

		mWindow mw = new mWindow (this);

		addWindowListener (mw);

	}

	public void actionPerformed (ActionEvent e)

	{

		String s = e.getActionCommand ();

		if (s.equals(i1))

		{

			mDialog md = new mDialog (this, "newDialogbox",true);

			md.setVisible (true);

		}

		else if (s.equals (i2))

		{

			msg=p+ " Open ";

		}

		else if (s.equals (i3))

		{

			msg=p+ " Save ";

		}

		else if (s.equals (i5))

		{

			System.exit(0);

		}

		else if (s.equals (i6))

		{

			msg = "";

		}

		else if (s.equals (i7))

		{

			t1.setText (msg);

		}

		else if (s.equals (i8))

		{

			t2.setText (t1.getText());

		}

		else if (s.equals (i14))

		{

			Scrollbar HS;

			HS = new Scrollbar (Scrollbar.HORIZONTAL);

			add (HS);

		}

		else if (s.equals (i15))

		{

			Scrollbar VS;

			VS = new Scrollbar (Scrollbar.VERTICAL);

			add (VS);

		}

	}

	public void itemStateChanged (ItemEvent e)

	{

		if (e.getSource() == i6)

		{

			t1.setText (msg);

			Font f1 = new Font ("Georgia",Font.BOLD,15);

			setFont (f1);

		}

		else if (e.getSource() == i7)

		{

			t1.setText (msg);

			Font f2 = new Font ("Georgia",Font.ITALIC,15);

			setFont (f2);

		}

		else if (e.getSource() == i8)

		{

			t1.setText (msg);

			Font f3 = new Font ("Georgia",Font.PLAIN,15);

			setFont (f3);

		}

	}

	class mWindow extends WindowAdapter

	{

		Menus2 m2;

		public mWindow (Menus2 m5)

		{

			m2 = m5;

		}

		public void windowClosing (WindowEvent e)

		{

			System.exit (0);

		}

	}

	class mDialog extends Dialog implements ActionListener

	{

		mDialog (Frame p,String q, boolean state)

		{

			super (p,q,state);

			setLayout (new FlowLayout ());

			setSize (300,100);

			add (new Label ("This is a Black Document"));

			add (new Label ("u Clicked on New"));

			Button b5 = new Button ("ok");

		}

		public void actionPerformed (ActionEvent e)

		{

			dispose ();

		}

	}

	public class Menus1 extends Applet

	{

		Frame f;

		public void init ()

		{

			f = new Menus2 ("FrameDemo");

			f.setSize (500,500);

			f.setTitle ("MENU");

			f.setVisible (true);

			f.setMenuBar (mb);

		}

		public void start ()

		{

			f.setVisible (true);

		}

		public void stop ()

		{

			f.setVisible (false);

		}

	}

}


#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Next time post the line number where the error occurred.
You declared i2, did you ever initialize it?

#3
dharmil007

dharmil007

    Newbie

  • Members
  • PipPip
  • 16 posts
u Tell me how do i mention a Line no. in This type of Error
Posted Image

#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
The only problem I can see is that you declared a variable called "i2" but never initialized it to anything.

#5
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts

dharmil007 said:

u Tell me how do i mention a Line no. in This type of Error
Sure thing, this is simply an issue of learning how to read a stack trace. It's important to learn to read these as you will most likely encounter a lot of them in your programming career. :)

The first thing to look for is any bottom level or custom package level classes in the stack, or more bluntly, a class that you wrote. Here's the only two frames on the stack that you programmed:
Posted Image
The top one is ran in the Menu2 object's constructor, line 45 of Menus1.java. That is this line of code:
        f.add (i2); 
The real problem in your code is that you accidentally assigned i3 twice instead of assigning i2 and then i3, see here:
        i3 = new MenuItem ("Open"); 

        i3 = new MenuItem ("Save"); 
But this stack trace would be crucial to determine what had happened, and it should get you to look at your f and i2 initialization.
Wow I changed my sig!

#6
dharmil007

dharmil007

    Newbie

  • Members
  • PipPip
  • 16 posts

ZekeDragon said:

Sure thing, this is simply an issue of learning how to read a stack trace. It's important to learn to read these as you will most likely encounter a lot of them in your programming career. :)

The first thing to look for is any bottom level or custom package level classes in the stack, or more bluntly, a class that you wrote. Here's the only two frames on the stack that you programmed:
Posted Image
The top one is ran in the Menu2 object's constructor, line 45 of Menus1.java. That is this line of code:
        f.add (i2); 
The real problem in your code is that you accidentally assigned i3 twice instead of assigning i2 and then i3, see here:
        i3 = new MenuItem ("Open"); 

        i3 = new MenuItem ("Save"); 
But this stack trace would be crucial to determine what had happened, and it should get you to look at your f and i2 initialization.


ohh Man !! Thanks a Lot.
i Dint knew This.
i Learned SOmething
Thanku Very Much. :c-thumbup: :c-thumbup:

#7
dharmil007

dharmil007

    Newbie

  • Members
  • PipPip
  • 16 posts
Hey thanks to all of u.
Now my Programm works, but there is only one problem.
Here is the snippet of the above code which doesnt work :


else if (e.getSource () == i14)

		{

			

			HS = new Scrollbar (Scrollbar.HORIZONTAL);

			add (HS);

		}

		else if (e.getSource () == i15)

		{

			

			VS = new Scrollbar (Scrollbar.VERTICAL);

			add (VS);

		}


The code compiles Succesfully but when click on this item, Nothing Happens

Edited by dharmil007, 15 February 2011 - 12:34 AM.


#8
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I think it DOES work. But the window doesn't get repainted.
Test this by resizing the window, this action will cause the frame to get repainted.

Normally this would be fixed by adding "repaint();" after you have added the scrollbars... But unfortunately this isn't working. Don't know why tho. :(

#9
dharmil007

dharmil007

    Newbie

  • Members
  • PipPip
  • 16 posts
Can anyOne giveme A Solution PlS. ????

#10
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
try calling validate() instead of repaint();

#11
dharmil007

dharmil007

    Newbie

  • Members
  • PipPip
  • 16 posts

lethalwire said:

try calling validate() instead of repaint();

Hey Thanks Man.
This solved the whole error now its perfectly Working.
CAn u PlS. tellme what is validate() & repaint ()

#12
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Haha, oh no. I was looking for "revalidate()" method as i knew that's the trick for JPanels, I just couldn't find it for a Frame :D




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users