• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException when calling connection Object

 
Alireza Azadi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All java Lovers, that im one of them, that will improve my ability and knowledge.
I know here is java Forum, but if somebody can help me, please give me solution.
Im searching about two days and still no success.

I have a android project in which i want to connect SQL Server 2012.
First of all,i searched and googled ,but no success.
I appreciate any help.
Before i connected to SQL Server 2005 and everything was OK.
Now i want to connect SQL Server 2012.
First of all, i went to SQL Server Configuration Management and enabled TCP/IP protcols for all IPs, and all listen to port # 1433.
And also enabled Named Pipes.
Then i go to firewall and add sqlservr.exe to programs(for dynamic IP)and also add port 1433 (TCP and UDP) for any connection.
My instance Name is default(MSSQLSERVER).
after i went to my android app,and add these lines to AndroidManifest.



OK, and then i went to my ConnectionHelper.java class and give this Connection String :



and also added these codes before this :



I use jTDS 1.3.0.
In MainActivity i create new instance of my ConnectionHelper class and give return connection to a CallableStatement.
It gives me RunTimeException ( I think because i call it in onCreate method of MainActivity) and NullPointException in the line :


I think because my ConnectionHelper class returns null,so the getConnection is null,so callableStatement can not be compiled and returns nullPointException.
And it returns null cause of my Connection String,am i right?

Can anybody help me with this?

I tried different Connection Strings but still no success.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alireza Azadi,

First of all, a warm welcome to CodeRanch!

Alireza Azadi wrote:I use jTDS 1.3.0.
In MainActivity i create new instance of my ConnectionHelper class and give return connection to a CallableStatement.
It gives me RunTimeException ( I think because i call it in onCreate method of MainActivity) and NullPointException in the line :

You think you get a runtime exception Could you share the stack trace of the exception?

Kind regards,
Roel
 
Alireza Azadi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Alireza Azadi wrote:I use jTDS 1.3.0.
In MainActivity i create new instance of my ConnectionHelper class and give return connection to a CallableStatement.
It gives me RunTimeException ( I think because i call it in onCreate method of MainActivity) and NullPointException in the line :

You think you get a runtime exception Could you share the stack trace of the exception?


Here is what android-studio says ! :

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.t_o_o.padrapars/com.example.t_o_o.padrapars.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.t_o_o.padrapars.MainActivity.onCreate(MainActivity.java:73)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5299)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
            at dalvik.system.NativeStart.main(Native Method)


The line # 73 is :
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what's conn? And what's getMyConnection returning?
 
Alireza Azadi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:And what's conn? And what's getMyConnection returning?


Here is my ConnectionHelper class that conn is an instance of it :

 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where in that code are you actually setting the connection attribute?
It's not happening in your constructor.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Where in that code are you actually setting the connection attribute?
It's not happening in your constructor.

I believe the connection is set using this line of code in the constructor
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alireza Azadi wrote:Here is my ConnectionHelper class that conn is an instance of it :

And is connection actually initialized or does the try-block throws some exception?

What's the output if you add this statement as the final line of the ConnectionHelper constructor?
 
Alireza Azadi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Where in that code are you actually setting the connection attribute?
It's not happening in your constructor.


As @Roel De Nijs said in my constructor i did that.
 
Alireza Azadi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Alireza Azadi wrote:Here is my ConnectionHelper class that conn is an instance of it :

And is connection actually initialized or does the try-block throws some exception?

What's the output if you add this statement as the final line of the ConnectionHelper constructor?


Roel De Nijs wrote:
Alireza Azadi wrote:Here is my ConnectionHelper class that conn is an instance of it :

And is connection actually initialized or does the try-block throws some exception?

What's the output if you add this statement as the final line of the ConnectionHelper constructor?


I made an instance of my Class.
Here is my MainActivity class.
It is onCreate method that i made and get NullPointException.




The method returns nothing.
And in debug mode when i create a break point at line debugger says conn is null !
Which means returns nothing.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alireza Azadi wrote:The method returns nothing.

I'm not familiar with Android development, so no idea how you can easily add a few log statements. Let me ask another question: if you add that line to the constructor and you put a breakpoint on that line and start debugging, what's the value of connection? I assume it's null because something went wrong in the try block.

Alireza Azadi wrote:And in debug mode when i create a break point at line debugger says conn is null !

That makes no sense! conn can never be null, because you have created an instance and assigned it to conn on line8
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Dave Tolls wrote:Where in that code are you actually setting the connection attribute?
It's not happening in your constructor.

I believe the connection is set using this line of code in the constructor



Ah.
I saw this in the constructor:

and thought "hang on"...
 
Alireza Azadi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Alireza Azadi wrote:The method returns nothing.

I'm not familiar with Android development, so no idea how you can easily add a few log statements. Let me ask another question: if you add that line to the constructor and you put a breakpoint on that line and start debugging, what's the value of connection? I assume it's null because something went wrong in the try block.

Alireza Azadi wrote:And in debug mode when i create a break point at line debugger says conn is null !

That makes no sense! conn can never be null, because you have created an instance and assigned it to conn on line8


A shows that code in my MainActivity via this Code, i displayed that into an AlertDialog


Believe me it returns null.
My DB collation is Persian, can it be the cause?
Cause before i successfully connect to SQL Server 2005 and everything was OK.
And just the collation is the difference between what i did with SQL Server 2005 and SQL Server 2012.
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And there's nothing in logcat?

For those exceptions, if the connection is a requirement for the app to run, I would simply let the app blow up (at least at the moment) rather than just log the exceptions.
If need be wrap them in a RuntimeException and throw it.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alireza Azadi wrote:Believe me it returns null.

No, I don't believe it! Your conn is a local reference variable (in the onCreate method) which is initialized a few lines before you used it. So conn can never be null!

Now the code that you show, just uses conn but you didn't show any code to create conn (and it can't be the same one as the one in MainActivity because that was a local variable). But I can believe conn.getMyConnection() is returning null, because (as mentioned in several posts) the try-block is throwing some kind of exception (and then the connection field in the ConnectionHelper class is not initialized and thus null). But according to you, there's nothing in the logs. So either you have to debug your application (and especially the try and catch blocks) or change the catch handlers toand then your application will blow up while running and you get the actual root cause of your problem. Because the NullPointerException you are experiencing is probably just a side-effect, it's not the root cause! (unless you are of course invoking setConnection in the ConnectionHelper class with null as parameter somewhere in the code)

Hope it helps!
Kind regards,
Roel
 
Alireza Azadi
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Alireza Azadi wrote:Believe me it returns null.

No, I don't believe it! Your conn is a local reference variable (in the onCreate method) which is initialized a few lines before you used it. So conn can never be null!

Now the code that you show, just uses conn but you didn't show any code to create conn (and it can't be the same one as the one in MainActivity because that was a local variable). But I can believe conn.getMyConnection() is returning null, because (as mentioned in several posts) the try-block is throwing some kind of exception (and then the connection field in the ConnectionHelper class is not initialized and thus null). But according to you, there's nothing in the logs. So either you have to debug your application (and especially the try and catch blocks) or change the catch handlers toand then your application will blow up while running and you get the actual root cause of your problem. Because the NullPointerException you are experiencing is probably just a side-effect, it's not the root cause! (unless you are of course invoking setConnection in the ConnectionHelper class with null as parameter somewhere in the code)

Hope it helps!
Kind regards,
Roel


I solved it !
I finally solved it ! :-D
Why you do not believe me? :-D
It returned null.
Problem was collation.
The collation of SQL Server 2012 i want to connect is Persian_....
Now i changed the Collation to defualt Latin_General_.....
And returns net.sourceforge.....
Thanks all for pay attention.
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alireza Azadi wrote:
I solved it !
I finally solved it ! :-D
Why you do not believe me? :-D
It returned null.
Problem was collation.
The collation of SQL Server 2012 i want to connect is Persian_....
Now i changed the Collation to defualt Latin_General_.....
And returns net.sourceforge.....
Thanks all for pay attention.


I would lay good odds that this mismatch was throwing an exception...
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alireza Azadi wrote:Why you do not believe me? :-D
It returned null.

Maybe you should read my post(s) more carefully
Roel De Nijs wrote:But I can believe conn.getMyConnection() is returning null, because (as mentioned in several posts) the try-block is throwing some kind of exception (and then the connection field in the ConnectionHelper class is not initialized and thus null). But according to you, there's nothing in the logs.
So I did believe you! I just didn't believe this part in one of your posts
Alireza Azadi wrote:debugger says conn is null !


And I can't still believe there was nothing in the logs! conn.getMyConnection() will only return null if and only if the try block throws an exception while creating the Connection object. And because all catch handlers write to some log, there should be various entries in the log file. And probably the root cause could have been easily discovered as well by debugging the application and setting a breakpoint in (probably) the catch handler for SQLException.

Alireza Azadi wrote:I solved it !
I finally solved it ! :-D

Glad to hear you solved it!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic