-2

I decided to try to connect a java client to a mysql db running on wamp. Can anybody tell me why im getting this runtime problem?

My DBConnect() class

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DbConnect {

    private Connection conn;
    private Statement statement;
    private ResultSet resultset;

    public DbConnect(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            conn=DriverManager.getConnection("jdbc:mysql://xxx.xxx.xxx.xxx:3306/pk","root","");
        }       catch (Exception e){
            System.out.println("Error: " + e);
        }
    }

    public String getData(){
        String result ="";
        try {
            resultset = statement.executeQuery("select * from data_user");
            while(resultset.next())
            {
                result+=resultset.getString("userFirstName");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
        return result;  
    }

My main() class

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        DbConnect conn = new DbConnect();
        System.out.println(conn.getData());
    }

My error at runtime

Error: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. Exception in thread "main" java.lang.NullPointerException at DbConnect.getData(DbConnect.java:26) at Main.main(Main.java:8) ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2 JDWP exit error AGENT_ERROR_NO_JNI_ENV(183): [util.c:840]

}

Offending line

    resultset = statement.executeQuery("select * from data_user"); 
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
Fearghal
  • 10,569
  • 17
  • 55
  • 97
  • It's just a regular null pointer. – takendarkk Nov 18 '15 at 17:31
  • Thx Im not sure why this is a null pointer. can you explain what im missing? – Fearghal Nov 18 '15 at 17:40
  • There are many questions here already about NPEs. – takendarkk Nov 18 '15 at 17:41
  • In this case i open my connection, i instantiate my statement and execute it in connection with a resultSet – Fearghal Nov 18 '15 at 17:42
  • Open your debugger and watch the variables to see what is null then. – takendarkk Nov 18 '15 at 17:43
  • 1
    Please don't completely change your question after you've received answers. – Sotirios Delimanolis Nov 18 '15 at 17:45
  • how can you not change your approach if people are indicating the error is in a diff place? I was thrown by the weird red JDWP error. that would be insanity. The question hasnt changed. I asked where the null is likely to be, its hardly completely changing it. try not to be as aggressive in future and i'll try not to miss obvious errors. – Fearghal Nov 18 '15 at 17:50
  • @takendarkk -my bad, it the connection is null as it fails to connect - timed out error...i'll have to try to chase that now thx, – Fearghal Nov 18 '15 at 17:52
  • Just a quick thought but is your database server running? – John Kane Nov 18 '15 at 20:10
  • It is yes but thats not to say it wasnt, to be honest with the combination of server performance issues, eclipse issues and wamp config it was a mix of things hut essentially it camedown to a null connection due to a closed port – Fearghal Nov 18 '15 at 21:10

1 Answers1

0

It looks like you didnt instantiate your statement, unless you are missing some code. You need to do something like the following: statement=conn.createStatement(); Take a look at this example

John Kane
  • 4,383
  • 1
  • 24
  • 42
  • Thx but i put this in and still get the error. Im not sure why this is a null pointer. can you explain what im missing? – Fearghal Nov 18 '15 at 17:40
  • Ok so the connection init didnt work. arggh. Fromt he tutorial im using i dont need to init the statment but what do i know....thx for your help. – Fearghal Nov 18 '15 at 17:47