Help with my first Java program

M

mohed.haidar

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.
 
D

Donkey Hot

(e-mail address removed) wrote in @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.
 
V

Vince

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.
 
L

lord.zoltar

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.
 
A

Andrew Thompson

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.
 
V

Vince

Andrew said:
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!
 
M

Mark Space

Vince said:
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.
 
M

Mark Space

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/
 
K

Karl

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.
 
A

Arne Vajhøj

Mark said:
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
 
A

Andrew Thompson

Arne said:
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
 
V

Vince

Mark said:
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...
 
A

Andrew Thompson

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).

* said:
..- 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
 
V

Vince

Andrew said:
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...
 
A

Andrew Thompson

Vince said:
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.
...
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
 
T

tim

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?
 
T

Tim Smith

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?
 
R

Roedy Green

M

Mark Space

Tim said:
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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top