Topic
  • 1 reply
  • Latest Post - ‏2012-11-10T02:40:37Z by SystemAdmin
michaeljordan846
michaeljordan846
2 Posts

Pinned topic java problem: problem in returning a variable

‏2012-10-06T06:04:45Z |
Hello Friends,

Basically i am looking to make things to read a text firl & create points based upon the data. However i get two errors when i compile it, kindly see the code bellow.

=========================================================
private static Point[] setPoints(String pathIn)
{
int a = 0;
int b = 5;
int size = -1;
int counter = 0;
String xHold = null;
String yHold = null;
int x = -1;
int y = -1;
Point[] points;

try
{
BufferedReader in = new BufferedReader(new FileReader(pathIn));
String str;
while ((str = in.readLine()) != null)
{

if (size != -1)
{
xHold = str.substring(a, b);
str = str.replaceFirst(xHold + " ", "");
yHold = str;

while(xHold.indexOf(" ") != -1)
{
xHold = xHold.replaceFirst(" ", "");
}
x = Integer.parseInt(xHold);

while(yHold.indexOf(" ") != -1)
{
yHold = yHold.replaceFirst(" ", "");
}
y = Integer.parseInt(yHold);
}
else
{
size = Integer.parseInt(str);
points = new Pointsize;
}

if (x <= 32767 && x >= 0
&& y <= 32767 && y >= 0)
{
Point g = new Point(x,y);
pointscounter = g;
counter++;
}

}
in.close();
}
catch (IOException e)
{ }
return points;
}
=========================================================

=========================================================
----jGRASP exec: javac -g -Xlint:unchecked Fast.java

Fast.java:137: error: variable points might not have been initialized
pointscounter = g;
^
Fast.java:146: error: variable points might not have been initialized
return points;
^
2 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
=========================================================

basically after this i just come to know about the error, but nothing i have changed & fixed it..

looking for help...
Thanks,

Web Design Company
Updated on 2012-11-10T02:40:37Z at 2012-11-10T02:40:37Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    210 Posts

    Re: java problem: problem in returning a variable

    ‏2012-11-10T02:40:37Z  
    Firstly,your error is because of the initial problem, because you do not initial the reference points at the point you declare it,and put it in the try-catch statement,Point g = new Point(x,y);pointscounter = g; statements won't be executed,if there is some Exception occured before it.This will result in the situation that using a reference be initial it, to which is not allows.Solution:you could just change "Point points = null;".
    Secondly,I think your program make a strict limitation on your format of you input file.If it is not for a special use, I think it may make your code weak!

    Hope this can serve you some help.