![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Quick question.. really Quick
I'm using the following code:
System.out.println ("position: " + fileData [i].indexOf ("<GPieceSize>"));I'm getting an error saying that there's a null pointerException. How is this possible? if the value isn't found within the string then a (-1) should be printed. Does anyone understand why there's a null pointer exception?
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
#2 |
|
Expert Programmer
|
fileData[i] is null.
|
|
|
|
|
|
#3 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Quote:
Would you know what would cause it to be null?
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Here's the code to read in the .txt file and store it in the string array from the above example
String temp = "";
BufferedReader in = new BufferedReader (new FileReader ("PieceData1.txt"));
while ((temp = in.readLine ()) != null)
{
fileCount = fileCount + 1;
}
in.close ();
System.out.print ("Size of file is: " + fileCount);
// size of file has been stored
// use the size of the file to make an array of the specified to size of the file
String fileData [] = new String [fileCount];
in = new BufferedReader (new FileReader ("PieceData1.txt"));
for (int i = 0 ; i < fileCount ; i++) //
{
fileData [i] = in.readLine ();
}
in.close ();
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
#5 |
|
Expert Programmer
|
This code is very inefficient as you read the entire file twice. You should instead use an ArrayList to store each line.
|
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
So i've given you the part where it reads from the file/ stores it into the array. That part doesn't give me any errors. Now, here's the code where the error comes into play. I've placed it in blue font.
int tempLength;
int position = 0;
int marker = -1; // a negative value so we know it's not anywhere in the file by default
SearchForGrid: // a label
for (int m = 0 ; m < fileCount ; m++)
{
tempLength = fileData [m].length ();
position = fileData [m].indexOf ("<GridSize>");
if (position >= 0) // if strings not found, returns negative. Thus, if found it's (> 0)
{
marker = m; // holds place where grid is found
m++; // we have nothing else to do on this line
break SearchForGrid; // done looking for grid
}
}
String tempString = new String ("");
boolean blnXFound = false, blnYFound = false;
SearchGridSize:
for (int i = marker ; i < fileCount ; i++)
{
System.out.println ("position: " + fileData [i].indexOf ("<GPieceSize>"));
position = fileData [i].indexOf ("<GPieceSize>");
// if we passed onto the other tag, exit
if (position >= 0)
{
System.out.println ("Check your file (PieceData.txt), Somethings not right in there. Quit right now.");
break SearchGridSize;
}
position = fileData [i].indexOf ("x=");
if (position >= 0)
{
tempLength = fileData [i].length (); // takes lenght so we know where string ends
for (int z = 2 ; z < tempLength ; z++)
{
tempString = tempString + fileData [i].charAt (z);
}
xGridSize = Integer.parseInt (tempString);
System.out.println (" " + xGridSize);
blnXFound = true;
}
position = fileData [i].indexOf ("y=");
if (position >= 0)
{
tempLength = fileData [i].length (); // takes lenght so we know where the string ends
for (int z = 2 ; z < tempLength ; z++)
{
tempString = tempString + fileData [i].charAt (z); // adds the remaining digits together
}
yGridSize = Integer.parseInt (tempString);
System.out.println (" " + yGridSize);
blnYFound = true;
}
if (blnXFound == true && blnYFound == true)
{
break SearchGridSize;
}
}
}
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
#7 |
|
Expert Programmer
|
I believe the problem is occuring as a result of code you have not posted. My guess is that the fileData array has been initialized (eg, String[] fileData = new String[fileCount];) but has not yet been filled with data from the first fragment of code you posted. (As you know, when an object array is first initialized, all of its indices are set to null.) Unless I'm missing something, you'll have to post more code.
|
|
|
|
|
|
#8 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
Quote:
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 214
Rep Power: 0
![]() |
It is not being initialized right here?
String fileData [] = new String [fileCount];
in = new BufferedReader (new FileReader ("PieceData1.txt"));
for (int i = 0 ; i < fileCount ; i++) //
{
fileData [i] = in.readLine ();
}
in.close ();
__________________
Death smiles at us all. All a man can do is smile back. |
|
|
|
|
|
#10 |
|
Expert Programmer
|
Yes. The fileData array is correctly initialized in that code, however that is not the case in the second code fragment you posted. You will have to post the rest of the code.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|