• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NullPointerException, sigh

 
Ranch Hand
Posts: 574
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I have this code that works just fine.

This is excerpted from 2 much larger files I don't want to post here (size, not becauseI'm still a lousy Java programmer).

The problem is, getNull() is a method in a different file that will either return an int[], or null.  It's working fine.  But when I do the "if(keys != null)" then the run time throws a NullPointerException.  Um, yeah, I'm kinda expecting null, that's why I test for it.  Why would it fail when I test for it?  Especially after I can write the code above that works fine?
 
Jim Venolia
Ranch Hand
Posts: 574
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I should point out it's more like:

in the real program.
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found your explanation hard to follow.

For a start please post stack trace (copy/paste).
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim,
Can you show a small example that shows the error. It's hard to speculate about the cause.

Without more info, my guess is that you have Integer somewhere in the code. Because Integer can be null. And it throws a null pointer when autoboxed into an int.
 
Jim Venolia
Ranch Hand
Posts: 574
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fail to see how it helps, considering I haven't posted the 2 offending source files.  But here goes:

Set1Challenge4.doLine is the "if(keys != null)" line.  Directly above it in live code is the "System.out.println("Keys: " + keys);" line.

 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I meant can you create a small example that illustrates the problem.
 
Jim Venolia
Ranch Hand
Posts: 574
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, just noticed you are an author on a book I bought last week.  I feel honored, and for whatever reason Evi Nemeth springs to mind.  Please tell me you'll stay away from small floating thingies outside of sight of land.

Also had contact with Richard Stevens.  Sent him an email asking if I should buy one of his books or if he had an update coming out.  He recommended I wait.

My history of published authors and their outcome isn't great.  Thank $diety I've only met Wil Weaton in person, never via email nor forums.

Dang, that's dark.  Keep writing books, answering my stupid questions, and stay out of small floating thingies outside of sight of land.
 
Jim Venolia
Ranch Hand
Posts: 574
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aaaaand, I'm a dumass.  I did post a sample.  The sample works.  The real life code doesn't but it's gonna be 3-400 lines over 3-4 files.  Which, much like Opus's FannyFloat, you don't want to see.


 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree that I don't want to see 300 lines of code! Unfortunately, the first act of troubleshooting is to start removing code that doesn't contribute to the problem. At some point, you have a small example or you know one line of code is the trigger.  You did recompile all this code at the same time, right? I'm starting to wonder if one of the class files is stale/doesn't match what you are expecting.

Like Evi, I'm from NY. Unlike her, I promise not to go on a mysterious boat adventure.
 
Jim Venolia
Ranch Hand
Posts: 574
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I build my app with a Makefile.   Nothing is stale.  otherClass.getNull() returns null more often than not.

Is there a better debugger than Eclipse?  Considering I think debugging with printf is better than Eclipse that's a pretty low bar.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I Like Eclipse's debugger so I'm not good to ask. I particularly like conditional breakpoints. Adding this post to the IDEs forum so you can get more opinions on that!
 
Jim Venolia
Ranch Hand
Posts: 574
VI Editor Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My problem with Eclipse is most of my data structures are arrays, which I'd like to see as a data dump instead of a long list of "data[0] = '0'.\n  data[1] = '1; \n".  I know there are workarounds for it, but it kinda sucks.

There are also issues with breakpoints.  They hang around forever, and I'm always deleting breakpoints for problems I've fixed a few days ago.

Then again, I haven't used Eclipse in over a year so my memory is foggy.

I have Android Studio, but my CPU doesn't support the debugging instructions they need.  So I have to run my app on my phone, when my app was never designed to run under Android.

All that said, have some code.  Won't compile, but it's the relevant parts of my code.

I fail to see why it would crash on the 'if(keys != null)' line.  I don't even.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this as an alternativeThat line won't produce a null pointer exception in either version. The left String shou‍ld read “D'oh!” surely? The way you have written it, with != rather than ==, it is difficult to read.

If the code won't compile, please sort out the compiler error (or show us it) first.
 
Jim Venolia
Ranch Hand
Posts: 574
VI Editor Chrome Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After doing some code cleanup and running findbugs the problem has gone away.  I still don't know how I was getting that exception when testing the reference for null, but it's working now.
 
Jim Venolia
Ranch Hand
Posts: 574
VI Editor Chrome Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Completely unrelated, but figuring out a different problem read a post that said "in case of error don't return null, return a 0 sized array".  Sounds like great advice, I've changed my code to do that.

I must be doing good, I'm replying to a 4 day old post and have been spending 4-5 hours a day writing Java
 
Evacuate the building! Here, take this tiny ad with you:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic