Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-16-2008, 04:12 PM
Member
 
Join Date: Apr 2008
Posts: 5
xkross is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-16-2008, 04:29 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
sanjeevtarar is on a distinguished road
Hello,

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


sanjeev
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-16-2008, 04:52 PM
sukatoa's Avatar
Senior Member
 
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 524
sukatoa is on a distinguished road
Send a message via Yahoo to sukatoa
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-16-2008, 04:54 PM
Member
 
Join Date: Apr 2008
Posts: 5
xkross is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-16-2008, 04:56 PM
Member
 
Join Date: Apr 2008
Posts: 5
xkross is on a distinguished road
Its a project assignment so i have to can't change the methods as i like.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Object from String (calling method dynamically) Java Tip Java Tips 0 02-16-2008 10:22 PM
is synchronization on method passing local variables as parameters needed reddzer Java Servlet 0 11-10-2007 05:47 PM
problems with assigning a value to object session osval New To Java 1 08-07-2007 12:10 AM
Passing a new object to a constructor please clarifiy the concept for me please. stefan24 New To Java 2 07-09-2007 06:01 PM
Problems with method ai_2007 Advanced Java 1 06-28-2007 07:49 PM


All times are GMT +3. The time now is 09:10 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org