Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Why wont this work?

Options
  • 13-12-2006 1:02am
    #1
    Registered Users Posts: 427 ✭✭


    Hey
    Can someone help me out here. I keep getting a null pointer ecxeption even tho the array is definitely big enough. I know its prob something simple but I just can't see it.
    BTW, this method is part of an ADT with ResultsArray is defined at the start
    public void printResults()
    {
    	ResultsArray.ensureCapacity(2000);
    	for (int p = 20;p < 50;p++)
    	{
    		if (ResultsArray.get(p)!=null)
    		{
    		            System.out.println(ResultsArray.get(p));
    	             }
    	 }
    }
    

    Thanks


Comments

  • Moderators, Music Moderators Posts: 25,868 Mod ✭✭✭✭Doctor DooM


    Is the nested if loop there running infinitely because you have no condition to halt it?


  • Registered Users Posts: 427 ✭✭Kevo


    I'm not sure what you mean.
    I thought it would stop when int p in the for loop reaches 50?


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    I'm not an expert on the language (in fact I have VERY little knowledge of it!) but from my experience elsewhere I'm pretty sure SDooM is right. Each looping operator needs parameters to initialise it, and also to break it. When is esultsArray.get(p) ever going to be null? You'll need to rethink that bit I imagine.


  • Registered Users Posts: 21,264 ✭✭✭✭Hobbes


    Ok first up you don't explain exactly what line it is crashing on. But this line looks wrong.

    ResultsArray.ensureCapacity(2000);

    Class names start with a capitals always, while variables start with lowercase.

    So is ResultsArray a static class? Or is it set elsewhere in the class.


  • Closed Accounts Posts: 209 ✭✭DublinEvents


    You will need to paste all your source code of that program for us to have a crack at figuring out the problem. That piece of code by itself looks to be ok.


  • Advertisement
  • Moderators, Science, Health & Environment Moderators Posts: 10,079 Mod ✭✭✭✭marco_polo


    Kevo wrote:
    Hey
    Can someone help me out here. I keep getting a null pointer ecxeption even tho the array is definitely big enough. I know its prob something simple but I just can't see it.
    BTW, this method is part of an ADT with ResultsArray is defined at the start
    public void printResults()
    {
    	ResultsArray.ensureCapacity(2000);
    	for (int p = 20;p < 50;p++)
    	{
    		if (ResultsArray.get(p)!=null)
    		{
    		            System.out.println(ResultsArray.get(p));
    	             }
    	 }
    }
    

    Thanks

    Looking at that code snippet, the only way you could possibliy get a null pointer exception is if ResultsArray has not been initialised properly and is a reference to null.

    Can you print more of the error message you get?
    Mark.


  • Registered Users Posts: 27,163 ✭✭✭✭GreeBo


    marco_polo wrote:
    Looking at that code snippet, the only way you could possibliy get a null pointer exception is if ResultsArray has not been initialised properly and is a reference to null.

    Can you print more of the error message you get?
    Mark.
    Yep, thats gotta be your problem
    also, as said above, you should start variables with lowercase letters as it will throw people off otherwise (its pretty much a standard)
    also, you are doing
    "ResultsArray.get(p)"
    twice, do it once, save it to something and then check to see if that thing is null. This is good practice and, while it may not be important in this case, someday ResultsArray.get() might involve a call to a DB in Tahiti.


Advertisement