Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Help with my first Java program

Reply
Thread Tools

Help with my first Java program

 
 
mohed.haidar@gmail.com
Guest
Posts: n/a
 
      12-27-2007
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:3
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.
 
Reply With Quote
 
 
 
 
Donkey Hot
Guest
Posts: n/a
 
      12-27-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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:3
> 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.

 
Reply With Quote
 
 
 
 
Vince
Guest
Posts: n/a
 
      12-27-2007
(E-Mail Removed) 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:3
> 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

 
Reply With Quote
 
lord.zoltar@gmail.com
Guest
Posts: n/a
 
      12-27-2007
On Dec 27, 8:53*am, (E-Mail Removed) 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.
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      12-27-2007
(E-Mail Removed) 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

 
Reply With Quote
 
Vince
Guest
Posts: n/a
 
      12-27-2007
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

 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      12-27-2007
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.
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      12-27-2007
(E-Mail Removed) 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/
 
Reply With Quote
 
Karl
Guest
Posts: n/a
 
      12-27-2007


<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Dec 27, 8:53 am, (E-Mail Removed) 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.


 
Reply With Quote
 
Arne Vajhøj
Guest
Posts: n/a
 
      12-27-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Executing a JAVA Program from a JAVA Program Shadow123 Java 0 08-11-2008 07:13 PM
Running Java Program from a Java Program Shadow123 Java 1 07-09-2008 08:20 PM
How to launch One Java Program from another Java Program joshivaibhav Java 3 11-21-2006 05:42 PM
How to javac a java program w/ another java program which is w/o a main method cjeffwang@yahoo.com Java 1 10-31-2005 04:25 AM
Calling Java program in another Java program Rey Java 4 12-12-2003 10:18 PM



Advertisments