Results 1 to 5 of 5
  1. #1
    xkross is offline Member
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default Problems passing a scan object to a method

    Hello Everyone, A special hello to advanced java users and moderators.
    I am currently doing a very intensive java course and have a problem with pass in Scanner object to a method in a class.

    In the main method i have the following codes:-

    public static void main(String[] args) throws IOException
    {
    Scanner scan = new Scanner(new File("magicData.txt"));

    int count = 1; //count which square we're on
    int size = scan.nextInt(); //size of next square


    //Expecting -1 at bottom of input file
    while (size != -1)
    {

    Square square = new Square(size);
    //create a new Square of the given size
    square.readSquare(scan);

    I am having a problem with the last line.
    it gives the error

    --------------------Configuration: <Default>--------------------
    Exception in thread "main" java.lang.NullPointerException
    at Square.readSquare(Square.java:118)
    at SquareTest.main(SquareTest.java:28)

    Here is the method in the Square class

    public void readSquare(Scanner scan)
    {
    for (int row = 0; row < square.length; row++)
    for (int col = 0; col < square.length; col ++)
    square[row][col] = scan.nextInt();
    }



    What can i do to fix this problem and prevent problems like these from occuring in the future?

  2. #2
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    Hello,

    Can you post complete class code?....to more understand your problem.


    sanjeev

  3. #3
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    What's the purpose of having this kind of implementation?

    Assuming that you have passed the Scanner object in another method, what's so special about that object yet you can create a new Scanner object? Don't they have the same effect? Have you seen any difference when you create new Scanner object in inside that method?

    Can't this Scanner class initialize an object in global type?

    regards,
    sukatoa

  4. #4
    xkross is offline Member
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default complete codes

    import java.util.Scanner;

    public class Square
    {
    int[][] square;

    //--------------------------------------
    //create new square of given size
    //--------------------------------------
    public Square(int size)
    {
    int[][] square = new int[size][size];
    }

    //--------------------------------------
    //return the sum of the values in the given row
    //--------------------------------------
    public int sumRow(int row)
    {
    int rowTotal = 0;
    for (int x = 0;x < square.length;x++)
    {
    rowTotal += square[row][x];
    }
    return rowTotal;
    }

    //--------------------------------------
    //return the sum of the values in the given column
    //--------------------------------------
    public int sumCol(int col)
    {

    int colTotal =0;
    for (int x = 0;x < square.length;x++)
    {
    colTotal += square[x][col];
    }
    return colTotal;

    }

    //--------------------------------------
    //return the sum of the values in the main diagonal
    //--------------------------------------
    public int sumMainDiag()
    {
    int MDTotal = 0;
    for (int x = 0;x < square.length;x++)
    {
    MDTotal += square[x][x];
    }
    return MDTotal;

    }

    //--------------------------------------
    //return the sum of the values in the other ("reverse") diagonal
    //--------------------------------------
    public int sumOtherDiag()
    {
    int OMDTotal = 0;
    for (int x = (square.length -1);x >= 0;x--)
    {
    OMDTotal += square[x][x];
    }
    return OMDTotal;

    }

    //--------------------------------------
    //return true if the square is magic (all rows, cols, and diags have
    //same sum), false otherwise
    //--------------------------------------
    public boolean magic()
    {
    boolean ItIsMagic = false;
    int TheSum;

    TheSum = sumMainDiag();

    if (TheSum == sumOtherDiag())
    {
    for (int x = 0; x < square.length; x++)
    {
    if (sumCol(x) == TheSum && sumRow(x) == TheSum)
    ItIsMagic = true;
    else
    {
    ItIsMagic = false;
    continue;
    }

    }
    }
    else
    {
    ItIsMagic = false;
    }
    return ItIsMagic;
    }

    //--------------------------------------
    //read info into the square from the input stream associated with the
    //Scanner parameter
    //--------------------------------------
    public void readSquare(Scanner scan)
    {
    for (int row = 0; row < square.length; row++)
    for (int col = 0; col < square.length; col ++)
    square[row][col] = scan.nextInt();
    }

    //--------------------------------------
    //print the contents of the square, neatly formatted
    //--------------------------------------
    public void printSquare()
    {
    for (int row = 0; row < square.length; row++)
    {
    for (int col = 0; col < square.length; col++)
    System.out.print(square[row][col] + "\t");
    System.out.println();
    }
    }

    }



    Main method below
    import java.io.*;

    public class SquareTest
    {
    public static void main(String[] args) throws IOException
    {
    Scanner scan = new Scanner(new File("magicData.txt"));

    int count = 1; //count which square we're on
    int size = scan.nextInt(); //size of next square


    //Expecting -1 at bottom of input file
    while (size != -1)
    {

    Square square = new Square(size);
    //create a new Square of the given size
    square.readSquare(scan);
    //call its read method to read the values of the square

    System.out.println("\n******** Square " + count + " ********");
    //print the square
    square.printSquare();

    //print the sums of its rows
    for (int x = 0; x < size;x++)
    {
    System.out.print(square.sumRow(x)+"\t");
    }

    //print the sums of its columns
    for (int x = 0; x < size;x++)
    {
    System.out.print(square.sumCol(x)+"\t");
    }
    //print the sum of the main diagonal
    System.out.println(square.sumMainDiag());
    //print the sum of the other diagonal
    System.out.println(square.sumOtherDiag());
    //determine and print whether it is a magic square
    if (square.magic() == true)
    System.out.println("The Square is a magic square");
    else
    System.out.println("The square is not a magic square");

    //get size of next square
    size = scan.nextInt();

    }

    }
    }

    i think the error i discribed earlier is what preventing me from seeing the assignment work.

  5. #5
    xkross is offline Member
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default

    Its a project assignment so i have to can't change the methods as i like.

Similar Threads

  1. Object from String (calling method dynamically)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-16-2008, 09:22 PM
  2. Replies: 0
    Last Post: 11-10-2007, 04:47 PM
  3. Replies: 1
    Last Post: 08-06-2007, 11:10 PM
  4. Replies: 2
    Last Post: 07-09-2007, 05:01 PM
  5. Problems with method
    By ai_2007 in forum Advanced Java
    Replies: 1
    Last Post: 06-28-2007, 06:49 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
  •