Reputation: 13
ok, so here is the deal. I have made a program that creates a byte array that has 150000000 different addresses. I then put that into a for loop that will assign the values a random positive number between 0-9 and it outputs that number to a file. that all works but where i'm running into problems is where I actually try to square it. im using the class BigInteger to try to store the number im trying to square. this is my code to try to put the number into a BigInteger
for(int i = 0; i<150000000; i++)
{
square = square.add(BigInteger.valueOf(number[i]));
}
and my compiler at run time gives me the error: Exception in thread "main" java.lang.NullPointerException at numberge.Numberge.main(Numberge.java:44) Java Result: 1
I have also tried to make the for loop iterate a smaller amount of times and it does not help. any ideas?
the entire output that the program gives me is: run: done generating/now writing to file squaring 150000000 Exception in thread "main" java.lang.NullPointerException at numberge.Numberge.main(Numberge.java:43) Java Result: 1 BUILD SUCCESSFUL (total time: 9 seconds)
the entire code is
import java.util.Random;
import java.io.*;
import java.math.BigInteger;
public class Numbersqu {
static byte[] number = new byte[150000000];
static Random generator = new Random(System.currentTimeMillis());
static BigInteger square = null;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException
{
for(int i = 0; i<150000000; i++)
{
number[i] = (byte) Math.abs(generator.nextInt(9));
}
System.out.println("done generating/now writing to file");
File file = new File("number.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
for(int i = 0; i<150000000; i++)
{
bw.write(Integer.toString(number[i]));
}
bw.close();
System.out.println("generating number to square");
System.out.println(Integer.toString(number.length));
for(int i = 0; i<150000000; i++)
{
square = square.add(BigInteger.valueOf(number[i]));
}
System.out.println("Writing square to file");
File file2 = new File("square.txt");
if (!file2.exists())
{
file2.createNewFile();
}
FileWriter fw2 = new FileWriter(file2.getAbsoluteFile());
BufferedWriter bw2 = new BufferedWriter(fw2);
bw2.write(square.toString());
bw2.close();
}
}
this is as far as I have gotten, once I can actually store the number then I will square it.
Upvotes: 0
Views: 696
Reputation: 2155
So here it is. Your variable square is null.
static BigInteger square = null;
Initialize it to some value before using here :
for(int i = 0; i<150000000; i++)
{
square = square.add(BigInteger.valueOf(number[i]));
}
Upvotes: 1
Reputation: 70929
I can see two possible issues, but with this code snippet, not enough information is presented to really know either of these items are the true problem.
Perhaps square is not initalized. If so, the
square = square.add(...
will dereference a null pointer at the square.add(...)
call, causing a null pointer exception.
To fix such an item square = new Square(); should be called beforehand.
number
is not initialized.fixing it is the same as fixing square
.
number[i]
lacks a value. This happens quite often when dealing with arrays of objects.Upvotes: 0