Results 1 to 4 of 4
- 02-06-2012, 07:53 AM #1
Member
- Join Date
- Nov 2011
- Posts
- 15
- Rep Power
- 0
What did I do wrong? [Run Time Exception Error] High School Comp. Sci
Instructions:
Conway's Game of Life
Runner [This is from my teacher, so it's correct]:
Java Code:import javax.swing.*; import java.awt.event.*; import java.awt.*; public class GameOfLifeRunner extends JFrame implements ActionListener { GameOfLifePanel myPanel; JButton step,run,reset; JPanel buttons; JComboBox premades; Timer myTimer; public GameOfLifeRunner() { myTimer = new Timer(150,this); premades = new JComboBox(new Object[] {"Premades","Glider","Exploder","Tumbler","Glider Gun","Random"}); this.setLayout(new BorderLayout()); myPanel = new GameOfLifePanel(50,40); step = new JButton("Step"); run = new JButton("Run"); reset = new JButton("Reset"); buttons = new JPanel(new FlowLayout()); buttons.add(step); buttons.add(run); buttons.add(reset); buttons.add(premades); this.add(myPanel); this.add(BorderLayout.NORTH,buttons); premades.addActionListener(this); reset.addActionListener(this); step.addActionListener(this); run.addActionListener(this); //this.setSize(567,589); this.pack(); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { if( e.getSource() == step) { myPanel.step(); } else if(e.getSource() == reset) { myPanel.reset(); } else if(e.getSource() == run) { if(run.getText().equals("Run")) { run.setText("Stop"); myTimer.start(); } else { run.setText("Run"); myTimer.stop(); } } else if( e.getSource() == myTimer) { myPanel.step(); } else if(e.getSource()==premades) { myPanel.premade((String)premades.getSelectedItem()); } repaint(); } class GameOfLifePanel extends JPanel implements MouseListener { private int myRows=50, myCols=50; int size = 10; GameOfLife myGOL; public GameOfLifePanel() { myGOL = new GameOfLife(); } public GameOfLifePanel(int r,int c) { myRows = r; myCols = c; this.setPreferredSize(new Dimension(myRows*size,myCols*size)); this.addMouseListener(this); myGOL = new GameOfLife(r,c); } public void printLocations() { System.out.print("{"); boolean flag = false; for( int r = 0; r<myGOL.numRows();r++) { for( int c = 0; c<myGOL.numCols();c++) { if( myGOL.getValAt(r,c) ) { if(flag) { System.out.print(","); } flag=true; System.out.print("{"+r+","+c+"}"); } } } System.out.println("}"); } public void paintComponent(Graphics g) { g.clearRect(0,0,1000,1000); for(int row = 0; row< myGOL.numRows(); row++) for(int col = 0; col<myGOL.numCols(); col++) { if( myGOL.getValAt(row,col )) g.setColor(Color.RED); else g.setColor(Color.WHITE); g.fillRect(row*size,col*size,size,size); g.setColor(Color.BLACK); g.drawRect(row*size,col*size,size,size); } } public void mouseClicked(MouseEvent m) { if(myTimer.isRunning()) { run.setText("Run"); myTimer.stop(); } } public void mouseReleased(MouseEvent m) { int x = m.getX(); int y = m.getY(); int r = x/size; int c = y/size; myGOL.toggle(r,c); repaint(); } public void reset() { myGOL.reset(); repaint(); } public void step() { myGOL.step(); repaint(); } public void premade(String item) { int[][] pairs=null; if( item.equals("Glider")) { pairs = new int[][]{{8,10},{9,8},{9,10},{10,9},{10,10}}; } else if( item.equals("Exploder")) { pairs = new int[][]{{18,17},{18,18},{18,19},{18,20},{18,21},{20,17},{20,21},{22,17},{22,18},{22,19},{22,20},{22,21}}; } else if( item.equals("Tumbler")) { pairs = new int[][]{{23,9},{23,10},{23,11},{24,6},{24,7},{24,11},{25,6},{25,7},{25,8},{25,9},{25,10},{27,6},{27,7},{27,8},{27,9},{27,10},{28,6},{28,7},{28,11},{29,9},{29,10},{29,11}}; } else if( item.equals("Glider Gun")) { pairs = new int[][]{{0,6},{0,7},{1,6},{1,7},{11,6},{11,7},{11,8},{12,5},{12,9},{13,4},{13,10},{14,5},{14,9},{15,6},{15,7},{15,8},{16,6},{16,7},{16,8},{21,4},{21,5},{21,6},{22,3},{22,4},{22,6},{22,7},{23,3},{23,4},{23,6},{23,7},{24,3},{24,4},{24,5},{24,6},{24,7},{25,2},{25,3},{25,7},{25,8},{30,6},{30,7},{34,4},{34,5},{35,4},{35,5}}; // pairs = new int[][]{{3,22},{4,22},{4,23},{4,24},{4,25},{5,12},{5,13},{5,23},{5,24},{5,25},{5,26},{5,34},{6,12},{6,13},{6,23},{6,26},{6,33},{6,35},{7,17},{7,23},{7,24},{7,25},{7,26},{7,31},{7,32},{7,36},{7,46},{7,47},{8,17},{8,22},{8,23},{8,24},{8,25},{8,31},{8,32},{8,36},{8,46},{8,47},{9,22},{9,31},{9,32},{9,36},{10,33},{10,35},{11,34}}; } else if( item.equals("Premades")) { printLocations(); } else if( item.equals("Random")) { for(int row = 0; row< myGOL.numRows(); row++) for(int col = 0; col<myGOL.numCols(); col++) { if( row<myGOL.numRows()&& col<myGOL.numCols()) myGOL.setValAt(row,col,(int)(Math.random()*4)==0); } } else System.out.println("ERROR IN PREMADE"); if(pairs != null) for( int i = 0; i<pairs.length;i++) { if( pairs[i][0]<myGOL.numRows()&& pairs[i][1]<myGOL.numCols()) myGOL.setValAt(pairs[i][0],pairs[i][1],true); } } public void mousePressed(MouseEvent m) { } public void mouseExited(MouseEvent m) { } public void mouseEntered(MouseEvent m) { } } public static void main(String[] args) { new GameOfLifeRunner(); } }
My Program:
Ty.Java Code:public class GameOfLife { boolean[][] myArr; //Default constructor. Creates a 50x50 boolean array in myArr. public GameOfLife() { myArr = new boolean[50][50]; } //Constructor, assigns the parameter array to the instance field array. public GameOfLife(boolean[][] arr2D) { arr2D = myArr; } //Constructor. Creates a boolean array with r Rows and c Columns and stores it in myArr public GameOfLife(int r, int c) { boolean newArr[][] = new boolean[r][c]; newArr = myArr; } //returns the value at the given row and column. public boolean getValAt(int r, int c) { return myArr[r][c]; } public void setValAt(int r, int c, boolean val) { myArr[r][c] = val; } //Toggles the value at the given location, turning true to false and false to true public void toggle(int r, int c) { if(myArr[r][c] == true) { myArr[r][c] = false; } if(myArr[r][c] == false) { myArr[r][c] = true; } } //returns the number of rows public int numRows() { int toRet = myArr.length; return toRet; } //returns the number of columns public int numCols() { int toRet = myArr[0].length; return toRet; } //assigns false to each of the values in the array public void reset() { for(int row = 0; row < myArr.length; row++) { for(int col = 0; col < myArr[0].length; col++) { myArr[row][col] = false; } } } // returns the number of neighbors that are in the cells surrounding the given row and columnn public int numOfNeighbors(int r,int c) { int toRet = 0; if(myArr[r][c] = true) { for(int row = r-1; row < myArr.length; row++) { for(int col = c-1; col < myArr[0].length; col++) { if((r != 0 && c != 0 && r != 49 && c != 49) && (myArr[row][col] == true) && (myArr[row][col] != myArr[r][c])) { toRet++; } } } } return toRet; } //The core method. Determines which cells live and die. Use the numOfNeighbors method! public void step() { boolean[][] tmp = new boolean[myArr.length][myArr[0].length]; for(int row = 0; row < myArr.length; row++) { for(int col = 0; col < myArr[0].length; col++) { if(myArr[row][col] == true) { if(this.numOfNeighbors(row, col) <= 1) { tmp[row][col] = false; } if(this.numOfNeighbors(row, col) >= 4) { tmp[row][col] = false; } if(this.numOfNeighbors(row, col) == 2 || this.numOfNeighbors(row, col) == 3) { tmp[row][col] = true; } } } } } }Last edited by Calaminh; 02-06-2012 at 07:56 AM.
- 02-06-2012, 08:15 AM #2
Re: What did I do wrong? [Run Time Exception Error] High School Comp. Sci
tl;dr
When seeking help with an error, post the entire stack trace and indicate in the code which line threw the error.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-06-2012, 08:29 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 15
- Rep Power
- 0
Re: What did I do wrong? [Run Time Exception Error] High School Comp. Sci
Sorry DarrylBurke, I forgot.
Java Code:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at GameOfLife.numRows(GameOfLife.java:48) at GameOfLifeRunner$GameOfLifePanel.paintComponent(GameOfLifeRunner.java:125) at javax.swing.JComponent.paint(JComponent.java:1029) at javax.swing.JComponent.paintChildren(JComponent.java:862) at javax.swing.JComponent.paint(JComponent.java:1038) at javax.swing.JComponent.paintChildren(JComponent.java:862) at javax.swing.JComponent.paint(JComponent.java:1038) at javax.swing.JLayeredPane.paint(JLayeredPane.java:567) at javax.swing.JComponent.paintChildren(JComponent.java:862) at javax.swing.JComponent.paintToOffscreen(JComponent.java:5131) at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:278) at javax.swing.RepaintManager.paint(RepaintManager.java:1224) at javax.swing.JComponent.paint(JComponent.java:1015) at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21) at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60) at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97) at java.awt.Container.paint(Container.java:1780) at java.awt.Window.paint(Window.java:3375) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713) at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693) at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEvent(EventQueue.java:597) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(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) Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at GameOfLife.numRows(GameOfLife.java:48) at GameOfLifeRunner$GameOfLifePanel.paintComponent(GameOfLifeRunner.java:125) at javax.swing.JComponent.paint(JComponent.java:1029) at javax.swing.JComponent.paintChildren(JComponent.java:862) at javax.swing.JComponent.paint(JComponent.java:1038) at javax.swing.JComponent.paintChildren(JComponent.java:862) at javax.swing.JComponent.paint(JComponent.java:1038) at javax.swing.JLayeredPane.paint(JLayeredPane.java:567) at javax.swing.JComponent.paintChildren(JComponent.java:862) at javax.swing.JComponent.paintToOffscreen(JComponent.java:5131) at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:278) at javax.swing.RepaintManager.paint(RepaintManager.java:1224) at javax.swing.JComponent.paint(JComponent.java:1015) at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21) at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60) at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97) at java.awt.Container.paint(Container.java:1780) at java.awt.Window.paint(Window.java:3375) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713) at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693) at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) at java.awt.EventQueue.dispatchEvent(EventQueue.java:597) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(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)
- 02-06-2012, 02:03 PM #4
Re: What did I do wrong? [Run Time Exception Error] High School Comp. Sci
At line 48 of GameOfLife.java, something is null which shouldn't be. Trace back in your code to find which variable used on that line hasn't been initialized.java.lang.NullPointerException
at GameOfLife.numRows(GameOfLife.java:48)
It doesn't get much simpler than that.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
School Project - runing out of time D:
By Bagzli in forum New To JavaReplies: 3Last Post: 03-02-2011, 05:04 PM -
High Compile Time
By Mojito_gr in forum Advanced JavaReplies: 7Last Post: 08-02-2010, 04:47 AM -
Wrong time
By OrangeDog in forum Suggestions & FeedbackReplies: 5Last Post: 11-20-2009, 03:24 AM -
What did i do wrong this time!
By PureAwesomeness in forum New To JavaReplies: 28Last Post: 01-19-2009, 11:47 PM -
Need help with a program for high school regarding multi-dimensional arrays.
By Torque in forum New To JavaReplies: 2Last Post: 01-07-2008, 07:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks