Results 1 to 7 of 7
  1. #1
    remus19's Avatar
    remus19 is offline Member
    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0

    Unhappy java.lang.StackOverflowError???

    i know many people ask that error. i read most of topics in forum about that but every one is different. so i decided to ask myself. i wrote following codes and i cant see there any infinite or recursive situation. i need help quickly. i have only one day for finish this project..
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class lightsout extends JFrame{
    		ImageIcon blcIcon = new ImageIcon("black.jpg");
    		ImageIcon prsIcon = new ImageIcon("pressed.jpg");
    		ImageIcon whtIcon = new ImageIcon("white.jpg");
    		Image blcImage = blcIcon.getImage();
    		Image prsImage = prsIcon.getImage();
    		Image whtImage = whtIcon.getImage();
    		JTextField jtf = new JTextField(15);
    		JPanel p3 = new JPanel();
    	        JPanel p2 = new JPanel();
    		int[][] matrix;
    		JLabel jlb = new JLabel("Enter a number:");
    		int n;
    		Graphics g;
        	public static void main(String[]args){
            lightsout frame = new lightsout();
            frame.setTitle("Lights Out");
        	frame.setSize(800,500);
        	frame.setLocationRelativeTo(null);
        	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	frame.setVisible(true);
        	}
        public lightsout() {
        	add(p3, BorderLayout.CENTER);
        	p3.setLayout(new BorderLayout(5,5));
        	setDisplay("2");
        	
        	p2.setLayout(new BorderLayout(25,25));
        	p2.add(jlb, BorderLayout.WEST);
        	p2.add(jtf, BorderLayout.CENTER);
        	p3.add(p2, BorderLayout.NORTH);
        	p3.add(new p1(), BorderLayout.CENTER);
        	jtf.addActionListener(new ActionListener() {
        		public void actionPerformed(ActionEvent e) {
        			setDisplay(jtf.getText());
        			}
        		});
        	}
         public void setDisplay(String index) {
    		matrix = null;
       		n = Integer.parseInt(index);
       		matrix = new int[n][n];
        	for(int i = 0; i < n; i++){
        		for(int j = 0; j < n; j++){
        			matrix[i][j] = 0;
        		}
    		}
    		p3.revalidate();
    		p3.repaint();
    		}
    }
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class p1 extends lightsout implements MouseListener{
    	public void p1(){
    		add(new panel());
    	}
    	public void mouseClicked(MouseEvent e){
        				int row = findWhichRowSelected(e.getY());
    					int column = findWhichColSelected(e.getX());
        				if (matrix[row][column] == 0){
        					matrix[row][column] = 1;
        				}
        				else if (matrix[row][column] == 1){
        					matrix[row][column] = 0;
        				}
        				repaint();
        }
    	public void mouseEntered (MouseEvent e)
    	{
    	}
    	public void mouseExited (MouseEvent e)
    	{
    	}
    	public void mousePressed (MouseEvent e)
    	{
    	}
    	public void mouseReleased (MouseEvent e)
    	{
    	}
    class panel extends JPanel{
    	protected void paintComponent(Graphics g) {
    			super.paintComponent(g);
    	 		for (int i = 0; i < n; i++) {
    				for (int j = 0; j < n; j++) {
    					if (matrix[i][j] == 0) {
    						g.drawImage(blcImage, i * getWidth() / n, j * getHeight() / n, getWidth() / n, getHeight() / n, this);
    					}
    					else if (matrix[i][j] == 1) {
    						g.drawImage(whtImage, i * getWidth() / n, j * getHeight() / n, getWidth() / n, getHeight() / n, this);
    					}
    				}
     			}
     		}
    	}
    	private int findWhichRowSelected (int row){
    		for (int i = 0; i < n; i++){
    			if (row<(i + 1)*getWidth()/n && row>(i + 2)*getWidth()/n){
    				return i;
    			}	
    		}
    		return -1;	
    	}
    	private int findWhichColSelected (int col){
    		for (int i = 0; i < n; i++){
    			if (col<(i + 1)*getHeight()/n && col>(i + 2)*getHeight()/n){
    				return i;
    			}	
    		}
    		return -1;	
    	}
    }

  2. #2
    remus19's Avatar
    remus19 is offline Member
    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0

    Default

    the error is this:
    Exception in thread "main" java.lang.StackOverflowError
    at java.awt.Insets.<init>(Insets.java:86)
    at sun.awt.windows.WToolkit.getScreenInsets(Native Method)
    at sun.awt.windows.WToolkit.getScreenInsets(WToolkit. java:549)
    at java.awt.Window.init(Window.java:395)
    at java.awt.Window.<init>(Window.java:433)
    at java.awt.Frame.<init>(Frame.java:403)
    at java.awt.Frame.<init>(Frame.java:368)
    at javax.swing.JFrame.<init>(JFrame.java:158)
    at lightsout.<init>(lightsout.java:33)
    at p1.<init>(p1.java:5)
    at lightsout.<init>(lightsout.java:42)
    at p1.<init>(p1.java:5)
    at lightsout.<init>(lightsout.java:42)
    at p1.<init>(p1.java:5)
    at lightsout.<init>(lightsout.java:42)
    at p1.<init>(p1.java:5)
    at lightsout.<init>(lightsout.java:42)
    at p1.<init>(p1.java:5)
    at lightsout.<init>(lightsout.java:42)
    at p1.<init>(p1.java:5)
    at lightsout.<init>(lightsout.java:42)
    at p1.<init>(p1.java:5)
    at lightsout.<init>(lightsout.java:42)
    at p1.<init>(p1.java:5)
    at lightsout.<init>(lightsout.java:42)
    at p1.<init>(p1.java:5)
    at lightsout.<init>(lightsout.java:42)
    at p1.<init>(p1.java:5)
    at lightsout.<init>(lightsout.java:42)
    at p1.<init>(p1.java:5)
    at lightsout.<init>(lightsout.java:42)
    at p1.<init>(p1.java:5)....... end continues

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Would you mind highlighting the two lines that alternate continuously?

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    You've got an infinite recursion going on in your constructor(s) - lightsout adds p1, but since p1 extends lightsout when you create this p1, it will try to create and add another, and this will continue until you get the overflow.
    First, suggested reading:
    Code Conventions for the Java Programming Language
    Second, not sure why p1 extends lightsout...either remove the extension or create a default constructor for p1 (which you don't have - there's a method by the same name but not a constructor)

  5. #5
    remus19's Avatar
    remus19 is offline Member
    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0

    Default

    i understand but if i remove the extension how can i use the variables like matrix, n, getWidth etc. ? i must use them in both lightsout and p1.

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Quote Originally Posted by remus19 View Post
    i understand but if i remove the extension how can i use the variables like matrix, n, getWidth etc. ? i must use them in both lightsout and p1.
    Many ways....for example you could pass an instance of lightsout (or the variables themselves) to the p1 class, and access the values in that way.

  7. #7
    remus19's Avatar
    remus19 is offline Member
    Join Date
    Apr 2011
    Posts
    13
    Rep Power
    0

Similar Threads

  1. java.lang.StackOverflowError Exception
    By Marcus in forum Web Frameworks
    Replies: 4
    Last Post: 08-24-2012, 10:02 PM
  2. java.lang.StackOverflowError
    By malstryx in forum New To Java
    Replies: 9
    Last Post: 10-01-2008, 04:14 AM
  3. java.lang.StackoverflowError
    By ravisankarvivek in forum New To Java
    Replies: 6
    Last Post: 06-23-2008, 09:05 AM
  4. java.lang.StackOverFlowError exception
    By jayaj in forum NetBeans
    Replies: 1
    Last Post: 06-08-2008, 11:17 AM
  5. java.lang.StackOverflowError
    By eva in forum New To Java
    Replies: 3
    Last Post: 12-24-2007, 09:54 AM

Posting Permissions

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