-2

I am working on a simple program to calculate a mathematical equation. But there is a problem that I could not find. Any help would be greatly appreciated.

It seems a problem is with

alpha[j] = (double)(j-1)*2*Math.PI/(double)rotationNum;

NullPointerException is returned. There has to be some silly mistakes here.

import java.util.*;
import java.io.*;
//import Jama.Matrix;

class efun {
    static double epso;
    static double sigma;
    static double alpha[];
    static double charge;
    static double axisR;
    static double axisZ;
    //static Random randGen;
    static int numPoints = -1;
    static int rotationNum;


    public static void main (String[] args) {
        try {
            sigma = 300e-6*1e2; 
            epso = 8.854e-12; 

            /*Input arguments*/
            numPoints = Integer.parseInt (args[0]);
            FileReader fr = new FileReader(args[1]);
            rotationNum = Integer.parseInt (args[2]);
            BufferedReader br = new BufferedReader(fr);         
            double pointsR[] = new double[numPoints];
            double pointsZ[] = new double[numPoints];
            double chargeDensity[] = new double[numPoints];
            double electricField = 0.0;
            double ER = 0.0;
            double EZ = 0.0;
            double EY = 0.0;

            for (int id = 0; id < numPoints; id++) {
                // read file
                while ( (line = br.readLine() )!= null) {
                    StringTokenizer stk = new StringTokenizer(line);
                    axisR = Double.parseDouble(stk.nextToken());
                    axisZ = Double.parseDouble(stk.nextToken());
                    charge = Double.parseDouble(stk.nextToken());

                    pointsR[id] = axisR;
                    pointsZ[id] = axisZ;
                    chargeDensity[id] = charge;
                    System.out.println("axisR: "+pointsR[id]+" and axisZ: "+ pointsZ[id]+"; its corresponding charge density is: "+ chargeDensity[id]);

                    double rotatedR[] = new double[numPoints];
                    double rotatedZ[] = new double[numPoints];
                    double rotatedY[] = new double[numPoints];
                    double sumSquarePoints[] = new double[numPoints];

                    for (int j = 1; j < rotationNum+1; j++) {                   
                        alpha[j] = (double)(j-1)*2*Math.PI/(double)rotationNum; 
                        System.out.println("print alpha: "+alpha[j]);
                        rotatedR[id] = pointsR[id] - Math.cos(alpha[j])*pointsR[id];
                        rotatedZ[id] = pointsZ[id];
                        rotatedY[id] = pointsR[id] - Math.sin(alpha[j])*pointsR[id];

                        sumSquarePoints[id] = Math.sqrt(rotatedR[id]*rotatedR[id] + rotatedZ[id]*rotatedZ[id] + rotatedY[id]*rotatedY[id]);

                        ER += chargeDensity[id]*rotatedR[id]/(sumSquarePoints[id]*sumSquarePoints[id]*sumSquarePoints[id]);
                        EZ += chargeDensity[id]*rotatedZ[id]/(sumSquarePoints[id]*sumSquarePoints[id]*sumSquarePoints[id]);
                        EY += chargeDensity[id]*rotatedY[id]/(sumSquarePoints[id]*sumSquarePoints[id]*sumSquarePoints[id]);

                        System.out.println ("ER is: "+ ER);
                        System.out.println ("EZ is: "+ EZ);
                        System.out.println ("EY is: "+ EY);
                    }
                }
            }

            electricField = sigma/(4*Math.PI*epso)*Math.sqrt(ER*ER + EZ*EZ + EY*EY); 
            System.out.println("electricField is: " + electricField);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
5
  • 2
    what does the stacktrace tell you?
    – Reimeus
    Aug 27, 2015 at 21:22
  • NullPointerException is returned as stated in the question.
    – Orangeblue
    Aug 27, 2015 at 21:23
  • 2
    Have you defined alpha ? Also the stacktrace has more info than just NPE.. it has a line number apart from other info.
    – Sid
    Aug 27, 2015 at 21:24
  • Just glancing at the code this for (int j = 1; j < rotationNum+1; j++){ looks odd. Why rotationNum+1. Are you confident that's what you meant to do? Again not looking at it hard so may well be completely fine.
    – jwpfox
    Aug 27, 2015 at 22:12
  • It is ok. It can be either j=0; j<rotationNum or the one above. Thanks though.
    – Orangeblue
    Aug 28, 2015 at 3:10

4 Answers 4

2

You never initialized alpha, you only declared it, so you can't access alpha[j]. Initialize it and make sure that its size is large enough for every j:

alpha = new double[MY_SIZE];

Also, make sure that you're passing in at least 3 arguments to main so that rotationNum is assigned correctly.

1

Your class variable alpha is declared, but not initialized, so Java gives it the default value of null. The variable was never initialized to any array.

static double alpha[];

However, it doesn't look like you're using any other intended value in the array except for the current value. Just declare it to be a local double (not an array), and use it as a normal variable.

double alpha = (double)(j-1)*2*Math.PI/(double)rotationNum;

And use alpha instead of alpha[j] a few lines down from there.

1
  • Thanks a lot! Also thanks for every one who has input here!!
    – Orangeblue
    Aug 27, 2015 at 21:42
0

You've never initialized alpha[]. Just like pontsR, pointsZ, and chargeDensity, you need to point alpha at a new array of doubles before you can use it.

0

Add alpha = new double[rotationNum ]; after getting rotationNum

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.