-1

I have the following little problem... I have this code that uses the method OpenFile() of one class ReadData to read a .txt file and also I have another class ArraysTZones used to create an object that stores 3 arrays (data1,data2,data3) and 3 integers (total1,total2,total3) returned by the method OpenFile(). The problem is that when I try to display each array (data1,data2,data3) using the method getArray() of ArrayTZones it stops and displays the error NullPointerException. Anyone knows how could I fix this?

public static void main (String args[]) throws IOException {
    String fileName = ".//data.txt";
    int[] def = new int[180];
    try {

        ReadData file = new ReadData(fileName);
        ArraysTZones summaryatz = new ArraysTZones();
        summaryatz = file.OpenFile();

        for (int i = 0; i < 180; i++)
            System.out.print (summaryatz.getArray1()[i] + " ");
        System.out.println ("");
        System.out.println (summaryatz.getTotal1());

        for (int i = 0; i < 180; i++) 
            System.out.print (summaryatz.getArray2()[i] + " ");
        System.out.println ("");
        System.out.println (summaryatz.getTotal2());

        for (int i = 0; i < 180; i++)
            System.out.print (summaryatz.getArray3()[i] + " ");
        System.out.println ("");
        System.out.println (summaryatz.getTotal3());
    }
    catch (IOException e) {
        System.out.println(e.getMessage());
    }
}

Heres OpenFile()

public ArraysTZones OpenFile() throws IOException {
    FileReader reader = new FileReader(path);
    BufferedReader textReader = new BufferedReader(reader);
    int numberOfTimeZones = 3;
    int[]  data1 = new int[180];
    int[]  data2 = new int[180];
    int[]  data3 = new int[180];
    int total1 = 0;
    int total2 = 0;
    int total3 = 0;
    ArraysTZones atz = new ArraysTZones();

    for (int i = 0; i < numberOfTimeZones; i++){
        if (i == 0) {
            String firstTimeZone = textReader.readLine();

            String[] val =  firstTimeZone.split ("\\s+");
            for (int u = 0; u < val.length; u++)
            {
                int stats =  (int)(Math.ceil(Math.abs(Double.parseDouble(val[u]))));
                total1 += stats;
                data1[u] = stats;
            }
            total1= total1/180;
            atz.setTotal1(total1);
            atz.setArray1(data1);
        }
        else
        if (i == 1) {
            String secondTimeZone = textReader.readLine();
            String[] val =  secondTimeZone.split ("\\s+");
            for (int u = 0; u < val.length; u++)
            {
                int stats =  (int)(Math.ceil(Math.abs(Double.parseDouble(val[u]))));
                total2 += stats;
                data2[u] = stats;
            }
            total2= total2/180;
            atz.setTotal2(total2);
            atz.setArray2(data2);
        }
        else {
            String thirdTimeZone = textReader.readLine();
            String[] val =  thirdTimeZone.split ("\\s+");
            for (int u = 0; u < val.length; u++)
            {
                int stats =  (int)(Math.ceil(Math.abs(Double.parseDouble(val[u]))));
                total3 += stats;
                data3[u] = stats;
            }
            total3= total3/180;
            atz.setTotal3(total3);
            atz.setArray3(data3);
        }
    }
    textReader.close();
    return atz;
}

The getArray()

public int[] getArray1 () {
    return data1;
}

And setArray()

public void setArray1 (int[] farray) {
    int[]  data1 = new int[180];
    //int[] farray = new int[180];
    data1 = farray;
}
xenteros
  • 15,586
  • 12
  • 56
  • 91
RedSkull
  • 47
  • 9
  • 1
    Without seeing the rest of your code, for `getArray()` and possibly your `OpenFile()` methods/classes we can only speculate as to the cause. Also the exception would be helpfull, as this should tell your exactly where the problem is occuring – Java Devil Sep 28 '14 at 22:26
  • My speculation is that your `getArray_` methods are returning `null`. – khelwood Sep 28 '14 at 22:27
  • 2
    try debugging your code. you will see which variable is null. and in debugging, don't use returned array directly like in your code summaryatz.getArray1()[i]. use it like this: for(...){ int arr[] = summaryatz.getArray1(); System.out.println(arr[i]); } – Adem Sep 28 '14 at 22:27
  • Can you add the exception stack trace (printStackTrace of Exception object), so the line throwing NullPointerException can be identified? – Sanket Parikh Sep 28 '14 at 22:31
  • I just added the methods ;)! – RedSkull Sep 28 '14 at 22:32
  • Sounds good! Can you edit the question and add the details getting printed from the e.printStackTrace() method? – Sanket Parikh Sep 28 '14 at 22:36
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – xenteros Sep 28 '16 at 11:46
  • Possible duplicate of [NullPointerException (Java)](http://stackoverflow.com/questions/30021160/nullpointerexception-java) – Tom Sep 28 '16 at 12:12

2 Answers2

1

The problem seems to be here

public void setArray1 (int[] farray)
{
    int[]  data1 = new int[180];
    //int[] farray = new int[180];
    data1 = farray;
}

You're declaring a new variable called data1 and storing the content of farray to it. After that method is done, that variable will be removed, due to his scope.

Remove int[] from the line int[] data1 = new int[180]; (or just remove the whole line .. it is unnecessary) and your data will be stored in the correct variable that was declared for the class.

public void setArray1 (int[] farray) {
    data1 = farray;
}
Tom
  • 16,842
  • 17
  • 45
  • 54
  • 1
    Assuming there is a class scope variable called `data1` of cause – Java Devil Sep 28 '14 at 22:38
  • http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – xenteros Sep 28 '16 at 11:47
  • @xenteros Like on the other answer: it can't be deleted since it has been accepted already (at least I can't delete it myself). Also there are much better dups for questions about shadowing issues, like: http://stackoverflow.com/questions/30021160/nullpointerexception-java – Tom Sep 28 '16 at 12:15
0

You have to initialize ArraysTZones

cozla
  • 137
  • 1
  • 11