0

I have this Code I am getting NullPointerException error at the last line when I tried to check the size of the array but please note that the array is defined in the previous line any suggesstion?

`static class point {
    double x;
    double y;
    double z;
    }

    static class problem {
    int n; 
    point[] nodeptr;

    }

 static double att_distance(int i, int j)

    {
        Tsp.instance = new Tsp.problem();
        instance.nodeptr = new Tsp.point[Input.General_Inputs.Num_Of_Ppes];
        instance.nodeptr[i] = new Tsp.point();
        instance.nodeptr[i].x = InOut.General_Calculation.P_C[i];
        instance.nodeptr[i].y = InOut.General_Calculation.C[i];
        instance.nodeptr[i].z = InOut.General_Calculation.L[i];
        instance.nodeptr[j] = new Tsp.point();
        instance.nodeptr[j].x = InOut.General_Calculation.P_C[j];
        instance.nodeptr[j].y = InOut.General_Calculation.C[j];
        instance.nodeptr[j].z = InOut.General_Calculation.L[j];

    double xd = (instance.nodeptr[i].x + 0.6*instance.nodeptr[i].y)*Math.pow(instance.nodeptr[i].z,-1);
    double yd = (instance.nodeptr[j].x + 0.6*instance.nodeptr[j].y)*Math.pow(instance.nodeptr[j].z,-1);
    double rij = ((xd + yd));
    return rij;
    }

    static double[][] compute_distances()

    {
    int i, j;
    double matrix[][] = new double[n][n];
    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            matrix[i][j] = att_distance(i, j);
        }
    }
    return matrix;
    }
static problem instance;    
static int n=Input.General_Inputs.Num_Pipes_Selected-1;

    Tsp.instance.distance = Tsp.compute_distances();
    System.out.println("Tsp.instance.distance.length          "+Tsp.instance.distance.length);

I get this error I tried to check on a solution on the net but I couldn't find a solution

Exception in thread "main" java.lang.NullPointerException

1 Answers1

0

variables inside static class also should be declared as static

  • 1
    No, in most cases definitely not. – Kayaman Mar 05 '15 at 07:58
  • @Kayaman if it is not, why did not you post up your answer so at least, I could learn? it is confusing to me how the op accepted the answer and your comment said the different. :( – Kick Buttowski Mar 05 '15 at 17:12
  • 1
    @KickButtowski I didn't post an answer because I didn't want to go through the question, but I knew outright that this answer was wrong, so I commented on it. I don't know why the OP accepted this answer, maybe it just happened to fix his problem or maybe it's a sock puppet account designed to give Sandeep Kumar more points. – Kayaman Mar 06 '15 at 08:32