1

I have to read some information from text file to different String arrays. So, I wrote a method for this task. Here is the code:

private String[] stateNames;
.
.
.
readHelper(stateNames);
.
.
.
private void readHelper( String[] str ) // str is stateNames
    {
        try
        {
            strLine = bufferReader.readLine();
            String[] tokens = strLine.split(",");
            str = new String[ tokens.length ];
            copyStr2Str(tokens, str);
            insertionSort( str ); // Everything is fine, str contains the data
        }
        catch (IOException ex)
        {
            Logger.getLogger(Generator.class.getName()).log(Level.SEVERE, null, ex);
        }

    } // End of readHelper()

However after readHelper() is executed stateNames becomes null. I think, I cause some pointer errors.

2 Answers 2

3

In Java, arguments are passed as a copy of the reference.

When you do,

str = new String[ tokens.length]

you discard the reference passed by the method call and create a new one (which only lives until the end of the try scope in which was created). The original stateNames keeps pointing to the original value, which was null.

Modify the method to return the str array and assign it to stateNames.

UPDATE to answer comment:

To understand this, try this code.

public class MyClass {
    private List<String> originalList1 = new ArrayList<String>();
    private List<String> originalList2 = new ArrayList<String>();

    public void run() {
       this.test(originalList1, originalList2);
    }

    public void test(List<String> list1, List<String> list2) {
       list1 = new ArrayList<String>();
       list1.add(new String("Hello"));
       list2.add(new String("World"));
       System.out.println("list1.size -> " + list1.size());
       System.out.println("originalList1.size -> " + originalList1.size());
       System.out.println("list2.size -> " + list2.size());
       System.out.println("originalList2.size -> " + originalList2.size());
    }
  }

Since you assign a new object to list1, the original reference is lost so the original object is not affected (but with list2, you are still using the original object). Since in your code you create a new String[], you no longer work in the original one.

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

1 Comment

Returning "str" won't cause a future "nullptrexception", right?
1

You should return the array as a return value of the method.

private String[] readHelper()
    {
        String[] str = null;
        try
        {
            strLine = bufferReader.readLine();
            String[] tokens = strLine.split(",");
            str = new String[ tokens.length ];
            copyStr2Str(tokens, str);
            insertionSort( str ); // Everything is fine, str contains the data
        }
        catch (IOException ex)
        {
            Logger.getLogger(Generator.class.getName()).log(Level.SEVERE, null, ex);
        }
        return str;
    } // End of readHelper()

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.