0

*The following code builds a "2D" array from strings in a text file. At present it is returning a NullPointException error on the line:

temp = thisLine.split(delimiter); My question is, am I correct in understanding that temp is returning null? If so, why, and how do I add a check for null? I'm rather new to Java, and this is my first attempt at creating a string array of arrays from a file.*

--------Edit--------

The above has been solved.

For those interested below is the code returning a IndexOutOfBoundsException. Specifically the line:

fileContents.set(i, fileContents.get(i).replace(hexLibrary[i][0], hexLibrary[i][1]));

System.out.println("SnR after this");

    String[][] hexLibrary;    // calls the replaces array from the LibToArray method
    hexLibrary = LibToArray();

    for(int i=0;i<502;i++){
        {
        fileContents.set(i, fileContents.get(i).replace(hexLibrary[i][0], hexLibrary[i][1]));
        }       
        }
    for (String row : fileContents) {
        System.out.println(row);        // print array to cmd
        }

______________________________


    public static String[][] LibToArray()
    {

        String thisLine;  
         String[] temp;  
         String delimiter=",";  
         String [][] hexLibrary = new String[502][2];  
    try
        {
            BufferedReader br= new BufferedReader(new FileReader("hexlibrary.txt"));  
            for (int j=0; j<502; j++) {  
                    thisLine=br.readLine(); 
                    temp = thisLine.split(delimiter);  
             for (int i = 0; i < 2; i++) {  
                hexLibrary[j][i]=temp[i];  
             }  
            }   
        }
        catch (IOException ex) {    // E.H. for try
        JOptionPane.showMessageDialog(null, "File not found.  Check name and directory."); // error message
        }
    return hexLibrary;
    }

3 Answers 3

1

It's more likely that thisLine is null. That will happen if you run out of input before 502 lines are read. If thisLine is not null, then thisLine.split(delimiter) will not return null. You should always check for a null line:

for (int j=0; j<502; j++) {  
    thisLine=br.readLine(); 
    if (thisLine != null) {
        temp = thisLine.split(delimiter);  
        for (int i = 0; i < 2; i++) {  
            hexLibrary[j][i]=temp[i];  
        }  
    } else {
        // report error: premature end of input file
        break; // no point in continuing to loop
    }
}

Personally, I'd write your method to not assume any particular file length:

public static String[][] LibToArray() {
    List<String[]> lines = new ArrayList<>();
    String delimiter=",";  
    try (BufferedReader br= new BufferedReader(new FileReader("hexlibrary.txt"))) {
        String line = br.readLine();
        while (line != null) {
            String[] tmp = line.split(delimiter);
            // the next line is dangerous--what if there was only one token?
            // should add a check that there were at least 2 elements.
            lines.add(new String[] {tmp[0], tmp[1]});
            line = br.readLine();
        }
    } catch (IOException ex) {
        JOptionPane.showMessageDialog(null, "File not found.  Check name and directory.");
    }
    String[][] hexLibrary = new String[lines.length][];
    lines.toArray(hexLibrary);
    return hexLibrary;
}

(The above uses the new Java 7 try-with-resources syntax. If you're using an earlier Java, you should add a finally clause that closes br before the method returns.

Sign up to request clarification or add additional context in comments.

3 Comments

Your null check structure worked. Now I've got an IndexOutOfBoundsException to deal with.
@InDeep - That's probably from a line that has no delimiters (and hence the array returned from split has fewer than 2 members).
it's a different section of code. Specifically the search structure that uses this array. I've added it in the OP if you're interested.
0

If the first line (or any line) of hexlibrary.txt is empty or is not delimited by ","s, the String array returned by split() will probably be null. To check for that, just add an if-condition around your second for loop, something like this: if (temp == null) { /* your loop here */ }

2 Comments

split() might return an empty array, but it will never return null.
A NullPointerException is thrown when an operation that needs to performed on an object, is performed on null. temp has nothing to do with the NPE in this case.
0

You are not checking for end of the stream while reading the file.

Method readLine returns a null if the reader reaches end of the stream. You are hitting this point (null) in the first for loop (before it exits), depending on number of lines in the text file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.