Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 java.lang.NullPointerException
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
beno




PostPosted: Wed Feb 18, 2009 8:48 pm   Post subject: java.lang.NullPointerException

Java:
import java.io.*;
import java.util.*;


public class S2
{
    static int number[];

    public static void main (String[] args) throws IOException
    {
        BufferedReader input = new BufferedReader (new FileReader ("INPUT.txt"));
        PrintWriter output = new PrintWriter (new FileWriter ("OUPUT.txt"));
        String r = input.readLine ();
        int n = 0;
        while (r != null)
        {
            number [n] = Integer.parseInt (r);
            r = input.readLine ();
            n++;
        }
        double check;
        int count;
        for (int m = 0 ; m < number.length ; m++)
        {
            count = 0;
            for (int i = 0 ; i <= number [m] ; i++)
            {
                double x = Math.pow (i, 2);
                for (int j = 0 ; j <= number [m] ; j++)
                {

                    double y = Math.pow (j, 2);
                    check = Math.sqrt (x + y);
                    if (check <= number [m])
                        count++;

                }
            }
            output.println (count);
        }
        output.close ();
        input.close ();
    }
}




ok, here is my program, the input are:
2
3
0
However, everytime when i try to run this program, it returns the error:

java.lang.NullPointerException
at S2.main(S2.java:17)

i looked over this program for several times, but i can't find any problem
does anyone know why Question

mod edit: remember to use syntax or code tag when posting code next time.
Sponsor
Sponsor
Sponsor
sponsor
HeavenAgain




PostPosted: Wed Feb 18, 2009 8:57 pm   Post subject: RE:java.lang.NullPointerException

Always look at the exception/error you are getting and also the line number where the error/exception occured.

So in this case, line 17 in S2.java, the main method throws an null pointer exception.

Line 17: "number [n] = Integer.parseInt (r);"

What that mean is something is null here (unassigned, empty). So looking back and checking your code, you'll notice, "oh hey, what is the number array? maybe it's null, it was never assigned" doh! Smile

ps. for your future references, know your error/exception messages, know why they occur, so you can fix them.
Dark




PostPosted: Sat Feb 21, 2009 6:52 pm   Post subject: RE:java.lang.NullPointerException

hmm you have
Java:
static int number[];


NullPointerException is caused by a null variable or whatnot, so try assigning your array to be full:

Java:
int number[] = new int[10];


Or however many numbers you need. if you declare and initialize like that it should fill the arrays with each number being equal to zero.
TheFerret




PostPosted: Sat Feb 21, 2009 9:04 pm   Post subject: RE:java.lang.NullPointerException

I believe in Java that the code to initialize arrays will not fill them and they still will throw a NullPointerExcpetion if you tried to use it, so you would have to use a for loop and make each cell of the array equal to 0...

Edit: Or better yet, use Arrays.fill(array, value) to fill the array with a value to start with...

Edit2: So it turns out that arrays are autopopulated with 0's now, so ignore what i said unless you want your array to not have 0's stored in them at the start...
Dark




PostPosted: Thu Feb 26, 2009 9:34 pm   Post subject: RE:java.lang.NullPointerException

Yes it's a series of integers so default is 0
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: