Results 1 to 4 of 4
Thread: HELP, can't run the program!
- 07-10-2011, 06:57 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 7
- Rep Power
- 0
HELP, can't run the program!
Hello, I'm a java beginner and I've built a battleship game. But when I run it I get many exception errors and i can't figure out a solution.
The game consists of two classes: class game which is nearly the whole game and a second class GameSetup which returns random String array positions (ex: A6, F3) for ships for the game.
The game is a multiplayer game, first the game consists of a window which contains 49 buttons which represent a 7x7 grid for the game. A button for exit and another button for new game. Then the game gets the random cells from GameSetup for three ships. And then compares this to the button the user clicked to see if it was a hit or a miss, then the window becomes invisible and the window for player 2 appears and so on, until someone scores 9 hits(3 ships each 3 cells) and wins.
I've posted a simple code of the game because the real code is very long because of very very many decisions based on 49 buttons.
Note: the game isn't fully completed but i wanted to solve the errors before completing it.
Also note that there are not any errors while writing the code, the errors generate only when I run it.
Anyway when i run the game it displays the following errors:
java.lang.NullPointerException
at Game.play(Game.java:1505)
at Game$1.run(Game.java:1087)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
And here is the simplified code:
Java Code:[LIST=1][*]import java.awt.*;[*]import java.awt.event.ActionEvent;[*]import java.awt.event.ActionListener;[*]import javax.swing.*;[*]import javax.swing.border.EmptyBorder; [*]public class Game { [*] JPanel contentPane;[*] JFrame w1 = new JFrame("Player 1 Turn");[*] JFrame w2 = new JFrame("Player 2 Turn");[*] int hits = 0;[*] int x = 0;[*] int win;[*] int abc = 0;[*] int abcdd = 0;[*] String[] p1Ship1 = null;[*] String[] p1Ship2 = null;[*] String[] p1Ship3 = null;[*] [*] String[] p2Ship1 = null;[*] String[] p2Ship2 = null;[*] String[] p2Ship3 = null;[*] [*] [*] int hitss;[*] String tf;[*] String playerr = "";[*] String scoress = "Congratulations,"+ playerr +" You WIN!!!!";[*] [*] //String used in decisions[*] String s1A0 = "";[*] String s1A1 = "";[*] // more Strings here[*] String s333G5 = "";[*] String s333G6 = "";[*] String ha = "f";[*] [*] String tff;[*] String player = "";[*] String scores = "Congratulations,"+ player +" You WIN!!!!";[*] [*] //String used in decisions[*] String ss1A0 = "";[*] String ss1A1 = "";[*] // more String here[*] String ss333G5 = "";[*] String ss333G6 = ""; [*] [*] String haa = "f";[*] [*] final JButton B0 = new JButton("");[*] [*] final JButton B1 = new JButton("");[*] [*] // more buttons here[*] [*] final JButton exit = new JButton("E X I T G A M E"); [*] final JButton newGame = new JButton("N E W G A M E");[*] [*] JLabel lblStats = new JLabel("STATS :");[*] [*] JLabel stats = new JLabel(".");[*] [*] public static void main(String[] args) {[*] EventQueue.invokeLater(new Runnable() {[*] public void run() {[*] try {[*] [*] Game p1 = new Game();[*] [*] p1.play();[*] [*] [*] } catch (Exception e) {[*] e.printStackTrace();[*] }[*] }[*] });[*] } [*] [*]public void play(){[*] [*] if (abc == 0){[*] [*] B0.setBackground(SystemColor.inactiveCaption);[*] [*] B1.setBackground(SystemColor.inactiveCaption);[*] [*] C0.setBackground(SystemColor.inactiveCaption);[*] [*] // more code here[*] [*] A6.setBackground(SystemColor.inactiveCaption);[*] [*] exit.addActionListener(new ActionListener() {[*] public void actionPerformed(ActionEvent e) {[*] System.exit(0);[*] }[*] });[*] exit.setFont(new Font("Times New Roman", Font.PLAIN, 13));[*] [*] [*] newGame.setFont(new Font("Times New Roman", Font.PLAIN, 13));[*] [*] [*] lblStats.setFont(new Font("Tahoma", Font.PLAIN, 16));[*] [*] [*] final SpringLayout sl_contentPane = new SpringLayout();[*] sl_contentPane.putConstraint(SpringLayout.WEST, exit, 0, SpringLayout.WEST, C4);[*] sl_contentPane.putConstraint(SpringLayout.NORTH, stats, 8, SpringLayout.NORTH, lblStats);[*] sl_contentPane.putConstraint(SpringLayout.WEST, stats, 16, SpringLayout.EAST, lblStats);[*] // more code here[*] sl_contentPane.putConstraint(SpringLayout.NORTH, label, 0, SpringLayout.NORTH, lblA_1);[*] sl_contentPane.putConstraint(SpringLayout.WEST, label, 0, SpringLayout.WEST, C0);[*] contentPane.setLayout(sl_contentPane);[*] contentPane.add(lblF);[*] contentPane.add(lblG);[*] //more code here[*] contentPane.add(A5);[*] contentPane.add(A6);[*] [*] GameSetup p1 = new GameSetup();[*] p1Ship1 = p1.Ship1;[*] p1Ship2 = p1.Ship2;[*] p1Ship3 = p1.Ship3;[*] }[*] [*] w1.setVisible(true); [*] [*] [*] [*]if (p1Ship1[0].equals("A0")){[*] A0.addActionListener(new ActionListener() {[*] public void actionPerformed(ActionEvent e) {[*] [*] [*] if (ss1A0.equals("")){[*] A0.setIcon(new ImageIcon("D:\\wan\\hit.jpg"));[*] ss1A0 = ha;[*]hits++; [*]if (hits == 9){ [*]try { Thread.sleep(500);} catch (InterruptedException e1) { e1.printStackTrace();} w1.setVisible(false); JOptionPane.showMessageDialog(null, scores);} if (abc == 0){ w1.setVisible(false); tf = "f"; play2(); [*]} [*] [*] }[*] }[*] });[*] [*] }[*] [*] if (p1Ship1[0].equals("A1")){[*] A1.addActionListener(new ActionListener() {[*] public void actionPerformed(ActionEvent e) {[*] [*] if (ss1A1.equals("")){[*] A1.setIcon(new ImageIcon("D:\\wan\\hit.jpg"));[*] ss1A1 = ha;[*]hits++; [*]if (hits == 9){ [*]try { Thread.sleep(500);} catch (InterruptedException e1) { e1.printStackTrace();} w1.setVisible(false); JOptionPane.showMessageDialog(null, scores);} if (abc == 0){ w1.setVisible(false); tf = "f"; play2(); [*]} [*] [*] }[*] }[*] });[*] [*] }[*] [*] //[*] // MORE decisions here[*] //[*] [*] if (p1Ship3[2].equals("G6")){[*] G6.addActionListener(new ActionListener() {[*] public void actionPerformed(ActionEvent e) {[*] [*] if(ss333G6.equals("")){[*] ss333G6 = ha;[*] G6.setIcon(new ImageIcon("D:\\wan\\hit3.jpg"));[*]hits++; [*]if (hits == 9){ [*]try { Thread.sleep(500);} catch (InterruptedException e1) { e1.printStackTrace();} w1.setVisible(false); JOptionPane.showMessageDialog(null, scores);} if (abc == 0){ w1.setVisible(false); tf = "f"; play2(); [*]} [*] [*] }[*] }[*] });[*] [*] } [*] [*] [*]if (! p1Ship1[0].equals("A0")){[*] A0.addActionListener(new ActionListener() {[*] public void actionPerformed(ActionEvent e) {[*] [*] [*] if (ss1A0.equals(ha)){[*] A0.setIcon(new ImageIcon("D:\\wan\\miss.jpg")); w1.setVisible(false); tf = "f"; play2(); [*] ss1A0 = ha;[*] [*] [*] [*] [*] }[*] }[*] });[*] [*] }[*] [*] [*] //[*] //MORE decisions here[*] //[*] [*] if (! p1Ship3[2].equals("G6")){[*] G6.addActionListener(new ActionListener() {[*] public void actionPerformed(ActionEvent e) {[*] [*] if(! ss333G6.equals(ha)){[*] ss333G6 = ha;[*] G6.setIcon(new ImageIcon("D:\\wan\\miss.jpg")); w1.setVisible(false); tf = "f"; play2(); [*] [*] [*] [*] [*] }[*] }[*] });[*] [*] } [*] [*] [*] if (hits == 9){[*] w1.setVisible(false); [*] win = 7;[*] JOptionPane.showMessageDialog(null, "hIII");[*] [*] } [*]}[*] [*] [*] [*]public void play2(){[*] [*] if (abcdd == 0){[*] [*] JPanel contentPane;[*] int x = 0;[*] int win;[*] [*] final JButton B0 = new JButton("");[*] [*] final JButton B1 = new JButton("");[*] [*] //more buttons here[*] [*] final JButton exit = new JButton("E X I T G A M E"); [*] final JButton newGame = new JButton("N E W G A M E");[*] [*] JLabel lblStats = new JLabel("STATS :");[*] [*] JLabel stats = new JLabel(".");[*] [*]B0.setBackground(SystemColor.inactiveCaption);[*] [*] B1.setBackground(SystemColor.inactiveCaption);[*] [*] C0.setBackground(SystemColor.inactiveCaption);[*] [*] //more code here[*] [*] A5.setBackground(SystemColor.inactiveCaption);[*] [*] A6.setBackground(SystemColor.inactiveCaption);[*] [*] exit.addActionListener(new ActionListener() {[*] public void actionPerformed(ActionEvent e) {[*] System.exit(0);[*] }[*] });[*] exit.setFont(new Font("Times New Roman", Font.PLAIN, 13));[*] [*] newGame.setFont(new Font("Times New Roman", Font.PLAIN, 13));[*] [*] [*] lblStats.setFont(new Font("Tahoma", Font.PLAIN, 16));[*] [*] contentPane = new JPanel();[*] [*] [*] contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));[*] w2.setContentPane(contentPane);[*] [*] [*] [*] [*] final SpringLayout sl_contentPane = new SpringLayout();[*] sl_contentPane.putConstraint(SpringLayout.WEST, exit, 0, SpringLayout.WEST, C4);[*] sl_contentPane.putConstraint(SpringLayout.NORTH, stats, 8, SpringLayout.NORTH, lblStats);[*] sl_contentPane.putConstraint(SpringLayout.WEST, stats, 16, SpringLayout.EAST, lblStats);[*] //more code here[*] sl_contentPane.putConstraint(SpringLayout.NORTH, label, 0, SpringLayout.NORTH, lblA_1);[*] sl_contentPane.putConstraint(SpringLayout.WEST, label, 0, SpringLayout.WEST, C0);[*] contentPane.setLayout(sl_contentPane);[*] contentPane.add(lblF);[*] contentPane.add(lblG);[*] //more code here[*] contentPane.add(A5);[*] contentPane.add(A6);[*] [*] GameSetup p2 = new GameSetup();[*] p2Ship1 = p2.Ship1;[*] p2Ship2 = p2.Ship2;[*] p2Ship3 = p2.Ship3;[*] [*] abcdd++; [*] }[*] [*]w2.setVisible(true); [*]if (p2Ship1[0].equals("A0")){[*] A0.addActionListener(new ActionListener() {[*] public void actionPerformed(ActionEvent e) {[*] [*] [*] if (s1A0.equals("")){[*] A0.setIcon(new ImageIcon("D:\\wan\\hit.jpg"));[*] s1A0 = haa;[*]hitss++; [*]if (hitss == 9){ [*]try { Thread.sleep(500);} catch (InterruptedException e1) { e1.printStackTrace();} w2.setVisible(false); JOptionPane.showMessageDialog(null, scoress);} if (abc == 0){ w2.setVisible(false); tff = "f"; play(); [*]} [*] [*] }[*] }[*] }); [*]} [*]if (p2Ship1[0].equals("A1")){[*] A1.addActionListener(new ActionListener() {[*] public void actionPerformed(ActionEvent e) {[*] [*] if (s1A1.equals("")){[*] A1.setIcon(new ImageIcon("D:\\wan\\hit.jpg"));[*] s1A1 = haa;[*]hitss++; [*]if (hitss == 9){ [*]try { Thread.sleep(500);} catch (InterruptedException e1) { e1.printStackTrace();} w2.setVisible(false); JOptionPane.showMessageDialog(null, scoress);} if (abc == 0){ w2.setVisible(false); tff = "f"; play(); [*]} [*] [*] }[*] }[*] }); [*]} [*]//[*]// MORE decisions here[*]// [*] [*]if (p2Ship3[2].equals("G6")){[*] G6.addActionListener(new ActionListener() {[*] public void actionPerformed(ActionEvent e) {[*] [*] if(s333G6.equals("")){[*] s333G6 = haa;[*] G6.setIcon(new ImageIcon("D:\\wan\\hit3.jpg"));[*]hitss++; [*]if (hitss == 9){ [*]try { Thread.sleep(500);} catch (InterruptedException e1) { e1.printStackTrace();} w2.setVisible(false); JOptionPane.showMessageDialog(null, scoress);} if (abc == 0){ w2.setVisible(false); tff = "f"; play(); [*]} [*] [*] }[*] }[*] }); [*]} [*] [*] [*]if (! p2Ship1[0].equals("A0")){[*] A0.addActionListener(new ActionListener() {[*] public void actionPerformed(ActionEvent e) {[*] [*] [*] if (s1A0.equals(haa)){[*] A0.setIcon(new ImageIcon("D:\\wan\\miss.jpg")); w2.setVisible(false); tff = "f"; play(); [*] s1A0 = haa; [*] [*] }[*] }[*] }); [*]} [*]//[*]//MORE decisions here[*]// [*] [*]if (! p2Ship3[2].equals("G6")){[*] G6.addActionListener(new ActionListener() {[*] public void actionPerformed(ActionEvent e) {[*] [*] if(! s333G6.equals(haa)){[*] s333G6 = haa;[*] G6.setIcon(new ImageIcon("D:\\wan\\miss.jpg")); w2.setVisible(false); tff = "f"; play(); [*] [*] }[*] }[*] }); [*]}[*] [*]} [*]}[/LIST]
So can u help me get the game to run, and if any of you have got any ideas to improve the game code or the game itself I'll be very thankfull.
- 07-10-2011, 07:41 PM #2
Look at line 1505 and see what variable is null. Then back track in the code to see why that variable does not have a non-null value.java.lang.NullPointerException
at Game.play(Game.java:1505)
If you can't see the logic flow that allows it to be null, add plenty of printlns to show its value every place it is changed. System.out.println("1var=" + var); where var is the name of your variable that is null. Change the leading 1 for every println so you can tell which one the println came from.
The output should show you where it is set to null or that it is not being set at all.
- 07-11-2011, 06:21 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 7
- Rep Power
- 0
- 07-11-2011, 06:27 PM #4
Similar Threads
-
How to code a program to send messages to a chat program?
By josh2992 in forum New To JavaReplies: 2Last Post: 04-02-2011, 12:57 PM -
How would I open a program from a single button of another program. Help...
By decgaid06 in forum New To JavaReplies: 13Last Post: 03-22-2011, 06:49 AM -
changing my program to array working program
By Chewart in forum New To JavaReplies: 39Last Post: 11-18-2009, 06:53 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks