My program was working fine until i introduced the if statement, without this "if" there are (java.lang.NullPointerException) errors .
What shall i do?
Thank you in advance .
Here is the necessary part of code for you:

Connection c ; 
Statement stmt =null ; 
if (c != null) {
try { 
stmt = c.createStatement(); 
String sql = "INSERT INTO Mytable (age,name,salary) VALUES ( ?,?,? )"; 
PreparedStatement pst = c.prepareStatement(sql);
pst.setInt(1, 38);
pst.setString(2, "Jim");
pst.setInt(3, 900); 
stmt.execute(sql);
stmt.close(); 
c.commit(); 
c.close();
} catch (SQLException exp) { exp.printStackTrace();}
System.exit(0); }}} 

Recommended Answers

All 9 Replies

Member Avatar for RudyM

You're not initializing c. Set it to null, it's been a while since I've used Java.

What shall i do?

Did you ever tried indentation?

Setting c to null wont fix your issue. You need to create a connection by using a DriverManager or a DataSource for instance.

Establishing a Connection

Member Avatar for RudyM

My program was working fine until i introduced the if statement

This is why I went straight into the if statement.

Almost everything you do is inside the if statement.

Your statement stmt = c.createStatement(); will produce a null pointer, because all you did was Connection c ;. If statement or not, defining c is not enough to create a database connection.

edit:
@RudyM I'm sorry, I thought you were the original poster.
You're right, the phrasing is a tad confusing; claiming issues before introducing the if statement and at the same time having no issues before introducing it.

Almost everything you do is inside the if statement.

Who can tell what is in the if statement and what is not with this formatting? Certainly the OP cannot. Three opening brackets and five closing brackets if I counted right. THAT is a problem in itself. I'm with ddanbe on this one. The lack of attention to detail in the formatting and the overall original post makes it harder to help you. I concur with the others. Adding the null check in the if statement certainly did not cause this problem. It doesn't solve the problem, but you only get INTO the if-statement if c is NOT null and that is a GOOD thing because it PREVENTS an error if c is null.

Re-reading this statement, it makes even less sense...

My program was working fine until i introduced the if statement, without this "if" there are (java.lang.NullPointerException) errors .

It was either "working fine" before you added the if statement or there were Null Pointer Exceptions before you added the if statement.

commented: Great thinking. +15

Who can tell what is in the if statement and what is not with this formatting?

Everybody who looks at the opening and closing brackets.

The OP clearly stated:

My program was working fine until i introduced the if statement, without this "if" there are (java.lang.NullPointerException) errors .

.. OK, "clearly" might not be the right word here.
Either the goal of the application is to throw NullPointerExceptions, or it was 'working' far from fine.

The actual question here is:

Why "if" statement isn't treated?

Well .. it IS treated. The condition just never will evaluate to true, since the variable isn't instantiated, which means the statements in the if block aren't executed, which is an entirely other thing.
My opinion: the OP should spend some more time studying the basics, before trying to jump into persistence (even if with basic JDBC).

I just saw RudyM had been negative-voted and the OP had not. How did THAT happen? Now fixed. :)

I find it interesting that we're all speculating about what the OP might have meant and the OP hasn't come back to actually TELL us what he/she meant. I reckon that if you added up the time we've spent responding to this thread compared to the time the OP spent crafting the thread, we'd "win" by a long shot. I've been spending way too much time trying to figure out what people are asking on this forum when they should simply tell us clearly in the first place.

And we should add to this all the time wasted by the folks over at Stack Overflow. Duplicate thread. No surprise. Anyone wanting to see the formatted code can go over there.

http://stackoverflow.com/questions/37010822/why-my-program-doesnt-treat-the-if-statement

And by duplicate thread, I mean VERBATIM, bad grammar and all. The time posted seems the same, almost as if the OP immediately posted TWO threads on different forums without bothering to tell either forum about the other. Naturally the OP got the same answers. And naturally the OP had no follow-up on either forum.

This isn't what forums are supposed to be about. In the old days, there was an etiquette and folks like the OP were immediately banned for wasting everyone's time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.