Results 1 to 8 of 8

Thread: Help for Java Application

  1. #1

    Help for Java Application

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    class MainProg
    {
    static JMenuBar mb;
    static JMenu mnMedia,mnQuit;
    static JMenuItem miAdvt,miPress,miOther,miClose;
    static JMenuItem miAdd,miChange,miDel,miView;
    public MainProg()
    {
    mnMedia = new JMenu("Media");
    mnMedia.setMnemonic('M');
    mnQuit = new JMenu("Quit");
    mnQuit.setMnemonic('Q');

    miAdvt = new JMenuItem("Ad Media");

    miPress = new JMenuItem("Print Media");
    miOther = new JMenuItem("Others");
    miClose = new JMenuItem("Close");
    menuListener mnl = new menuListener();
    miAdvt.addActionListener(mnl);
    miPress.addActionListener(mnl);
    miOther.addActionListener(mnl);
    miClose.addActionListener(mnl);
    mnMedia.add(miAdvt);
    mnMedia.add(miPress);
    mnMedia.add(miOther);
    mb.add(mnMedia);


    }

    public class menuListener implements ActionListener
    {
    public void actionPerformed (ActionEvent e)
    {
    String item =e.getActionCommand();
    if(item.equals("Close"))
    {
    System.exit (0);
    }
    else
    {
    if(item.equals("Ad Media"))
    {
    System.out.println("Ad Media Selected ....");
    }
    }
    }
    }


    public static void main(String args[])
    {
    System.out.println("Starting MainProg...");
    MainProg mp = new MainProg();
    JFrame frame = new JFrame("MediaSoft");
    frame.setJMenuBar(mb);
    frame.setSize(400,400);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
    }
    +++++++++++++++++++++++++++++++++
    The above source compiles but when executed gives a NullPointerException????

  2. #2
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,401
    Always check if the creation of an object actually succeeds before using it. One of the new objects you've created probably fails for some reason.
    Oliver's Law:
    Experience is something you don't get until just after you need it.

  3. #3
    Senior Member
    Join Date
    Jul 2002
    Posts
    339
    Shouldn't you instantiate the menubar first
    mb = new JMenuBar();
    before adding menu to it
    mb.add(mnMedia);
    ?

    I wonder why the compiler didn't catch it. Or is it because mb is static? Not sure, haven't coding in Java since 1999...

    Peace always,
    <jdenny>
    Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
    I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds


  4. #4
    Senior Member
    Join Date
    Feb 2003
    Posts
    109
    You should be getting a compile error, but "mb" is out of scope in main() and will be considered a null pointer at runtime. If you really want to do it the way you have set up, you have to extend JMenuBar in MainProg and then add it to the new JFrame.
    $person!=$kiddie or die(\"Alas, die you hotmail hacker!!\");
    SecureVision

  5. #5
    Senior Member
    Join Date
    Jan 2002
    Posts
    1,207
    Originally posted here by SirDice
    Always check if the creation of an object actually succeeds before using it. One of the new objects you've created probably fails for some reason.
    No, this is unnecessary.

    Only in programming languages which lack exception handling should you need to do this.

    In Java, in an object instantiation fails, the constructor should throw an exception. In this case, the calling routine will call its exception handler or return to its caller, not try and use the object.

    Slarty

  6. #6
    str34m3r
    Guest
    I'm going to have to go with jdenny on this one. You never instantiated mb and inside the contructor you have the line :

    mb.add(bleh)

    That's most likely where your null exception is coming from.

  7. #7
    Junior Member
    Join Date
    Feb 2003
    Posts
    21
    Hopefully this post isn't too late to be of help.

    JDenny is exactly correct, simply adding the line mb = new JMenuBar(); inside the constructor for MainProg prior to mb.add(mnMedia); will allow you're program to run, however there are a few other things that seem to be amiss as well as some more general Java programming practices that I would like to mention.

    There are a number of JMenuItems and one JMenu that have been declared but aren't actually being used including the the JMenuItem miClose and what I would assume is its corresponding JMenu mnQuit which you already have handling for. I'll assume that this is merely the result of seeing you're program half-written and that this will be remedied latter on.

    I'm unsure why you have declared your JMenuBar, JMenus, and JMenuItemms as static. This isn’t appropriate. They should, however, be declared private. Also, inner classes for event handling should generally not be declared public.

    Those little bits of nitpicking aside, the way in which you wrote this program is not the preferable method for using a JFrame as the base for a program. The preferable practice is to have you're class extend JFrame itself. I rewrote you're program in the prefered style. I also added miClose to mnQuit and mnQuit to the JMenuBar. The other JMenuItems I did not add because I do not know where you intend them to go and they have no handling as of yet.

    BTW, using a more easily readable brace style/white space convension will make you're programs easier to debug when you get to writing longer ones. (BSD style is the only way to go IMO)

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

    public class MainProg2 extends JFrame
    {
    private JMenuBar mb;
    private JMenu mnMedia, mnQuit;
    private JMenuItem miAdvt, miPress, miOther, miClose;
    private JMenuItem miAdd, miChange, miDel, miView;

    public MainProg2(String title)
    {
    super(title);
    mb = new JMenuBar();
    setJMenuBar(mb);
    menuListener mnl = new menuListener();
    mnMedia = new JMenu("Media");
    mnMedia.setMnemonic('M');
    mnQuit = new JMenu("Quit");
    mnQuit.setMnemonic('Q');
    miAdvt = new JMenuItem("Ad Media");
    miPress = new JMenuItem("Print Media");
    miOther = new JMenuItem("Others");
    miClose = new JMenuItem("Close");
    miAdvt.addActionListener(mnl);
    miPress.addActionListener(mnl);
    miOther.addActionListener(mnl);
    miClose.addActionListener(mnl);
    mnQuit.add(miClose);
    mnMedia.add(miAdvt);
    mnMedia.add(miPress);
    mnMedia.add(miOther);
    mb.add(mnMedia);
    mb.add(mnQuit);
    }
    class menuListener implements ActionListener
    {
    public void actionPerformed (ActionEvent e)
    {
    String item = e.getActionCommand();
    if(item.equals("Close"))
    {
    System.exit (0);
    }
    else
    {
    if(item.equals("Ad Media"))
    {
    System.out.println("Ad Media Selected ....");
    }
    }
    }
    }
    public static void main(String [] args)
    {
    System.out.println("Starting MainProg...");
    MainProg2 frame = new MainProg2("MainProg2");
    frame.setSize(400,400);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    }

  8. #8
    Senior Member
    Join Date
    Jul 2002
    Posts
    339
    AviS187, that's what's the CODE tags are for. To insert monospace text (and preserve spacing), simply enclose your code with a pair of [ CODE ] and [ / CODE ] tags (without the spaces), or click the # button (below the font selection) when you're creating/editing your post.

    Peace always,
    <jdenny>

    PS: Yeah, I saw that thirdeyeblind declared some other objects but never used them. And the static declaration did catch my eyes. But s/he only asked about the null pointer exception in his Java program (which obviously will also happen in C and C++). In Java, the program will immediately exit with errors, but in C, you never know what will happen...
    Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
    I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •