Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 1st, 2006, 10:29 PM   #1
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
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.
Eric the Red is offline   Reply With Quote
Old Jun 1st, 2006, 10:35 PM   #2
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 903
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
fileData[i] is null.
titaniumdecoy is offline   Reply With Quote
Old Jun 1st, 2006, 10:40 PM   #3
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Quote:
Originally Posted by titaniumdecoy
fileData[i] is null.
Well i'm reading in the arrray from a ".txt" file. and at no point is the string array empty.

Would you know what would cause it to be null?
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Old Jun 1st, 2006, 10:43 PM   #4
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
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.
Eric the Red is offline   Reply With Quote
Old Jun 1st, 2006, 10:44 PM   #5
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 903
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
This code is very inefficient as you read the entire file twice. You should instead use an ArrayList to store each line.
titaniumdecoy is offline   Reply With Quote
Old Jun 1st, 2006, 10:50 PM   #6
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
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.
Eric the Red is offline   Reply With Quote
Old Jun 1st, 2006, 11:05 PM   #7
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 903
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Old Jun 1st, 2006, 11:14 PM   #8
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
Quote:
Originally Posted by titaniumdecoy
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.
Alright thanks. I'll look into that.
__________________
Death smiles at us all. All a man can do is smile back.
Eric the Red is offline   Reply With Quote
Old Jun 1st, 2006, 11:16 PM   #9
Eric the Red
Hobbyist Programmer
 
Eric the Red's Avatar
 
Join Date: Feb 2006
Posts: 214
Rep Power: 0 Eric the Red is an unknown quantity at this point
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.
Eric the Red is offline   Reply With Quote
Old Jun 2nd, 2006, 12:05 AM   #10
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 903
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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.
titaniumdecoy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:23 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC