Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Quick question.. really Quick (http://www.programmingforums.org/showthread.php?t=10128)

Eric the Red Jun 1st, 2006 9:29 PM

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?

titaniumdecoy Jun 1st, 2006 9:35 PM

fileData[i] is null.

Eric the Red Jun 1st, 2006 9:40 PM

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?

Eric the Red Jun 1st, 2006 9:43 PM

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 ();


titaniumdecoy Jun 1st, 2006 9:44 PM

This code is very inefficient as you read the entire file twice. You should instead use an ArrayList to store each line.

Eric the Red Jun 1st, 2006 9:50 PM

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;
            }

        }

    }


titaniumdecoy Jun 1st, 2006 10:05 PM

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.

Eric the Red Jun 1st, 2006 10:14 PM

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.

Eric the Red Jun 1st, 2006 10:16 PM

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 ();


titaniumdecoy Jun 1st, 2006 11:05 PM

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.


All times are GMT -5. The time now is 1:41 AM.

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