• 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

My java application code gives java.lang.NullPointerException error while running program

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




I got this error Exception in thread "main" java.lang.NullPointerException
at com.jstl.bt.main(bt.java:39) in my program while running.....
Screenshot_1.png
[Thumbnail for Screenshot_1.png]
this is the error
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This line results in NullPointerException:That means len1 is null.
And you can not call methods on null references.
 
sarojni agrwal
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But in my case I did not initialize len1 = null so why it gives me error?
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have this:

If len1 is null then it means that rs.getBlob("Image"); is returning null. Is that what you get if the database column is empty? I'm not sure about that.
 
sarojni agrwal
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually Image column is not empty, but some of the indexes are null in database, like in image...
Screenshot_2.png
[Thumbnail for Screenshot_2.png]
some of indexes are null in Image column
Screenshot_3.png
[Thumbnail for Screenshot_3.png]
But others are filled with images..
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if some of the column values are null, then, yeah, you're going to get an NPE when trying to dereference a null. Your code need to be prepared to deal with null values.

And, surely you can come up with better variable names than len1 and len21?
 
sarojni agrwal
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
4-5 indexes are NULL, but after that indexes contains images, so why it gives NullPointerException? and I tried to remove NULL values by run the queries but it did not remove NULL values what should I do in that case?


1)DELETE FROM extra WHERE Image = "NULL";

2)ALTER TABLE extra ALTER COLUMN Image DROP DEFAULT;
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it is a valid use case that the 'Image' column can contain null, then you need to handle that possibility in your code. For example:

If the presence of null in the 'Image' column is considered a data integrity error then removing those rows is the right thing to do. The SQL for that would be:
 
Ranch Hand
Posts: 171
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can try debugging and inspecting the variable len1 in the ide to figure out what exactly was returned.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic