4 Replies - 692 Views - Last Post: 15 November 2012 - 08:42 AM Rate Topic: -----

#1 GRRRRR123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-November 12

Magic Square NullPointerException error.

Posted 14 November 2012 - 08:44 PM

Im wondering why i get an null pointer exception, i cant seem to find out where its occuring at. I'm wondering if anyone can spot it in my program ><


     public class Square
    {
        int [][]board;
        int size;
        int row = 0;
        int col = 0;
        int column;
        int sumR;
        int sumC;
        int sumDiag;
        int [][] table;
        boolean rowEqual;
        boolean columnEqual;
       
        public Square (int n)
        {  
           
            size = n;
            board = new int[size][size];
            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    board[i][j] = 0;
                }
            }
        }
       
        public void add(int aNum)
        {
           
            {
                if (col> size - 1)
                {
                    row ++;
                    col = 0;
                }
                    board[row][col] = aNum;
                    col++;
            }
           
        }
       
        public boolean sumRow()
        {
            int prevSum = 0;
            sumR = 0;
            rowEqual = true;
                for(row = 0; row < board.length; row++)
                {
                    if (row != 0)
                    {
                        for (column = 0; column < board.length; column++)
                        {
                            sumR = sumR + table[row][column];
                        }
                       
                        if (prevSum != sumR)
                        {
                            rowEqual = false;
                            break;
                        }
                       
                        prevSum = sumR;
                    }
                   
                    else
                    {
                        for (column = 0; column < board.length; column++)
                        {
                            sumR = sumR + table[row][column];
                        }
                       
                        prevSum = sumR;
                    }
                }
                   
                   
                return rowEqual;
        }
       
        public boolean sumCol()
        {
            int prevSum = 0;
            sumC = 0;
            columnEqual = true;
                for(column = 0; column < board.length; column++)
                {
                    if (column != 0)
                    {
                        for (row = 0; row < board.length; row++)
                        {
                            sumC = sumC + table[row][column];
                        }
                       
                        if (prevSum != sumC)
                        {
                            columnEqual = false;
                            break;
                        }
                       
                        prevSum = sumC;
                    }
                   
                    else
                    {
                        for (row = 0; row < board.length; row++)
                        {
                            sumC = sumC + table[row][column];
                        }
                       
                        prevSum = sumC;
                    }
                }
                   
                   
                return columnEqual;
        }
       
       /* public int sumDiag()
        {
            for (row = 0, column = board.length - 1; row < board.length; row++)
                {
                    sumDiag += board[row][column];
                    column --;
                }
            return sumDiag;
        }*/
       
        public boolean isMagic()
        {
           boolean notorisMagic = false;
            if (sumRow( ) == true && sumCol() == true)
            {
                if(sumR == sumC)
                {
                            notorisMagic = true;      
                }
            }
            return notorisMagic;
        }
       
        @Override
        public String toString()
        {
            String r = "";
                for (int i = 0; i < size; i++)
                {
                    r = r + "|";
                        for (int j = 0; j< size; j++)
                        {
                            if (board[i][j] < 10)
                            {
                                r = r + "0" + board[i][j] + "|";
                            }
                           
                            else
                            {
                                r = r + board[i][j] + "|";
                            }
                        }
                     r = r + "\n" ;  
                       
                }
                return r;
        }
       
    }



Is This A Good Question/Topic? 0
  • +

Replies To: Magic Square NullPointerException error.

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,448
  • Joined: 20-September 08

Re: Magic Square NullPointerException error.

Posted 15 November 2012 - 04:18 AM

http://technojeeves....free/118-errors
Was This Post Helpful? 1
  • +
  • -

#3 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Magic Square NullPointerException error.

Posted 15 November 2012 - 04:22 AM

I would suggest you use try...catch blocks so that you will be in a position to see where exactly in the program the exception is taking place.

regards,
Raghav
Was This Post Helpful? -1
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,448
  • Joined: 20-September 08

Re: Magic Square NullPointerException error.

Posted 15 November 2012 - 04:46 AM

Likewise
In point of fact, the most likely reason for not seeing that information is by using try/catch (incorrectly)
Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Magic Square NullPointerException error.

Posted 15 November 2012 - 08:42 AM

View Postraghav.naganathan, on 15 November 2012 - 07:22 AM, said:

I would suggest you use try...catch blocks so that you will be in a position to see where exactly in the program the exception is taking place.

Remove all the try/catch and the run time stack trace will tell you exactly where is the error :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1