Help with my first Java program

Discussion in 'Java' started by mohed.haidar@gmail.com, Dec 27, 2007.

  1. Guest

    Hello guys. I just wrote my first java program and i have a couple of
    questions and a problem. I would be glad if someone can give me a
    hand.

    The questions :
    1. How do i pack everything into i nice executable file for others
    to run without the need to open a cmd prompt and typing java ... and
    so on ?
    2. How do you guys do when your looking for a command. Is there some
    kind of registry online, or do you have books or do you just know it
    all from the getgo :) ?

    The problem is that, while the compailer doesn't complain, i still get
    an error at runtime. The error staits

    Exception in thread "main" java.lang.NullPointerException
    at DiveLog.DiveLog.populateMettoMenu(DiveLog.java:76)
    at DiveLog.DiveLog.<init>(DiveLog.java:38)
    at DiveLog.DiveLog.main(DiveLog.java:15)
    Press any key to continue . . .

    and i cant get the program running.

    The source looks like this

    package DiveLog;

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

    public class Metto
    {
    private JFrame mettoFrame;
    private JTabbedPane mettoPane;


    public static void main(String[] args)
    {
    Metto go = new Metto();
    }


    public Metto()
    {

    JFrame mettoFrame = new JFrame("Metto ligger långt efter");

    mettoFrame.addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    });

    mettoPane = new JTabbedPane(SwingConstants.RIGHT);
    mettoPane.setBackground(Color.blue);
    mettoPane.setForeground(Color.white);
    populateMettoPane();


    populateMettoMenu();

    mettoFrame.getContentPane().add(mettoPane);


    mettoFrame.pack();
    mettoFrame.setSize(750, 650);
    mettoFrame.setBackground(Color.white);
    mettoFrame.setVisible(true);

    }

    private void populateMettoPane()
    {
    mettoPane.addTab("Tryck här",
    null,
    new press1(),
    "Det här va inte så svårt");
    mettoPane.addTab("Eller här",
    null,
    new press2(),
    "Inte det här häller");
    }

    private void populateMettoMenu()
    {
    JMenuBar mb = new JMenuBar();
    JMenu menu = new JMenu("File");
    JMenuItem item = new JMenuItem("Exit");
    item.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    System.exit(0);
    }
    });
    menu.add(item);
    mb.add(menu);
    mettoFrame.setJMenuBar(mb);

    }

    public class press1 extends JPanel
    {
    }

    public class press2 extends JPanel
    {
    }


    }

    Thx in advance for any help you guys can give me. Mohamed Haidar.
    , Dec 27, 2007
    #1
    1. Advertising

  2. Donkey Hot Guest

    wrote in news:05e9ea93-a409-4198-8b38-13b6d587cb61
    @v4g2000hsf.googlegroups.com:

    > Hello guys. I just wrote my first java program and i have a couple of
    > questions and a problem. I would be glad if someone can give me a
    > hand.
    >
    > The questions :
    > 1. How do i pack everything into i nice executable file for others
    > to run without the need to open a cmd prompt and typing java ... and
    > so on ?
    > 2. How do you guys do when your looking for a command. Is there some
    > kind of registry online, or do you have books or do you just know it
    > all from the getgo :) ?
    >
    > The problem is that, while the compailer doesn't complain, i still get
    > an error at runtime. The error staits
    >
    > Exception in thread "main" java.lang.NullPointerException
    > at DiveLog.DiveLog.populateMettoMenu(DiveLog.java:76)
    > at DiveLog.DiveLog.<init>(DiveLog.java:38)
    > at DiveLog.DiveLog.main(DiveLog.java:15)
    > Press any key to continue . . .
    >
    > and i cant get the program running.
    >
    > The source looks like this
    >
    > package DiveLog;
    >
    > import javax.swing.*;
    > import java.awt.*;
    > import java.awt.event.*;
    >
    > public class Metto
    > {
    > private JFrame mettoFrame;
    > private JTabbedPane mettoPane;
    >
    >
    > public static void main(String[] args)
    > {
    > Metto go = new Metto();
    > }
    >
    >
    > public Metto()
    > {
    >
    > JFrame mettoFrame = new JFrame("Metto ligger långt efter");
    >
    > mettoFrame.addWindowListener(new WindowAdapter()
    > {
    > public void windowClosing(WindowEvent e)
    > {
    > System.exit(0);
    > }
    > });
    >
    > mettoPane = new JTabbedPane(SwingConstants.RIGHT);
    > mettoPane.setBackground(Color.blue);
    > mettoPane.setForeground(Color.white);
    > populateMettoPane();
    >
    >
    > populateMettoMenu();
    >
    > mettoFrame.getContentPane().add(mettoPane);
    >
    >
    > mettoFrame.pack();
    > mettoFrame.setSize(750, 650);
    > mettoFrame.setBackground(Color.white);
    > mettoFrame.setVisible(true);
    >
    > }
    >
    > private void populateMettoPane()
    > {
    > mettoPane.addTab("Tryck här",
    > null,
    > new press1(),
    > "Det här va inte så svårt");
    > mettoPane.addTab("Eller här",
    > null,
    > new press2(),
    > "Inte det här häller");
    > }
    >
    > private void populateMettoMenu()
    > {
    > JMenuBar mb = new JMenuBar();
    > JMenu menu = new JMenu("File");
    > JMenuItem item = new JMenuItem("Exit");
    > item.addActionListener(new ActionListener()
    > {
    > public void actionPerformed(ActionEvent e)
    > {
    > System.exit(0);
    > }
    > });
    > menu.add(item);
    > mb.add(menu);
    > mettoFrame.setJMenuBar(mb);
    >
    > }
    >
    > public class press1 extends JPanel
    > {
    > }
    >
    > public class press2 extends JPanel
    > {
    > }
    >
    >
    > }
    >
    > Thx in advance for any help you guys can give me. Mohamed Haidar.
    >


    You have declared variable "mettoFrame" twice, once as a property in
    class, and then a local variable in the constructor Metto(). You do not
    create the class member mettoFrame, but try to use it in
    populateMettoMenu().

    Netbeans IDE told this as soon as loaded the source in it.
    Donkey Hot, Dec 27, 2007
    #2
    1. Advertising

  3. Vince Guest

    wrote:
    > Hello guys. I just wrote my first java program and i have a couple of
    > questions and a problem. I would be glad if someone can give me a
    > hand.
    >
    > The questions :
    > 1. How do i pack everything into i nice executable file for others
    > to run without the need to open a cmd prompt and typing java ... and
    > so on ?


    Check out how t create a JAR file as example

    > 2. How do you guys do when your looking for a command. Is there some
    > kind of registry online, or do you have books or do you just know it
    > all from the getgo :) ?


    You'll find everything you need at java.sun.com.

    >
    > The problem is that, while the compailer doesn't complain, i still get
    > an error at runtime. The error staits
    >
    > Exception in thread "main" java.lang.NullPointerException
    > at DiveLog.DiveLog.populateMettoMenu(DiveLog.java:76)
    > at DiveLog.DiveLog.<init>(DiveLog.java:38)
    > at DiveLog.DiveLog.main(DiveLog.java:15)
    > Press any key to continue . . .
    >
    > and i cant get the program running.


    The compiler tells you that you've got a runtime error at line 76
    (populateMettoMenu). The NullPointer means basically that at one stage
    you try to use the content of an object that is NULL. Basically if there
    is no value to use, the program will fail... Check that your object's
    really a content and then it will work..

    >
    > The source looks like this
    >
    > package DiveLog;
    >
    > import javax.swing.*;
    > import java.awt.*;
    > import java.awt.event.*;
    >
    > public class Metto
    > {
    > private JFrame mettoFrame;
    > private JTabbedPane mettoPane;
    >
    >
    > public static void main(String[] args)
    > {
    > Metto go = new Metto();
    > }
    >
    >
    > public Metto()
    > {
    >
    > JFrame mettoFrame = new JFrame("Metto ligger långt efter");
    >
    > mettoFrame.addWindowListener(new WindowAdapter()
    > {
    > public void windowClosing(WindowEvent e)
    > {
    > System.exit(0);
    > }
    > });
    >
    > mettoPane = new JTabbedPane(SwingConstants.RIGHT);
    > mettoPane.setBackground(Color.blue);
    > mettoPane.setForeground(Color.white);
    > populateMettoPane();
    >
    >
    > populateMettoMenu();
    >
    > mettoFrame.getContentPane().add(mettoPane);
    >
    >
    > mettoFrame.pack();
    > mettoFrame.setSize(750, 650);
    > mettoFrame.setBackground(Color.white);
    > mettoFrame.setVisible(true);
    >
    > }
    >
    > private void populateMettoPane()
    > {
    > mettoPane.addTab("Tryck här",
    > null,
    > new press1(),
    > "Det här va inte så svårt");
    > mettoPane.addTab("Eller här",
    > null,
    > new press2(),
    > "Inte det här häller");
    > }
    >
    > private void populateMettoMenu()
    > {
    > JMenuBar mb = new JMenuBar();
    > JMenu menu = new JMenu("File");
    > JMenuItem item = new JMenuItem("Exit");
    > item.addActionListener(new ActionListener()
    > {
    > public void actionPerformed(ActionEvent e)
    > {
    > System.exit(0);
    > }
    > });
    > menu.add(item);
    > mb.add(menu);
    > mettoFrame.setJMenuBar(mb);
    >
    > }
    >
    > public class press1 extends JPanel
    > {
    > }
    >
    > public class press2 extends JPanel
    > {
    > }
    >
    >
    > }
    >
    > Thx in advance for any help you guys can give me. Mohamed Haidar.


    --
    Posted via a free Usenet account from http://www.teranews.com
    Vince, Dec 27, 2007
    #3
  4. Guest

    On Dec 27, 8:53 am, wrote:
    > Hello guys. I just wrote my first java program and i have a couple of
    > questions and a problem. I would be glad if someone can give me a
    > hand.
    >
    > The questions :
    >   1. How do i pack everything into i nice executable file for others
    > to run without the need to open a cmd prompt and typing java ... and
    > so on ?


    Package it in a JAR file. Learning at least the basics about JAR files
    is pretty important for Java development.
    A co-worker of mine would often use JSmooth to create an executable
    wrapper, but I think you still need to create a JAR file as the input
    for JSmooth.
    , Dec 27, 2007
    #4
  5. wrote:
    >Hello guys. I just wrote my first java program and i have a couple of
    >questions and a problem. I would be glad if someone can give me a
    >hand.
    >
    >The questions :
    > 1. How do i pack everything into i nice executable file for others
    >to run without the need to open a cmd prompt and typing java ... and
    >so on ?


    As two other people have mentioned, the first step is
    to put it in a Jar archive.

    But that is not enough in itself, to make a 'double click -
    launch' file. To provide that level of convenience, you will
    need to either..
    a) Provide a manifest file in the Jar that sepcifies the main class.
    b) Launch the app. using Java Web Start, and suggest (via
    the JNLP file) desktop shortcuts or menu items.

    >Thx in advance for any help you guys can give me. Mohamed Haidar.


    Thanks in future for taking care to spell words fully.
    That word is 'Thanks', not 'Thx' (which I think is a
    proprietary sound system/format.). Note also that
    the word 'I' should *always* be upper case - no matter
    where it occurs in a sentence.

    --
    Andrew Thompson
    http://www.physci.org/

    Message posted via http://www.javakb.com
    Andrew Thompson, Dec 27, 2007
    #5
  6. Vince Guest

    Andrew Thompson wrote:

    > Thanks in future for taking care to spell words fully.
    > That word is 'Thanks', not 'Thx' (which I think is a
    > proprietary sound system/format.). Note also that
    > the word 'I' should *always* be upper case - no matter
    > where it occurs in a sentence.


    Thanks posting comments like that in alt.english.usage :) Without any
    animosity though!

    --
    Posted via a free Usenet account from http://www.teranews.com
    Vince, Dec 27, 2007
    #6
  7. Mark Space Guest

    Vince wrote:

    >
    > Thanks posting comments like that in alt.english.usage :) Without any
    > animosity though!
    >


    I disagree. While the point about animosity is usually well taken, this
    was one of Andrews milder post. And grammar and spelling are relevant
    to c.l.j.p. It's how we all communicate -- our "protocol" if you will.
    I don't want to see this group devolve into a mess of miss-spellings,
    leet-speak, chat shorthand and general bastardization of the English
    language. The only people who get a break are non-English speakers.
    The rest should know better.
    Mark Space, Dec 27, 2007
    #7
  8. Mark Space Guest

    wrote:
    > Hello guys. I just wrote my first java program and i have a couple of
    > questions and a problem. I would be glad if someone can give me a
    > hand.


    As someone else alluded to, the real answer here is to download and use
    a decent IDE like NetBeans. While trying to do things by hand is a
    commendable learning exercise, it's not really practical. A good IDE
    will be your best friend.

    http://download.netbeans.org/netbeans/6.0/final/

    You want the download all the way on the right, under the column that
    says "All."


    > 2. How do you guys do when your looking for a command. Is there some
    > kind of registry online, or do you have books or do you just know it
    > all from the getgo :) ?


    We have it surgically implanted at birth by the Hive Over-Mind.

    Seriously, we read books and online tutorials. Online magazines and
    trade magazines are also good -- it's important to have a conduit for
    learning new things, not just learning something once and then assuming
    we know it all. (Although, the jar command is hardly new.)

    For new people, I recommend O'Reilly's book Learning Java. The latest
    edition is 3rd, I think. Learning Java explains the command line
    utilities and much more. It's a very complete introduction to the language.

    I also recommend looking a Sun's tutorials online. Sun updates those
    periodically so you are almost always looking at the best and latest
    information.

    http://java.sun.com/docs/books/tutorial/

    Also, the Javapassion web site has a free online class to take on Java.
    It's tough for a true beginner but if you are coding already I think
    you could handle it easily. The class does introduce the command line
    tools and makes heavy use of the jar command. However the class does
    use Netbeans primarily, which I think is best since a good IDE is essential.

    http://www.javapassion.com/javaintro/
    Mark Space, Dec 27, 2007
    #8
  9. Karl Guest

    <> wrote in message
    news:...
    > On Dec 27, 8:53 am, wrote:
    >> Hello guys. I just wrote my first java program and i have a couple of
    >> questions and a problem. I would be glad if someone can give me a
    >> hand.
    >>
    >> The questions :
    >> 1. How do i pack everything into i nice executable file for others
    >> to run without the need to open a cmd prompt and typing java ... and
    >> so on ?

    >
    > Package it in a JAR file. Learning at least the basics about JAR files
    > is pretty important for Java development.
    > A co-worker of mine would often use JSmooth to create an executable
    > wrapper, but I think you still need to create a JAR file as the input
    > for JSmooth.


    Yes, and if you want it to start with a double click, consider creating a
    Windows shortcut with the Java command line in it. If you want to hide the
    DOS console, use javaw.exe instead of java.exe in your command line.
    Karl, Dec 27, 2007
    #9
  10. Arne Vajhøj Guest

    Mark Space wrote:
    > As someone else alluded to, the real answer here is to download and use
    > a decent IDE like NetBeans. While trying to do things by hand is a
    > commendable learning exercise, it's not really practical. A good IDE
    > will be your best friend.


    It is much easier using an IDE. But it is part of understanding
    Java development to understand what the IDE does behind the scene.
    And that is hard to learn without having to work with command line
    tools, directories and classpath etc..

    Arne
    Arne Vajhøj, Dec 27, 2007
    #10
  11. Arne Vajhøj wrote:
    >> As someone else alluded to, the real answer here is to download and use
    >> a decent IDE like NetBeans. While trying to do things by hand is a
    >> commendable learning exercise, it's not really practical. A good IDE
    >> will be your best friend.

    >
    >It is much easier using an IDE. But it is part of understanding
    >Java development to understand what the IDE does behind the scene.


    This is where Ant comes into its own. The Ant tasks
    related to Java development are very easy to map between
    command line arguments and task options.

    While Ant build files can be run from the command line,
    sans IDE, most IDEs can also load and work with (invoke
    etc.) Ant scripts.

    >And that is hard to learn without having to work with command line
    >tools, directories and classpath etc..


    I think it is important for developers to understand this
    type of stuff. Many questions that start with "How do I
    make my IDE...?" would already be answered if the
    questioner had better knowledge of the underlying
    SDK tools.

    --
    Andrew Thompson
    http://www.physci.org/

    Message posted via JavaKB.com
    http://www.javakb.com/Uwe/Forums.aspx/java-general/200712/1
    Andrew Thompson, Dec 28, 2007
    #11
  12. Vince Guest

    Mark Space wrote:
    > Vince wrote:
    >
    >>
    >> Thanks posting comments like that in alt.english.usage :) Without any
    >> animosity though!
    >>

    >
    > I disagree. While the point about animosity is usually well taken, this
    > was one of Andrews milder post. And grammar and spelling are relevant
    > to c.l.j.p. It's how we all communicate -- our "protocol" if you will.
    > I don't want to see this group devolve into a mess of miss-spellings,
    > leet-speak, chat shorthand and general bastardization of the English
    > language.


    I fully agree with you on this one...


    > The only people who get a break are non-English speakers. The
    > rest should know better.


    That's the point, have you seen the name of the original poster. Mohed
    Haidar, most probably a non native English speaker that - using your
    words - should be allowed to get a break...

    --
    Posted via a free Usenet account from http://www.teranews.com
    Vince, Dec 28, 2007
    #12
  13. Vince wrote:
    >>> Thanks posting comments like that in alt.english.usage :) Without any
    >>> animosity though!


    There was none intended.

    >That's the point, have you seen the name of the original poster.


    Yes, I did. ..

    >..Mohed Haidar, ..


    .. I also noted that the OP capitalised the instances
    of the word 'I' that led sentences, but not other
    occurrences. That in turn, led to to suspect that
    while they were not entirely familiar with English, they
    were doing their best to communicate their message.
    As such, I felt they might like that tip.

    >... most probably a non native English speaker that - using your
    >words ..


    'Thx' is not English. Other people that are (perhaps like the
    OP) not native speakers, would have more trouble understanding
    what leet speak words like 'thx' mean, than the properly spelt
    'thanks'. If you pop over to Google* and do a search on thx,
    what is the first thing that comes up? (hint: I alluded to it in
    my initial post).

    * <http://www.google.com/search?as_q=thx>

    > ..- should be allowed to get a break...


    And on that subject, how about *you* give posters a 'break'
    in future, by (politely) giving them tips that will help them
    to communicate more effectively on this (English based) forum?

    It is all in your perspective. You seem to feel I was somehow
    'rude' in my initial comments.

    I suggest you deal with that, because I feel it is entirely
    appropriate and helpful to discuss the best ways to
    communicate, and I see no advantage to anybody, in
    altering my response to these situations.

    --
    Andrew Thompson
    http://www.physci.org/

    Message posted via JavaKB.com
    http://www.javakb.com/Uwe/Forums.aspx/java-general/200712/1
    Andrew Thompson, Dec 28, 2007
    #13
  14. Vince Guest

    Andrew Thompson wrote:
    > Vince wrote:
    >>>> Thanks posting comments like that in alt.english.usage :) Without any
    >>>> animosity though!

    >
    > There was none intended.
    >


    I meant my comment was without animosity, sorry for the misunderstanding...

    > It is all in your perspective. You seem to feel I was somehow
    > 'rude' in my initial comments.


    No I didn't percept your comment as rude, I was just following your
    postings - with great respect to your constructive contributions - and
    realized that - by guess - you mention the capital 'I' every 3rd time...
    Thus I thought you may be interested that there is an interesting group
    focusing on things like that...

    > and I see no advantage to anybody, in
    > altering my response to these situations.


    Me neither, except that it looks like Don Quichote fighting against mills...

    Anyway, no big deal about that.. peace...

    --
    Posted via a free Usenet account from http://www.teranews.com
    Vince, Dec 28, 2007
    #14
  15. Vince wrote:
    >>>>> Thanks posting comments like that in alt.english.usage :) Without any
    >>>>> animosity though!

    >>
    >> There was none intended.

    >
    >I meant my comment was without animosity, sorry for the misunderstanding...


    After all this discussion about 'clear and effective
    communication' I have to concede that I read too
    much into your initial (clear) words. My bad.
    ...
    >> ...I see no advantage to anybody, in
    >> altering my response to these situations.

    >
    >Me neither, except that it looks like Don Quichote fighting against mills...


    ;-) Perhaps. But 'different folks/different strokes/
    different approaches'. In a vibrant community (with
    lots of different opinions and approaches), almost
    anything is good.

    >Anyway, no big deal about that.. peace...


    :)

    --
    Andrew Thompson
    http://www.physci.org/

    Message posted via JavaKB.com
    http://www.javakb.com/Uwe/Forums.aspx/java-general/200712/1
    Andrew Thompson, Dec 28, 2007
    #15
  16. Guest

    On Dec 27, 2:19 pm, Mark Space <> wrote:
    > Vince wrote:
    >
    > > Thanks posting comments like that in alt.english.usage :) Without any
    > > animosity though!

    >
    > I disagree. While the point about animosity is usually well taken, this
    > was one of Andrews milder post. And grammar and spelling are relevant
    > to c.l.j.p. It's how we all communicate -- our "protocol" if you will.
    > I don't want to see this group devolve into a mess of miss-spellings,
    > leet-speak, chat shorthand and general bastardization of the English
    > language. The only people who get a break are non-English speakers.
    > The rest should know better.


    Who died and left you king?
    , Dec 28, 2007
    #16
  17. Tim Smith Guest

    In article <x5Tcj.32721$>,
    Mark Space <> wrote:
    > As someone else alluded to, the real answer here is to download and use
    > a decent IDE like NetBeans. While trying to do things by hand is a
    > commendable learning exercise, it's not really practical. A good IDE
    > will be your best friend.


    Typing "javac Foo.java" to compile, and "java Foo" to run a program is
    not practical?

    --
    --Tim Smith
    Tim Smith, Dec 28, 2007
    #17
  18. Roedy Green Guest

    On Thu, 27 Dec 2007 05:53:03 -0800 (PST),
    wrote, quoted or indirectly quoted someone who said :

    > 1. How do i pack everything into i nice executable file for others
    >to run without the need to open a cmd prompt and typing java ... and
    >so on ?


    1. give them a jar. See http://mindprod.com/jgloss/jar.html

    2. natively compile. see
    http://mindprod.com/jgloss/nativecompiler.html

    3. use an installer. see
    http://mindprod.com/jgloss/installer.html
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Dec 28, 2007
    #18
  19. Roedy Green Guest

    On Thu, 27 Dec 2007 05:53:03 -0800 (PST),
    wrote, quoted or indirectly quoted someone who said :

    > 2. How do you guys do when your looking for a command. Is there some
    >kind of registry online, or do you have books or do you just know it
    >all from the getgo :) ?


    I use Copernic to search my local hard disk source and JavaDoc.

    see http://mindprod.com/jgloss/copernic.html

    Also check out http://mindprod.com/jgloss/jgloss.html

    You can look up words and follow links. The entries are often linked
    to even more detailed info. My essays are aimed at the newbie.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Dec 28, 2007
    #19
  20. Mark Space Guest

    Tim Smith wrote:

    > Typing "javac Foo.java" to compile, and "java Foo" to run a program is
    > not practical?


    No. Not even slightly. Ant is a big step up from typing javac each
    time you want to compile something, but a good IDE does far more than that.
    Mark Space, Dec 28, 2007
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rey
    Replies:
    4
    Views:
    10,915
    Roedy Green
    Dec 12, 2003
  2. matt
    Replies:
    9
    Views:
    405
    Andrew Thompson
    Oct 27, 2004
  3. Replies:
    1
    Views:
    507
    Andrew Thompson
    Oct 31, 2005
  4. joshivaibhav
    Replies:
    3
    Views:
    621
    joshivaibhav
    Nov 21, 2006
  5. Christopher M. Lusardi
    Replies:
    3
    Views:
    181
    Alan J. Flavell
    Oct 19, 2003
Loading...

Share This Page