Results 1 to 4 of 4
  1. #1
    Ritter is offline Member
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

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

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    java.lang.NullPointerException
    at Game.play(Game.java:1505)
    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.
    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.

  3. #3
    Ritter is offline Member
    Join Date
    Feb 2009
    Posts
    7
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    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.
    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.
    First of all thanks, but actually line 1505 doesn't contain a variable (I think) it contains the following:

    contentPane.setLayout(sl_contentPane);

    which is line 85 in the code above so any suggestions????

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    Java Code:
    contentPane.setLayout(sl_contentPane);
    line 1505 doesn't contain a variable
    contentPane and sl_contentPane are the variables on that line. setLayout() is the name of a method.

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  2. Replies: 13
    Last Post: 03-22-2011, 06:49 AM
  3. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •