Reputation: 115
I have created a java program using Eclipse and am attempting to run it online (it's a simple game). I have the .class file working on it's own and the program runs end to end with no problems. I have problems when I try and run it on the website. I have used the following code to attempt to launch it as an applet with no luck:
<html>
<body>
<applet
code="Handler.class"
codebase="AlamoAdventure/"
name="Alamo Battle Adventure"
width="680"
height="509"
archive="Handler.jar"
id="Alamo Battle Adventure" >
</applet>
</body>
</html>
When I goto the website it tells me to click on the applet for details. When I do that, I get a popup that gives me a RuntimeException of: java.lang.reflect.InvocationTargetException
What are the steps that I am missing to get this running? Both the Handler.class and the Handler.jar are in the same location.
The main menu java code is as follows:
/*
* Main Menu is the start up window that is displayed when the program starts. It has one button, "Start Game", and a randomly
* selected picture from a batch of 6.
* 0->23
*/
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.util.Random;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MainMenu extends JFrame
{
private JButton sGame; //The "Start Game" Button
private FlowLayout layout; //Layout of the window
private ImageIcon image; //The images
public MainMenu()
{
super("ABA:Main Menu"); // Title of Window
JPanel right = new JPanel();
JPanel left = new JPanel();
left.setLayout(new BoxLayout(left, BoxLayout.LINE_AXIS));
add(left);
right.setLayout(new BoxLayout(right, BoxLayout.PAGE_AXIS));
add(right);
sGame = new JButton("Start Game");
left.add(sGame);
left.add(Box.createRigidArea(new Dimension(10,0)));
right.add(left);
Images image = new Images();
right.add(image);
ButtonHandle handle = new ButtonHandle();
sGame.addActionListener(handle);
} // end Button const.
/*
* When the button is pressed it will call the functions to start the new windows and storyline
*/
private class ButtonHandle implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
if (event.getActionCommand().contentEquals("Start Game"))
{
int[] i;
int n, ch, c;
i = new int[3];
Text story = new Text();
int k = 10;
i = Options.Option1(k);
n = i[0];
ch = i[2];
c = i[1];
/*
* Debugging technique to allow proper pathing for map and story line
*/
System.out.print(n);
System.out.print(c);
String option1;
String option2;
String option3;
String storyMode;
String[] a;
a = new String[2];
a = Choices.getChoice(n);
storyMode = story.getText(ch,n);
option1 = a[0];
option2 = a[1];
option3 = a[2];
Button call = new Button(option1, option2, option3, storyMode, n);
call.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
call.setSize(680,509);
call.setVisible(true);
MainMenu.this.dispose();
}
}
}
/*
* Class to render the 6 random pictures on the Main Menu under the "Start Game"
*/
class Images extends JPanel
{
private ImageIcon picture;
private Random generator = new Random(); // Generator for random numbers
private String[] images = {"1.JPG", "2.JPG", "3.JPG",
"4.JPG", "5.JPG", "6.JPG"}; // These are the paths to the 6 pictures that will be randomly called
public Images()
{
int randomN = generator.nextInt(images.length); // Random number between 0 and 5
picture = new ImageIcon(getClass().getResource(images[randomN])); // initializes the picture and selects the path from the randomN
}
protected void paintComponent(Graphics g)
{
super.paintComponents(g);
Dimension d = getSize();
g.drawImage(picture.getImage(),0,0,d.width, d.height, null);
}
public Dimension getPreferredSize()
{
return new Dimension( picture.getIconWidth(), picture.getIconHeight() );
}
}
}
Upvotes: 0
Views: 150
Reputation: 168845
A little testing here suggests the class is not an applet. When the console is set to level 5, and the applet is run, the details are there. I expect you made a JFrame
by mistake, but that might be better in any case.
Post that class declaration so I can confirm.
When run from the command line, I see:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at MainMenu$Images.<init>(MainMenu.java:120)
at MainMenu.<init>(MainMenu.java:46)
at Handler.main(Handler.java:11)
What is on line 120 of MainMenu.java
?
Upvotes: 1