• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException in my array  RSS feed

 
Christina Bremmerman
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I keep leaving this and coming back to it but I am still missing where the error is. I thought it was somewhere in my toStringSorted()
I thought that maybe I copied the array into my sorted array wrong so I changed my sortAccountArray(sortAccounts) to (theAccounts) but it still gave me the error
Can anyone see what I'm missing?
Thanks!

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which line is causing the NPE? Look at everything you dereference on that line, and try to trace back to where it's getting set to non-null. If you can't find the problem that way, add lines to print out each of those things right before the NPE line. If you still can't figure it out, add more print statements until you can see exactly what's going on. If you still can't figure it out, post again, with details.
 
Christina Bremmerman
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh oops I forgot to add that, sorry.

The line causing it is this one:



Also thanks for the advice on the printing, I never thought to do that with an array, I'm doing that now so hopefully I can find it.
These arrays are kicking my butt
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christina Bremmerman wrote:Oh oops I forgot to add that, sorry.

The line causing it is this one:



So at least one of the following is null:
  • accountArray
  • i, if it's an Integer rather than an int primitive
  • accountArray[i]
  • minIndex, if it's an Integer rather than an int primitive
  • accountArray[minIndex]


  • Also thanks for the advice on the printing,


    You're welcome. And i would print them in the order I gave above. After all, if it's accountArray that's null, and you try to print accountArray[i] first, you'll an NPE just from trying to print, which kind of defeats the purpose.
     
    Christina Bremmerman
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well it turns out this was a biigger problem than I thought.
    I didn't write the code the assigment is just to add a few methods to sort the accounts, and I had read one of the methods as creating the array as well as the object, but it turned out that I was only creating the objects.




    ...and I removed the rest because it made me look dumb haha...
     
    Christina Bremmerman
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Actually...I found some more bigger problems...so scratch I'll that and I'll be back when I have something constructive to add.
     
    Christina Bremmerman
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Wow...so haha I fixed a bunch of things, my array is getting created with the objects and it looks like my array is disappearing. Could someone explain to me what may be causing me to lose my array? it gets lost if I send theAccounts to my sortAccount() as well but I can print from both of them after the for loop closes....or if I completely take out sortAccountArray(sortAccounts); everything runs fine, it just isn't sorting the second array



    I'm guessing I'm missing something in the actual method....but I'm not sure what to look for exactly...or how to look for it...I've checked this against my notes and nothing is sticking out as wrong. Also if there are any suggestions on what to look for in general? I realize this may not be happening where I think it it, but these are the only places in my code that seem to be causing problems

     
    Christina Bremmerman
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I feel so close but I do believe I should give my brain a rest and come back to this....last note though...I stepped through the sortAccountArray method and now I am thinking I need to go over my compareTo method(in a separate class)..
    also I hope no one minds me posting so much on here...it's actually helping my thought process a little bit and when I come back to it I think that it'll help to bring me back....


     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It's hard to say for sure what's going on without seeing complete code--or better yet, and SSCCE that has just enough code to demonstrate the problem and nothing unrelated to it.

    However, if you feel a variable's value is "getting lost," it's probably one of the following:

    1) You have a local variable that's hiding a member variable.



    2) You have two local variables in two different methods, but no member variable. Locals cease to exist when their method ends, and even if two methods have locals with the same name, they're two completely unrelated variables.

    3) You're properly using a member variable, and not hiding it, but you have two different enclosing instances.



     
    Christina Bremmerman
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for all your help Jeff! Thanks for that link too, I had a lot of code so I didn't want to post everything, so I was trying to narrow it down to keep anyone from having to read too much, but now I see there's a better way to do that

    Turns out I was copying the whole array even though there were only 15 spaces with data in them, so when it was trying to sort the copied array it was hitting the null values!
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Cool. Glad you got it figured out!
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!