Help with my first Java program

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

  1. mohed.haidar

    mohed.haidar 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

    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(
    at DiveLog.DiveLog.<init>(
    at DiveLog.DiveLog.main(
    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)

    mettoPane = new JTabbedPane(SwingConstants.RIGHT);



    mettoFrame.setSize(750, 650);


    private void populateMettoPane()
    mettoPane.addTab("Tryck här",
    new press1(),
    "Det här va inte så svårt");
    mettoPane.addTab("Eller här",
    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)


    public class press1 extends JPanel

    public class press2 extends JPanel


    Thx in advance for any help you guys can give me. Mohamed Haidar.
    mohed.haidar, Dec 27, 2007
    1. Advertisements

  2. mohed.haidar

    Donkey Hot Guest

    wrote in
    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

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

  3. mohed.haidar

    Vince Guest

    Check out how t create a JAR file as example
    You'll find everything you need at
    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..
    Vince, Dec 27, 2007
  4. mohed.haidar

    lord.zoltar Guest

    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.
    lord.zoltar, Dec 27, 2007
  5. 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.
    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, Dec 27, 2007
  6. mohed.haidar

    Vince Guest

    Thanks posting comments like that in alt.english.usage :) Without any
    animosity though!
    Vince, Dec 27, 2007
  7. mohed.haidar

    Mark Space Guest

    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
  8. mohed.haidar

    Mark Space Guest

    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.

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

    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

    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.
    Mark Space, Dec 27, 2007
  9. mohed.haidar

    Karl Guest

    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
  10. mohed.haidar

    Arne Vajhøj Guest

    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 Vajhøj, Dec 27, 2007
  11. 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.
    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

    Message posted via
    Andrew Thompson, Dec 28, 2007
  12. mohed.haidar

    Vince Guest

    I fully agree with you on this one...

    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...
    Vince, Dec 28, 2007
  13. There was none intended.
    Yes, I did. ..
    .. 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.
    '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).

    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

    Message posted via
    Andrew Thompson, Dec 28, 2007
  14. mohed.haidar

    Vince Guest

    I meant my comment was without animosity, sorry for the misunderstanding...
    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...
    Me neither, except that it looks like Don Quichote fighting against mills...

    Anyway, no big deal about that.. peace...
    Vince, Dec 28, 2007
  15. 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.
    ;-) Perhaps. But 'different folks/different strokes/
    different approaches'. In a vibrant community (with
    lots of different opinions and approaches), almost
    anything is good.

    Andrew Thompson

    Message posted via
    Andrew Thompson, Dec 28, 2007
  16. mohed.haidar

    tim Guest

    Who died and left you king?
    tim, Dec 28, 2007
  17. mohed.haidar

    Tim Smith Guest

    Typing "javac" to compile, and "java Foo" to run a program is
    not practical?
    Tim Smith, Dec 28, 2007
  18. mohed.haidar

    Roedy Green Guest

    Roedy Green, Dec 28, 2007
  19. mohed.haidar

    Roedy Green Guest

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


    Also check out

    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, Dec 28, 2007
  20. mohed.haidar

    Mark Space Guest

    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
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.