• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException  RSS feed

 
Marius Vasile
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would be very gratefull if you could find where is my problem and how can I fix it. (java.lang.NullPointerException)

here is my html from where I get a textfield and I upload 2 files

index.html



in this servlet I save the uploaded files into a specific folder and after that I am connecting to a database where I want to store the string got from html and the file paths from the uploaded files

UploadServlet.java - I selected only the code where the problem is



-------------------------

 
Vinodh Sa
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

can you tell us that which is line number 126 in your UploadServlet.java class ?
 
Marius Vasile
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is the second line from this code



I forgot to put these 2 lines, they are just before the other strings in my servlet
 
Vinodh Sa
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

I guess its pretty simple. You are getting the request parameter from the first line i.e. "String rid = request.getParameter("rid");"

And without checking it for null you are making another String variable "String idmarca = new String(rid);"

Its equivalent to the below line, which will obviously give you a compilation error
"String idmarca = new String(null);"

Add a null check to the variable "rid" before using it to store in another variable.

Hope that helps !!!
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, there's no point having a line like String idmarca = new String(rid). What's wrong with String idmarca = rid?
 
Marius Vasile
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, thank you for your time Vinodh Sa.

If I add a null check I get here ps.setString(1, idmarca); the error that idmarca cannot be resolved to a variable.
 
Vinodh Sa
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that means you have added the code like this


am i right ? and also as mentioned by Matthew, i dont see any point in your above code. Why not use the rid variable directly instead of creating another reference for the same String object.

And use in your set String code as below
 
Marius Vasile
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But what is the use to form string idmarca if I use rid into pst.getString(1, rid); ? Also I have the same NPE at the others strings that I use into pst.getString
 
Vinodh Sa
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all you have to make a null check for any variable before using and dont declare the string declaration inside that if null check block
not as below

instead do as below

 
Vinodh Sa
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and also thers no point in doing this new String while storing it into "idmarca"
instead of idmarca = new String(rid); use like idmarca = rid

and in this case there is literally no point in adding that null check.
instead you can store diretly rid into idmarca and make use of null check in set string method call while calling DB.

Dude, you gotta get strong in your java basics first, till then no one can help you.
 
Marius Vasile
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Vinodh Sa and Matthew Brown. It worked. I am new to Java programming, but I will learn. Have a nice day!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!