nullpointerexception error with random
SO I have the following code
public void rand(int N){
double[]x=new double[N];
double[]y=ne开发者_如何学Gow double[N];
double[][]res=new double[N][N];
for(int i=0;i<N;i++){x[i]=random.nextDouble();y[i]=random.nextDouble();}
}
but the it would return the nullpointerexceptionerror on the for loop....can anybody tell me what's wrong with it?
Where did you initialize random? I don't see it.
You'll have another problem: the x and y arrays and the res matrix are declared, initialized, and immediately go out of scope when you leave the method. All that work is wasted.
I'd think about it more like this:
import java.util.Random;
/**
* MatrixTest
* @author Michael
* @since 2/20/11
*/
public class MatrixTest
{
private static final int DEFAULT_SIZE = 3;
private double [] x;
private double [] y;
private int n;
public static void main(String[] args)
{
MatrixTest m = new MatrixTest();
System.out.println(m);
}
public MatrixTest()
{
this(DEFAULT_SIZE);
}
public MatrixTest(int n)
{
this.init(n);
}
public void init(int n)
{
Random random = new Random(System.currentTimeMillis());
this.x = new double[n];
this.y = new double[n];
this.n = n;
for (int i = 0; i < this.n; i++)
{
x[i] = random.nextDouble();
y[i] = random.nextDouble();
}
}
@Override
public String toString()
{
final StringBuilder sb = new StringBuilder();
sb.append("MatrixTest");
sb.append("{x=").append(x == null ? "null" : "");
for (int i = 0; x != null && i < x.length; ++i)
{
sb.append(i == 0 ? "" : ", ").append(x[i]);
}
sb.append(", y=").append(y == null ? "null" : "");
for (int i = 0; y != null && i < y.length; ++i)
{
sb.append(i == 0 ? "" : ", ").append(y[i]);
}
sb.append(", n=").append(n);
sb.append('}');
return sb.toString();
}
}
random
seems to be the only object in that code snippet, everything else is a primitive. Where's random
being instantiated?
As far as I can tell everything looks fine except for the random variable? Where is it defined? Also, what is the error message?
Based on this my suspicion is that the random variable is uninitialised.
Is it possible that random
is null
?
I don't see a declaration for random
. I assume it is an instance variable declared in the enclosing class. If so, I suspect that random
has not been initialized with a reference to a Random
object. When the ... = random.nextDouble();
statement is executed with a null
object reference, the result is an NPE.
By the way, if you fixed the egregious style issues with the loop statement, the source of your problem would be easier to spot.
for (int i = 0; i < N; i++) {
x[i] = random.nextDouble(); // NPE on this line
y[i] = random.nextDouble();
}
Good style is best practice. Spending a few seconds to format your code properly can save minutes when you / someone else reads it, and hours (or even days) if your bad style is obscuring a bug ... like this:
for(i=0;i<N;i++)x[i]=random.nextDouble();y[i]=random.nextDouble();
(OK ... the "fatal" style-related mistakes in the above example are not ones that you made. But that doesn't invalidate my point.)
精彩评论