0

I am trying to make an Arraylist with its [0] element and [1] element a string and the others doubles. I want to use these arrays for my coordinates in my other classes.

I get an NullPointerException, what am i doing wrong?

enter code here

public class ExampleTextInArrayReader
{
    //naam document van ECG-patiént 1
    public static final String FILE_NAME = "ecg1.txt";
    //array waarin de String van ECG-waarden is omgezet in een ArrayMetDoubles
    private Double[] arrayMetDoubles = new Double[722-2];
    private String[] lines, gegevensPatiënt;
    //velden voor patientnummer en de bijbehorende afdeling
    public void main()
    { 
        // Open the file
        TextInArrayReader in = new TextInArrayReader(FILE_NAME);

        // Check for errors during opening file
        if (in.getError() != null) 
        { 
            System.out.println("Error opening file: " + in.getError());
            System.exit(1);
        }

        // Read the file
        String[] lines = in.getLines();  
        if (in.getError() != null) // check for errors during reading file
        { 
            System.out.println("Error reading file: " + in.getError());
            System.exit(1);
        }

        // Print file contents
        for (int i = 0; i < lines.length; i++) System.out.println(lines[i]);
    }

    public String[] drukGegevensPatiëntAf()
    {
        gegevensPatiënt = new String[2];
        for(int i = 0; i < 2; i++)
        {
            gegevensPatiënt[i] = lines[i];
        }
        return gegevensPatiënt;
    }

    public void changeArray()
    {
        // Get the ECG-values from the lines array and change them in doubles and put them in an arrayMetDoubles array
        arrayMetDoubles = new Double[lines.length - 2];
        for(int i = 2; i < arrayMetDoubles.length; i++)
         {        
            arrayMetDoubles[i] = Double.parseDouble(lines[i + 2]);
        }
    }

    public Double [] getLijnen()
{
    changeArray();
    return arrayMetDoubles;
}

}

5
  • WHere are you getting the exception? Commented Dec 17, 2013 at 15:12
  • java.lang.NullPointerException at ExampleTextInArrayReader.changeArray(ExampleTextInArrayReader.java:48) Commented Dec 17, 2013 at 15:14
  • Is this your complete program? Commented Dec 17, 2013 at 15:16
  • No it is 1 particular class of my package. But it should compile without the other classes. Commented Dec 17, 2013 at 15:16
  • It might compile but it wont run without a main method Commented Dec 17, 2013 at 15:18

2 Answers 2

1

Becuase the class member varaible lines is not yet assigned.In the Main method you have assingned to the local variable

 String[] lines = in.getLines(); 

Change the above line to

    lines = in.getLines(); 

in Main() method.

And Make sure the Main() method called before changeArray Method .

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

Comments

0

I like how you mixed Nederlands with English throughout your code! :)

Anyway, in the for loop, loop from 0 to length-2... Or else the code will fill ArraymetDoubles from index 2 to two places outside the array

public void changeArray()
{

    arrayMetDoubles = new Double[lines.length - 2];
    for(int i = 0; i < arrayMetDoubles.length; i++)
    {        
        arrayMetDoubles[i] = Double.parseDouble(lines[i + 2]);
    }
}

3 Comments

My problem still isn't solved. I get another NullPointerException:java.lang.NullPointerException at ExampleTextInArrayReader.changeArray(ExampleTextInArrayReader.java:48) Now my array (arrayMetDoubles) isn't even receiving the doubles in the lines[i]
Verdorie! Maybe you can double-check if the arrayMetDoubles variable and the lines-variable are initialized and set correctly? if Lines == NULL, Lines.length will throw throw that error I think...
Problem is solved. Thanks very much! My question to you now is how do i use those array-values assigned to an array in my ExampleTextInArrayReader into another class in which i can calculate different x,y-coordinates and put them into another Double[] xcoordinate, ycoordinate?

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.